How to create FiltersAggregation query using JEST builders?

826 views Asked by At

How can I create FiltersAggregation query using JEST AggregationBuilders or similar? I looked at FiltersAggregationIntegrationTest but query part is defined directly by JSON and I need something more like AggregationBuilders (as I'm using this for standard term aggregation for example)

Link to FiltersAggregationIntegrationTest: https://github.com/searchbox-io/Jest/blob/master/jest/src/test/java/io/searchbox/core/search/aggregation/FiltersAggregationIntegrationTest.java

2

There are 2 answers

0
Marcel On

I've one possibility:

FilterAggregationBuilder testFilter = AggregationBuilders.filter("test");
testFilter.filter(FilterBuilders.typeFilter("typeName"));
new SearchSourceBuilder().aggregation(testFilter);

This is a filter by Type, but the FilterBuilders has a termFilter too.

0
jmlw On

Here is a solution that may work, I've cross posted this to the Jest github issue as well.

QueryBuilder filterTermsQuery = QueryBuilders.termsQuery("fieldName", "value1", "value2", "value3");
SearchSourceBuilder searchSourceBuilder = SearchSourceBuilder.searchSource()
    .query(boolQueryBuilder)
    .size(0)
    .aggregation(
        AggregationBuilders
            .filter("filterAggName") // returns FilterAggregationBuilder
            .filter(filterTermsQuery));

The gist is that you want to create a search source builder to use in the jest client, and supply that with and aggregation (which could also include sub-aggregations by chaining sub-aggregations on the AggregationBuilders method). Then define an aggregation of filter type available in AggregationBuilders. This returns a new builder of FilterAggregationBuilder where you can provide any QueryBuilder as the filter aggregation type. According to documentation, the .filter(termsQuery) call will cause only the documents that match the filter to fall into the bucket of this filter.

Hopefully this will resolve your issue unless I misunderstood your use case.