|
18 | 18 | import co.elastic.clients.elasticsearch._types.aggregations.Aggregate;
|
19 | 19 |
|
20 | 20 | import java.util.ArrayList;
|
| 21 | +import java.util.HashMap; |
21 | 22 | import java.util.List;
|
22 | 23 | import java.util.Map;
|
23 | 24 |
|
|
28 | 29 | * AggregationsContainer implementation for the Elasticsearch aggregations.
|
29 | 30 | *
|
30 | 31 | * @author Peter-Josef Meisch
|
| 32 | + * @author Sascha Woo |
31 | 33 | * @since 4.4
|
32 | 34 | */
|
33 | 35 | public class ElasticsearchAggregations implements AggregationsContainer<List<ElasticsearchAggregation>> {
|
34 | 36 |
|
35 | 37 | private final List<ElasticsearchAggregation> aggregations;
|
| 38 | + private final Map<String, ElasticsearchAggregation> aggregationsAsMap; |
36 | 39 |
|
37 |
| - public ElasticsearchAggregations(List<ElasticsearchAggregation> aggregations) { |
| 40 | + public ElasticsearchAggregations(Map<String, Aggregate> aggregations) { |
38 | 41 |
|
39 | 42 | Assert.notNull(aggregations, "aggregations must not be null");
|
40 | 43 |
|
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)))); |
50 | 47 |
|
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()); |
56 | 50 | }
|
57 | 51 |
|
58 | 52 | @Override
|
59 | 53 | public List<ElasticsearchAggregation> aggregations() {
|
60 | 54 | return aggregations;
|
61 | 55 | }
|
| 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 | + |
62 | 74 | }
|
0 commit comments