Search code examples
elasticsearchelasticsearch-java-apielasticsearch-query

ES Java dynamically add keyed filters to AggregationBuilder


I want to have a method that loops through an ArrayList and based on it's content dynamically generate x amount of Keyed Filters

List<KeyedFilter> filters = new ArrayList<KeyedFilter>();
for (String a: b) {
     filters.add(generateKeyedFilterFromList(a.key, a.value, a.buckets);
}

private KeyedFilter (generateKeyedFilterFromList(String key, String  value, String[] buckets) {
    KeyedFilter filter = new KeyedFilter(key, 
        QueryBuilder(value, buckets));
}

this return the list of KeyedFilters just fine, however I have yet to find a way to apply it to AggregationBuilder (String, KeyedFilter...)

AggregationBuilder agg = AggregationBuilders.filter("filterName", filters); 

tried an array

KeyedFilter[] filterArray = new KeyedFilter[filters.size()];
filterArray = filters.toArray(filterArray);
AggregationBuilder agg = AggregationBuilders.filter("filterName", filterArray); // honestly I am not completely sure why this doesn't work here

Solution

  • Apologies for answering my own question, but in case anyone else is interested. The above actually does work, it was another error that was causing it to fail.