Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using elasticsearch aggregations on script_field values?

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?

like image 298
coryfoo Avatar asked Aug 12 '14 06:08

coryfoo


People also ask

What is Elasticsearch 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.

How to access script Fields by aggregations?

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.

What data does Elasticsearch return?

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.

What scripts are used in the scripted metric aggregation?

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.


1 Answers

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

like image 179
progrrammer Avatar answered Nov 14 '22 21:11

progrrammer