Thursday, 24 October 2019

Elasticsearch 1.7 scripted_metric analyzed string

I have a scripted metric and I'm trying to get the value of an analyzed string, which is returning an array of strings.

I'm trying to get the correct value for an analyzed field which is returning the string sorted alphabetically and split by spaces. Is working well well with not analyzed string.

Scripted metric with an analyzed field:

"aggs": {
            'influencers': {
                'scripted_metric': {
                    "init_script": "_agg['transactions'] = []",
                    'map_script': """
                        result = [:];
                        result['field_analyzed1'] = doc['field_analyzed1'].values;
                        result['field_analyzed2'] = doc['field_analyzed2'].value;
                        result['field_not_analyzed'] = doc['field_not_analyzed'].value;
                        _agg.transactions.add(result);
                        """
                }
            }
        }

I'm trying to get the following string Francisco Claudio Urbano but the result of field_analyzed1 is ['claudio', 'francisco', 'urbano'] and the result of field_analyzed2 is claudio, only the first string of the array. With the field field_not_analyzed the result is correct Francisco Claudio Urbano.

I think with newer versions I can do something like this or use painless scripting:

result['field_text'] = doc['field_text.keyword'].value;

But I need it with the ES 1.7

body = {
    'script_fields': {
        'test': {
            'script': 'doc["analyzed_field"].value',
            'lang': 'groovy'
        }
    }
}


from Elasticsearch 1.7 scripted_metric analyzed string

No comments:

Post a Comment