elastic/elasticsearch-java

Missing filter aggregation

ixixix-001 opened this issue · 2 comments

Java API client version

8.15.2

Java version

21

Elasticsearch Version

8.15.2

Problem description

Hi, there is no way to define a filter aggregation (not "filters").

Seems the class "FilterAggregation" is not generated at all.
And: The code for method "filter()" in class "Aggregation" returns a Query object instead of an aggregation variant.

public Query filter() { return TaggedUnionUtils.get(this, Kind.Filter); }

Was this an intentional decision to implement it this way (as a Query-Object) ?
Otherwise it would be nice to add the class "Filteraggregation" in the generator and change the return types from Query to "FilterAggregation".

Hello, thanks for the report! I think this is correct, filter in an aggregation is none other than a query that can be used to filter the result of the aggregation, as the documentation states:

A single bucket aggregation that narrows the set of documents to those that match a query.

So to translate the example used in the documentation in java dsl:

POST /sales/_search?size=0&filter_path=aggregations
{
  "aggs": {
    "avg_price": { "avg": { "field": "price" } },
    "t_shirts": {
      "filter": { "term": { "type": "t-shirt" } },
      "aggs": {
        "avg_price": { "avg": { "field": "price" } }
      }
    }
  }
}
esClient.search(s -> s
        .aggregations(Map.of("t_shirts", Aggregation.of(a -> a
            .filter(f -> f
                .term(t -> t
                    .field("type")
                    .value(FieldValue.of("t-shirt"))
                ))
                .aggregations("avg_price", ag -> ag
                    .avg(av -> av
                        .field("price")
                    )
                )
            ), "avg_price", Aggregation.of(agg -> agg
            .avg(av -> av
                .field("price")
            ))))
        .index("sales")
        .size(0)
    , Void.class);