0

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

Nkosi
  • 235,767
  • 35
  • 427
  • 472
David Marko
  • 2,477
  • 3
  • 27
  • 58

2 Answers2

0

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.

Andy
  • 49,085
  • 60
  • 166
  • 233
Marcel
  • 377
  • 5
  • 16
0

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.

jmlw
  • 91
  • 1
  • 3