|
18 | 18 | import co.elastic.clients.elasticsearch._types.aggregations.Aggregate;
|
19 | 19 |
|
20 | 20 | import java.util.ArrayList;
|
| 21 | +import java.util.Collections; |
| 22 | +import java.util.HashMap; |
21 | 23 | import java.util.List;
|
22 | 24 | import java.util.Map;
|
23 | 25 |
|
|
28 | 30 | * AggregationsContainer implementation for the Elasticsearch aggregations.
|
29 | 31 | *
|
30 | 32 | * @author Peter-Josef Meisch
|
| 33 | + * @author Sascha Woo |
31 | 34 | * @since 4.4
|
32 | 35 | */
|
33 | 36 | public class ElasticsearchAggregations implements AggregationsContainer<List<ElasticsearchAggregation>> {
|
34 | 37 |
|
35 | 38 | private final List<ElasticsearchAggregation> aggregations;
|
| 39 | + private Map<String, ElasticsearchAggregation> aggregationsAsMap; |
36 | 40 |
|
37 | 41 | public ElasticsearchAggregations(List<ElasticsearchAggregation> aggregations) {
|
38 | 42 |
|
39 | 43 | Assert.notNull(aggregations, "aggregations must not be null");
|
40 | 44 |
|
41 | 45 | this.aggregations = aggregations;
|
| 46 | + |
| 47 | + if (aggregations.isEmpty()) { |
| 48 | + aggregationsAsMap = Collections.emptyMap(); |
| 49 | + } |
42 | 50 | }
|
43 | 51 |
|
44 | 52 | /**
|
45 |
| - * convenience constructor taking a map as it is returned from the new Elasticsearch client. |
| 53 | + * Convenience constructor taking a map as it is returned from the new Elasticsearch client. |
46 | 54 | *
|
47 |
| - * @param aggregationsMap aggregate map |
| 55 | + * @param aggregationsAsMap aggregate map |
48 | 56 | */
|
49 |
| - public ElasticsearchAggregations(Map<String, Aggregate> aggregationsMap) { |
| 57 | + public ElasticsearchAggregations(Map<String, Aggregate> aggregationsAsMap) { |
50 | 58 |
|
51 |
| - Assert.notNull(aggregationsMap, "aggregationsMap must not be null"); |
| 59 | + Assert.notNull(aggregationsAsMap, "aggregationsAsMap must not be null"); |
52 | 60 |
|
53 |
| - aggregations = new ArrayList<>(aggregationsMap.size()); |
54 |
| - aggregationsMap |
| 61 | + aggregations = new ArrayList<>(aggregationsAsMap.size()); |
| 62 | + aggregationsAsMap |
55 | 63 | .forEach((name, aggregate) -> aggregations.add(new ElasticsearchAggregation(new Aggregation(name, aggregate))));
|
| 64 | + |
| 65 | + if (aggregations.isEmpty()) { |
| 66 | + this.aggregationsAsMap = Collections.emptyMap(); |
| 67 | + } |
56 | 68 | }
|
57 | 69 |
|
58 | 70 | @Override
|
59 | 71 | public List<ElasticsearchAggregation> aggregations() {
|
60 | 72 | return aggregations;
|
61 | 73 | }
|
| 74 | + |
| 75 | + /** |
| 76 | + * @return the {@link ElasticsearchAggregation}s keyed by aggregation name. |
| 77 | + */ |
| 78 | + public Map<String, ElasticsearchAggregation> asMap() { |
| 79 | + |
| 80 | + if (aggregationsAsMap == null) { |
| 81 | + Map<String, ElasticsearchAggregation> newAggregationsAsMap = new HashMap<>(aggregations.size()); |
| 82 | + for (ElasticsearchAggregation aggregation : aggregations) { |
| 83 | + newAggregationsAsMap.put(aggregation.aggregation().getName(), aggregation); |
| 84 | + } |
| 85 | + aggregationsAsMap = Collections.unmodifiableMap(newAggregationsAsMap); |
| 86 | + } |
| 87 | + |
| 88 | + return aggregationsAsMap; |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Returns the aggregation that is associated with the specified name. |
| 93 | + * |
| 94 | + * @param the name |
| 95 | + * @return the aggregation |
| 96 | + */ |
| 97 | + public ElasticsearchAggregation get(String name) { |
| 98 | + return asMap().get(name); |
| 99 | + } |
| 100 | + |
62 | 101 | }
|
0 commit comments