Skip to content

Commit c930394

Browse files
authored
[elc] add convenient methods to improve access to aggregations.
Original Pull Request #2266 Closes #2265
1 parent 33c9180 commit c930394

File tree

1 file changed

+27
-15
lines changed

1 file changed

+27
-15
lines changed

src/main/java/org/springframework/data/elasticsearch/client/elc/ElasticsearchAggregations.java

+27-15
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import co.elastic.clients.elasticsearch._types.aggregations.Aggregate;
1919

2020
import java.util.ArrayList;
21+
import java.util.HashMap;
2122
import java.util.List;
2223
import java.util.Map;
2324

@@ -28,35 +29,46 @@
2829
* AggregationsContainer implementation for the Elasticsearch aggregations.
2930
*
3031
* @author Peter-Josef Meisch
32+
* @author Sascha Woo
3133
* @since 4.4
3234
*/
3335
public class ElasticsearchAggregations implements AggregationsContainer<List<ElasticsearchAggregation>> {
3436

3537
private final List<ElasticsearchAggregation> aggregations;
38+
private final Map<String, ElasticsearchAggregation> aggregationsAsMap;
3639

37-
public ElasticsearchAggregations(List<ElasticsearchAggregation> aggregations) {
40+
public ElasticsearchAggregations(Map<String, Aggregate> aggregations) {
3841

3942
Assert.notNull(aggregations, "aggregations must not be null");
4043

41-
this.aggregations = aggregations;
42-
}
43-
44-
/**
45-
* convenience constructor taking a map as it is returned from the new Elasticsearch client.
46-
*
47-
* @param aggregationsMap aggregate map
48-
*/
49-
public ElasticsearchAggregations(Map<String, Aggregate> aggregationsMap) {
44+
Map<String, ElasticsearchAggregation> elasticsearchAggregationsAsMap = new HashMap<>(aggregations.size());
45+
aggregations.forEach((name, aggregate) -> elasticsearchAggregationsAsMap //
46+
.put(name, new ElasticsearchAggregation(new Aggregation(name, aggregate))));
5047

51-
Assert.notNull(aggregationsMap, "aggregationsMap must not be null");
52-
53-
aggregations = new ArrayList<>(aggregationsMap.size());
54-
aggregationsMap
55-
.forEach((name, aggregate) -> aggregations.add(new ElasticsearchAggregation(new Aggregation(name, aggregate))));
48+
this.aggregationsAsMap = elasticsearchAggregationsAsMap;
49+
this.aggregations = new ArrayList<>(aggregationsAsMap.values());
5650
}
5751

5852
@Override
5953
public List<ElasticsearchAggregation> aggregations() {
6054
return aggregations;
6155
}
56+
57+
/**
58+
* @return the {@link ElasticsearchAggregation}s keyed by aggregation name.
59+
*/
60+
public Map<String, ElasticsearchAggregation> aggregationsAsMap() {
61+
return aggregationsAsMap;
62+
}
63+
64+
/**
65+
* Returns the aggregation that is associated with the specified name.
66+
*
67+
* @param the name
68+
* @return the aggregation
69+
*/
70+
public ElasticsearchAggregation get(String name) {
71+
return aggregationsAsMap.get(name);
72+
}
73+
6274
}

0 commit comments

Comments
 (0)