Skip to content

Commit fb7bfdd

Browse files
VachaShahdblock
andauthored
[Backport 2.x] Fixing composite aggregations with correct parameters (#974)
* Fixing composite aggregations with correct parameters (#967) * Fixing composite aggregations with correct parameters Signed-off-by: Vacha Shah <[email protected]> * Adding tests Signed-off-by: Vacha Shah <[email protected]> * Adding guides, samples and fixing tests Signed-off-by: Vacha Shah <[email protected]> * Addressing comments Signed-off-by: Vacha Shah <[email protected]> --------- Signed-off-by: Vacha Shah <[email protected]> * Fix: test. (#972) Signed-off-by: dblock <[email protected]> Signed-off-by: Vacha Shah <[email protected]> --------- Signed-off-by: Vacha Shah <[email protected]> Signed-off-by: dblock <[email protected]> Co-authored-by: Daniel (dB.) Doubrovkine <[email protected]>
1 parent a64b258 commit fb7bfdd

File tree

10 files changed

+929
-33
lines changed

10 files changed

+929
-33
lines changed

Diff for: CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
1414

1515
### Fixed
1616
- Fix integer overflow for variables in indices stats response ([#960](https://github.com/opensearch-project/opensearch-java/pull/960))
17+
- Fix composite aggregations for search requests ([#967](https://github.com/opensearch-project/opensearch-java/pull/967))
1718

1819
### Security
1920

Diff for: guides/search.md

+22
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44
- [Basic Search](#basic-search)
55
- [Get raw JSON results](#get-raw-json-results)
66
- [Search documents using a match query](#search-documents-using-a-match-query)
7+
- [Search documents using a hybrid query](#search-documents-using-a-hybrid-query)
78
- [Search documents using suggesters](#search-documents-using-suggesters)
89
- [Using completion suggester](#using-completion-suggester)
910
- [Using term suggester](#using-term-suggester)
1011
- [Using phrase suggester](#using-phrase-suggester)
1112
- [Aggregations](#aggregations)
13+
- [Composite Aggregations](#composite-aggregations)
1214

1315
# Search
1416

@@ -299,4 +301,24 @@ for (Map.Entry<String, Aggregate> entry : searchResponse.aggregations().entrySet
299301
}
300302
```
301303

304+
#### Composite Aggregations
305+
306+
```java
307+
final Map<String, CompositeAggregationSource> comAggrSrcMap = new HashMap<>();
308+
CompositeAggregationSource compositeAggregationSource1 = new CompositeAggregationSource.Builder().terms(
309+
termsAggrBuilder -> termsAggrBuilder.field("title.keyword").missingBucket(false).order(SortOrder.Asc)
310+
).build();
311+
comAggrSrcMap.put("titles", compositeAggregationSource1);
312+
313+
CompositeAggregation compAgg = new CompositeAggregation.Builder().sources(comAggrSrcMap).build();
314+
Aggregation aggregation = new Aggregation.Builder().composite(compAgg).build();
315+
316+
SearchRequest request = SearchRequest.of(r -> r.index(indexName).query(q -> q.match(m -> m.field("title").query(FieldValue.of("Document 1")))).aggregations("my_buckets", aggregation));
317+
SearchResponse<IndexData> response = client.search(request, IndexData.class);
318+
for (Map.Entry<String, Aggregate> entry : response.aggregations().entrySet()) {
319+
LOGGER.info("Agg - {}", entry.getKey());
320+
entry.getValue().composite().buckets().array().forEach(b -> LOGGER.info("{} : {}", b.key(), b.docCount()));
321+
}
322+
```
323+
302324
You can find a working sample of the above code in [Search.java](../samples/src/main/java/org/opensearch/client/samples/Search.java).

Diff for: java-client/src/main/java/org/opensearch/client/opensearch/_types/aggregations/CompositeAggregationSource.java

+34-33
Original file line numberDiff line numberDiff line change
@@ -49,18 +49,16 @@
4949
@JsonpDeserializable
5050
public class CompositeAggregationSource implements JsonpSerializable {
5151
@Nullable
52-
private final TermsAggregation terms;
52+
private final CompositeTermsAggregationSource terms;
5353

5454
@Nullable
55-
private final HistogramAggregation histogram;
55+
private final CompositeHistogramAggregationSource histogram;
5656

5757
@Nullable
58-
private final DateHistogramAggregation dateHistogram;
58+
private final CompositeDateHistogramAggregationSource dateHistogram;
5959

6060
@Nullable
61-
private final GeoTileGridAggregation geotileGrid;
62-
63-
// ---------------------------------------------------------------------------------------------
61+
private final CompositeGeoTileGridAggregationSource geotileGrid;
6462

6563
private CompositeAggregationSource(Builder builder) {
6664

@@ -79,31 +77,31 @@ public static CompositeAggregationSource of(Function<Builder, ObjectBuilder<Comp
7977
* API name: {@code terms}
8078
*/
8179
@Nullable
82-
public final TermsAggregation terms() {
80+
public final CompositeTermsAggregationSource terms() {
8381
return this.terms;
8482
}
8583

8684
/**
8785
* API name: {@code histogram}
8886
*/
8987
@Nullable
90-
public final HistogramAggregation histogram() {
88+
public final CompositeHistogramAggregationSource histogram() {
9189
return this.histogram;
9290
}
9391

9492
/**
9593
* API name: {@code date_histogram}
9694
*/
9795
@Nullable
98-
public final DateHistogramAggregation dateHistogram() {
96+
public final CompositeDateHistogramAggregationSource dateHistogram() {
9997
return this.dateHistogram;
10098
}
10199

102100
/**
103101
* API name: {@code geotile_grid}
104102
*/
105103
@Nullable
106-
public final GeoTileGridAggregation geotileGrid() {
104+
public final CompositeGeoTileGridAggregationSource geotileGrid() {
107105
return this.geotileGrid;
108106
}
109107

@@ -141,83 +139,87 @@ protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) {
141139

142140
}
143141

144-
// ---------------------------------------------------------------------------------------------
145-
146142
/**
147143
* Builder for {@link CompositeAggregationSource}.
148144
*/
149145

150146
public static class Builder extends ObjectBuilderBase implements ObjectBuilder<CompositeAggregationSource> {
151147
@Nullable
152-
private TermsAggregation terms;
148+
private CompositeTermsAggregationSource terms;
153149

154150
@Nullable
155-
private HistogramAggregation histogram;
151+
private CompositeHistogramAggregationSource histogram;
156152

157153
@Nullable
158-
private DateHistogramAggregation dateHistogram;
154+
private CompositeDateHistogramAggregationSource dateHistogram;
159155

160156
@Nullable
161-
private GeoTileGridAggregation geotileGrid;
157+
private CompositeGeoTileGridAggregationSource geotileGrid;
162158

163159
/**
164160
* API name: {@code terms}
165161
*/
166-
public final Builder terms(@Nullable TermsAggregation value) {
162+
public final Builder terms(@Nullable CompositeTermsAggregationSource value) {
167163
this.terms = value;
168164
return this;
169165
}
170166

171167
/**
172168
* API name: {@code terms}
173169
*/
174-
public final Builder terms(Function<TermsAggregation.Builder, ObjectBuilder<TermsAggregation>> fn) {
175-
return this.terms(fn.apply(new TermsAggregation.Builder()).build());
170+
public final Builder terms(Function<CompositeTermsAggregationSource.Builder, ObjectBuilder<CompositeTermsAggregationSource>> fn) {
171+
return this.terms(fn.apply(new CompositeTermsAggregationSource.Builder()).build());
176172
}
177173

178174
/**
179175
* API name: {@code histogram}
180176
*/
181-
public final Builder histogram(@Nullable HistogramAggregation value) {
177+
public final Builder histogram(@Nullable CompositeHistogramAggregationSource value) {
182178
this.histogram = value;
183179
return this;
184180
}
185181

186182
/**
187183
* API name: {@code histogram}
188184
*/
189-
public final Builder histogram(Function<HistogramAggregation.Builder, ObjectBuilder<HistogramAggregation>> fn) {
190-
return this.histogram(fn.apply(new HistogramAggregation.Builder()).build());
185+
public final Builder histogram(
186+
Function<CompositeHistogramAggregationSource.Builder, ObjectBuilder<CompositeHistogramAggregationSource>> fn
187+
) {
188+
return this.histogram(fn.apply(new CompositeHistogramAggregationSource.Builder()).build());
191189
}
192190

193191
/**
194192
* API name: {@code date_histogram}
195193
*/
196-
public final Builder dateHistogram(@Nullable DateHistogramAggregation value) {
194+
public final Builder dateHistogram(@Nullable CompositeDateHistogramAggregationSource value) {
197195
this.dateHistogram = value;
198196
return this;
199197
}
200198

201199
/**
202200
* API name: {@code date_histogram}
203201
*/
204-
public final Builder dateHistogram(Function<DateHistogramAggregation.Builder, ObjectBuilder<DateHistogramAggregation>> fn) {
205-
return this.dateHistogram(fn.apply(new DateHistogramAggregation.Builder()).build());
202+
public final Builder dateHistogram(
203+
Function<CompositeDateHistogramAggregationSource.Builder, ObjectBuilder<CompositeDateHistogramAggregationSource>> fn
204+
) {
205+
return this.dateHistogram(fn.apply(new CompositeDateHistogramAggregationSource.Builder()).build());
206206
}
207207

208208
/**
209209
* API name: {@code geotile_grid}
210210
*/
211-
public final Builder geotileGrid(@Nullable GeoTileGridAggregation value) {
211+
public final Builder geotileGrid(@Nullable CompositeGeoTileGridAggregationSource value) {
212212
this.geotileGrid = value;
213213
return this;
214214
}
215215

216216
/**
217217
* API name: {@code geotile_grid}
218218
*/
219-
public final Builder geotileGrid(Function<GeoTileGridAggregation.Builder, ObjectBuilder<GeoTileGridAggregation>> fn) {
220-
return this.geotileGrid(fn.apply(new GeoTileGridAggregation.Builder()).build());
219+
public final Builder geotileGrid(
220+
Function<CompositeGeoTileGridAggregationSource.Builder, ObjectBuilder<CompositeGeoTileGridAggregationSource>> fn
221+
) {
222+
return this.geotileGrid(fn.apply(new CompositeGeoTileGridAggregationSource.Builder()).build());
221223
}
222224

223225
/**
@@ -245,11 +247,10 @@ public CompositeAggregationSource build() {
245247

246248
protected static void setupCompositeAggregationSourceDeserializer(ObjectDeserializer<CompositeAggregationSource.Builder> op) {
247249

248-
op.add(Builder::terms, TermsAggregation._DESERIALIZER, "terms");
249-
op.add(Builder::histogram, HistogramAggregation._DESERIALIZER, "histogram");
250-
op.add(Builder::dateHistogram, DateHistogramAggregation._DESERIALIZER, "date_histogram");
251-
op.add(Builder::geotileGrid, GeoTileGridAggregation._DESERIALIZER, "geotile_grid");
252-
250+
op.add(Builder::terms, CompositeTermsAggregationSource._DESERIALIZER, "terms");
251+
op.add(Builder::histogram, CompositeHistogramAggregationSource._DESERIALIZER, "histogram");
252+
op.add(Builder::dateHistogram, CompositeDateHistogramAggregationSource._DESERIALIZER, "date_histogram");
253+
op.add(Builder::geotileGrid, CompositeGeoTileGridAggregationSource._DESERIALIZER, "geotile_grid");
253254
}
254255

255256
}

0 commit comments

Comments
 (0)