I have created groovy script to calculate new field values. I then can use that script in my queries to calculate the new field value using a 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 just fine and I see results that each have a fields object containing my_field in it. Perfect.
Now I want to use a terms aggregation to get the 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 runs just fine and I still see my calculated results in every field, however the aggregations object contains one key, counts_by_my_field, with an empty buckets array inside.
What am I missing? Is it possible to use script_fields in aggregations?