Skip to content

Commit

Permalink
Fixing composite aggregations with correct parameters (opensearch-pro…
Browse files Browse the repository at this point in the history
…ject#967)

* Fixing composite aggregations with correct parameters

Signed-off-by: Vacha Shah <vachshah@amazon.com>

* Adding tests

Signed-off-by: Vacha Shah <vachshah@amazon.com>

* Adding guides, samples and fixing tests

Signed-off-by: Vacha Shah <vachshah@amazon.com>

* Addressing comments

Signed-off-by: Vacha Shah <vachshah@amazon.com>

---------

Signed-off-by: Vacha Shah <vachshah@amazon.com>
  • Loading branch information
VachaShah authored May 7, 2024
1 parent de05226 commit cc6c273
Show file tree
Hide file tree
Showing 10 changed files with 926 additions and 33 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ This section is for maintaining a changelog for all breaking changes for the cli
### Fixed
- Fix version and build ([#254](https://github.com/opensearch-project/opensearch-java/pull/254))
- 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

0 comments on commit cc6c273

Please sign in to comment.