Skip to content

Commit 64ba269

Browse files
committed
[elc] add convenient methods to improve access to aggregations
1 parent 33c9180 commit 64ba269

File tree

1 file changed

+45
-6
lines changed

1 file changed

+45
-6
lines changed

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

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

2020
import java.util.ArrayList;
21+
import java.util.Collections;
22+
import java.util.HashMap;
2123
import java.util.List;
2224
import java.util.Map;
2325

@@ -28,35 +30,72 @@
2830
* AggregationsContainer implementation for the Elasticsearch aggregations.
2931
*
3032
* @author Peter-Josef Meisch
33+
* @author Sascha Woo
3134
* @since 4.4
3235
*/
3336
public class ElasticsearchAggregations implements AggregationsContainer<List<ElasticsearchAggregation>> {
3437

3538
private final List<ElasticsearchAggregation> aggregations;
39+
private Map<String, ElasticsearchAggregation> aggregationsAsMap;
3640

3741
public ElasticsearchAggregations(List<ElasticsearchAggregation> aggregations) {
3842

3943
Assert.notNull(aggregations, "aggregations must not be null");
4044

4145
this.aggregations = aggregations;
46+
47+
if (aggregations.isEmpty()) {
48+
aggregationsAsMap = Collections.emptyMap();
49+
}
4250
}
4351

4452
/**
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.
4654
*
47-
* @param aggregationsMap aggregate map
55+
* @param aggregationsAsMap aggregate map
4856
*/
49-
public ElasticsearchAggregations(Map<String, Aggregate> aggregationsMap) {
57+
public ElasticsearchAggregations(Map<String, Aggregate> aggregationsAsMap) {
5058

51-
Assert.notNull(aggregationsMap, "aggregationsMap must not be null");
59+
Assert.notNull(aggregationsAsMap, "aggregationsAsMap must not be null");
5260

53-
aggregations = new ArrayList<>(aggregationsMap.size());
54-
aggregationsMap
61+
aggregations = new ArrayList<>(aggregationsAsMap.size());
62+
aggregationsAsMap
5563
.forEach((name, aggregate) -> aggregations.add(new ElasticsearchAggregation(new Aggregation(name, aggregate))));
64+
65+
if (aggregations.isEmpty()) {
66+
this.aggregationsAsMap = Collections.emptyMap();
67+
}
5668
}
5769

5870
@Override
5971
public List<ElasticsearchAggregation> aggregations() {
6072
return aggregations;
6173
}
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+
62101
}

0 commit comments

Comments
 (0)