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?
Elasticsearch - Aggregations. The aggregations framework collects all the data selected by the search query and consists of many building blocks, which help in building complex summaries of the data.
And there is no any method to access script fields by aggregations. See explanation. Here is Javadoc for, ValuesSourceAggregationBuilder#script () used by Terms aggregation for scripting . Sets the script which generates the values.
Elasticsearch returns an array of daily buckets. Within each bucket, it shows the number of documents ("doc_count") within each bucket as well as the revenue generated from each day ("daily_revenue"). You can also calculate multiple metrics per bucket.
The scripted metric aggregation uses scripts at 4 stages of its execution: Executed prior to any collection of documents. Allows the aggregation to set up any initial state. In the above example, the init_script creates an array transactions in the state object. Executed once per document collected. This is a required script.
That is not possible, not yet. script_fields dont work when placed as field
in aggregation or in facet.
And there is no any method to access script fields by aggregations. See explanation.
I digged into Elasticsearch implementation code,
Here is Javadoc for, ValuesSourceAggregationBuilder#script() used by Terms aggregation for scripting .
Sets the script which generates the values. If the script is configured along with the field (as in {@link #field(String)}), then * this script will be treated as a {@code value script}. A value script will be applied on the values that are extracted from * the field data (you can refer to that value in the script using the {@code _value} reserved variable). If only the script is configured * (and the no field is configured next to it), then the script will be responsible to generate the values that will be aggregated. *
This means, you cannot send "script_id" either to the aggregations. You can only do this,
POST index/type/_search
{
"aggs": {
"name": {
"terms": {
"script": "_source.data[0]",
"lang": "groovy",
"params": {}
}
}
}
}
Hope this helps!! Thanks
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With