Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Backport 2.x] Fixing composite aggregations with correct parameters #974

Merged
merged 2 commits into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)

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

### Security

Expand Down
22 changes: 22 additions & 0 deletions guides/search.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
- [Basic Search](#basic-search)
- [Get raw JSON results](#get-raw-json-results)
- [Search documents using a match query](#search-documents-using-a-match-query)
- [Search documents using a hybrid query](#search-documents-using-a-hybrid-query)
- [Search documents using suggesters](#search-documents-using-suggesters)
- [Using completion suggester](#using-completion-suggester)
- [Using term suggester](#using-term-suggester)
- [Using phrase suggester](#using-phrase-suggester)
- [Aggregations](#aggregations)
- [Composite Aggregations](#composite-aggregations)

# Search

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

#### Composite Aggregations

```java
final Map<String, CompositeAggregationSource> comAggrSrcMap = new HashMap<>();
CompositeAggregationSource compositeAggregationSource1 = new CompositeAggregationSource.Builder().terms(
termsAggrBuilder -> termsAggrBuilder.field("title.keyword").missingBucket(false).order(SortOrder.Asc)
).build();
comAggrSrcMap.put("titles", compositeAggregationSource1);

CompositeAggregation compAgg = new CompositeAggregation.Builder().sources(comAggrSrcMap).build();
Aggregation aggregation = new Aggregation.Builder().composite(compAgg).build();

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));
SearchResponse<IndexData> response = client.search(request, IndexData.class);
for (Map.Entry<String, Aggregate> entry : response.aggregations().entrySet()) {
LOGGER.info("Agg - {}", entry.getKey());
entry.getValue().composite().buckets().array().forEach(b -> LOGGER.info("{} : {}", b.key(), b.docCount()));
}
```

You can find a working sample of the above code in [Search.java](../samples/src/main/java/org/opensearch/client/samples/Search.java).
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,16 @@
@JsonpDeserializable
public class CompositeAggregationSource implements JsonpSerializable {
@Nullable
private final TermsAggregation terms;
private final CompositeTermsAggregationSource terms;

@Nullable
private final HistogramAggregation histogram;
private final CompositeHistogramAggregationSource histogram;

@Nullable
private final DateHistogramAggregation dateHistogram;
private final CompositeDateHistogramAggregationSource dateHistogram;

@Nullable
private final GeoTileGridAggregation geotileGrid;

// ---------------------------------------------------------------------------------------------
private final CompositeGeoTileGridAggregationSource geotileGrid;

private CompositeAggregationSource(Builder builder) {

Expand All @@ -79,31 +77,31 @@ public static CompositeAggregationSource of(Function<Builder, ObjectBuilder<Comp
* API name: {@code terms}
*/
@Nullable
public final TermsAggregation terms() {
public final CompositeTermsAggregationSource terms() {
return this.terms;
}

/**
* API name: {@code histogram}
*/
@Nullable
public final HistogramAggregation histogram() {
public final CompositeHistogramAggregationSource histogram() {
return this.histogram;
}

/**
* API name: {@code date_histogram}
*/
@Nullable
public final DateHistogramAggregation dateHistogram() {
public final CompositeDateHistogramAggregationSource dateHistogram() {
return this.dateHistogram;
}

/**
* API name: {@code geotile_grid}
*/
@Nullable
public final GeoTileGridAggregation geotileGrid() {
public final CompositeGeoTileGridAggregationSource geotileGrid() {
return this.geotileGrid;
}

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

}

// ---------------------------------------------------------------------------------------------

/**
* Builder for {@link CompositeAggregationSource}.
*/

public static class Builder extends ObjectBuilderBase implements ObjectBuilder<CompositeAggregationSource> {
@Nullable
private TermsAggregation terms;
private CompositeTermsAggregationSource terms;

@Nullable
private HistogramAggregation histogram;
private CompositeHistogramAggregationSource histogram;

@Nullable
private DateHistogramAggregation dateHistogram;
private CompositeDateHistogramAggregationSource dateHistogram;

@Nullable
private GeoTileGridAggregation geotileGrid;
private CompositeGeoTileGridAggregationSource geotileGrid;

/**
* API name: {@code terms}
*/
public final Builder terms(@Nullable TermsAggregation value) {
public final Builder terms(@Nullable CompositeTermsAggregationSource value) {
this.terms = value;
return this;
}

/**
* API name: {@code terms}
*/
public final Builder terms(Function<TermsAggregation.Builder, ObjectBuilder<TermsAggregation>> fn) {
return this.terms(fn.apply(new TermsAggregation.Builder()).build());
public final Builder terms(Function<CompositeTermsAggregationSource.Builder, ObjectBuilder<CompositeTermsAggregationSource>> fn) {
return this.terms(fn.apply(new CompositeTermsAggregationSource.Builder()).build());
}

/**
* API name: {@code histogram}
*/
public final Builder histogram(@Nullable HistogramAggregation value) {
public final Builder histogram(@Nullable CompositeHistogramAggregationSource value) {
this.histogram = value;
return this;
}

/**
* API name: {@code histogram}
*/
public final Builder histogram(Function<HistogramAggregation.Builder, ObjectBuilder<HistogramAggregation>> fn) {
return this.histogram(fn.apply(new HistogramAggregation.Builder()).build());
public final Builder histogram(
Function<CompositeHistogramAggregationSource.Builder, ObjectBuilder<CompositeHistogramAggregationSource>> fn
) {
return this.histogram(fn.apply(new CompositeHistogramAggregationSource.Builder()).build());
}

/**
* API name: {@code date_histogram}
*/
public final Builder dateHistogram(@Nullable DateHistogramAggregation value) {
public final Builder dateHistogram(@Nullable CompositeDateHistogramAggregationSource value) {
this.dateHistogram = value;
return this;
}

/**
* API name: {@code date_histogram}
*/
public final Builder dateHistogram(Function<DateHistogramAggregation.Builder, ObjectBuilder<DateHistogramAggregation>> fn) {
return this.dateHistogram(fn.apply(new DateHistogramAggregation.Builder()).build());
public final Builder dateHistogram(
Function<CompositeDateHistogramAggregationSource.Builder, ObjectBuilder<CompositeDateHistogramAggregationSource>> fn
) {
return this.dateHistogram(fn.apply(new CompositeDateHistogramAggregationSource.Builder()).build());
}

/**
* API name: {@code geotile_grid}
*/
public final Builder geotileGrid(@Nullable GeoTileGridAggregation value) {
public final Builder geotileGrid(@Nullable CompositeGeoTileGridAggregationSource value) {
this.geotileGrid = value;
return this;
}

/**
* API name: {@code geotile_grid}
*/
public final Builder geotileGrid(Function<GeoTileGridAggregation.Builder, ObjectBuilder<GeoTileGridAggregation>> fn) {
return this.geotileGrid(fn.apply(new GeoTileGridAggregation.Builder()).build());
public final Builder geotileGrid(
Function<CompositeGeoTileGridAggregationSource.Builder, ObjectBuilder<CompositeGeoTileGridAggregationSource>> fn
) {
return this.geotileGrid(fn.apply(new CompositeGeoTileGridAggregationSource.Builder()).build());
}

/**
Expand Down Expand Up @@ -245,11 +247,10 @@ public CompositeAggregationSource build() {

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

op.add(Builder::terms, TermsAggregation._DESERIALIZER, "terms");
op.add(Builder::histogram, HistogramAggregation._DESERIALIZER, "histogram");
op.add(Builder::dateHistogram, DateHistogramAggregation._DESERIALIZER, "date_histogram");
op.add(Builder::geotileGrid, GeoTileGridAggregation._DESERIALIZER, "geotile_grid");

op.add(Builder::terms, CompositeTermsAggregationSource._DESERIALIZER, "terms");
op.add(Builder::histogram, CompositeHistogramAggregationSource._DESERIALIZER, "histogram");
op.add(Builder::dateHistogram, CompositeDateHistogramAggregationSource._DESERIALIZER, "date_histogram");
op.add(Builder::geotileGrid, CompositeGeoTileGridAggregationSource._DESERIALIZER, "geotile_grid");
}

}
Loading
Loading