I am using a json object to craft a searchSession query on the backend it looks like this:
{
"searchFilters":{
"diseaseRelationFilter":{"diseaseRelation": "6"},
"negatedFilter": {"negated":"false"}
},
"sortOrders": []
}
What I would like to be able to do is add aggregation fields to this object and have the backend have them in the results
"sortOrders": [],
"agg_field_list": ["agg_field1", "agg_field2"]
My code on the back looks like this:
SearchQuery<E> query = searchSession.search(myClass)
.where(w -> { /* ... Stuff to handle the searchFilters ... */ })
.sort(s -> { /* ... Stuff to handle the sortOrders ... */ })
.toQuery();
However I can not conditionally add aggregations… I would like to be able to do something like the following:
if(agg_field_list.size() > 0) {
for(String agg_field: agg_field_list) {
AggregationKey<Map<String, Long>> aggKey = AggregationKey.of(agg_field);
someObject.aggregation(aggKey, p -> p.terms().field(agg_field, String.class).maxTermCount(10);
}
}
This should generate the following ES query:
"aggregations": {
"agg_field1": {
"terms": {
"field": "agg_field1", "size": 10
}
},
"agg_field2": {
"terms": {
"field": "agg_field2", "size": 10
}
}
},
I have tried the following:
DefaultSearchQueryOptionsStep step = searchSession.search(myClass)
.where(w -> { /* ... Stuff to handle the searchFilters ... */ })
.sort(s -> { /* ... Stuff to handle the sortOrders ... */ });
step.aggregation(aggkey, a -> { /* ... Stuff to handle the agg_field_list */ });
However DefaultSearchQueryOptionsStep
is not visible therefore can’t be used like this. Also the three methods .where()
.sort()
and .aggregation()
are defined in the AbstractSearchQueryOptionsStep
class and these, do not seem to have a way to split them up. You have to chain them together.
So the documentation offers a direct json object, for options for creating these manual:
Example:
.aggregation(countsByPriceHistogramKey, f -> f.fromJson( "{"
+ "\"histogram\": {"
+ "\"field\": \"price\","
+ "\"interval\": 10"
+ "}"
+ "}" ) )
However this does not work as f
does not have the method fromJson
so the doc’s need to be updated in this case to remove all the examples showing the fromJson
:
Has someone run into this issue… is this a feature request? Bug? Just not sure where to turn. Thanks for your help.
The full example of how I am trying to use this can be found here: