7

Use elastic search aggregations on script_field values?

 2 years ago
source link: https://www.codesd.com/item/use-elastic-search-aggregations-on-script-field-values.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Use elastic search aggregations on script_field values?

advertisements

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?


While I have not been able to make an aggregation use a script_field value for the aggregation, I have discovered another way of accomplishing what I had hoped to do. It turns out that the aggregation will accept a script_id configuration that can calculate the script field value during aggregation.

Here is my example working with the script_id as part of the aggregation instead:

{
    "query": {
        "filtered": {
            "query": {
                "bool": {
                    "must": { "match_all": {}}
                }
            }
        }
    },
    "aggs": {
        "counts_by_my_field": {
            "terms": {
                "script_id": "my_field_calculator",
                "lang" : "groovy",
                "params": {}
            }
        }
    }
}


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK