Using elasticsearch brackets in script_field values?

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?

+5
source share
2 answers

While I could not create the aggregation, use the script_field value for the aggregation, I discovered another way to accomplish what I was hoping to do. It turns out that the aggregation will take on the script_id configuration, which can calculate the value of the script field during aggregation.

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

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

This is impossible, not yet. script_fields does not work when placing field in aggregation or in facet.

And there is no way to access aggregate script fields. See Explanations.

I dug up Elasticsearch implementation code,

Here is the Javadoc for, ValuesSourceAggregationBuilder # script () used by aggregating terms for scripts.

Sets a script that generates values. If the script is configured with the field (as in {@link #field (String)}), then * this script will be considered the value {@code script}. The value of A script will be applied to the extracted values ​​from * the data field (you can refer to this value in the script using the reserved variable {@code _value}). If only the script is configured * (and the no field is not set next to it), then the script will be responsible for generating the values ​​that will be aggregated. *

This means that you cannot send "script_id" either to aggregates. You can do it,

 POST index/type/_search { "aggs": { "name": { "terms": { "script": "_source.data[0]", "lang": "groovy", "params": {} } } } } 

Hope this helps! thanks

+2
source

Source: https://habr.com/ru/post/1200086/


All Articles