I created a groovy script to calculate the new field values. Then I can use this script in my queries to calculate the new field value using the script_fields parameter. Here is an example:
{ "query": { "filtered": { "query": { "bool": { "must": {"match_all": {}} } } } }, "script_fields":{ "my_field":{ "script_id":"my_field_calculator", "lang" : "groovy", "params": {} } } }
This works fine, and I see the results, each of which has a fields object containing my_field . Excellent.
Now I want to use term aggregation to get counts of each occurrence of this new field value, something like this:
{ "query": { "filtered": { "query": { "bool": { "must": {"match_all": {}} } } } }, "script_fields":{ "my_field":{ "script_id":"my_field_calculator", "lang" : "groovy", "params": {} } } "aggs": { "counts_by_my_field": { "terms": { "field": "my_field" } } } }
The query is doing very well, and I still see my calculated results in each field, however the aggregations object contains one key counts_by_my_field with an empty buckets array inside.
What am I missing? Can script fields be used in aggregates?
source share