A
A
Andrey Kulikovsky2016-03-18 19:46:26
elasticsearch
Andrey Kulikovsky, 2016-03-18 19:46:26

How to use strings for selections requiring "numeric"?

The essence of the problem is this. The type has constant fields, as well as additional dynamic fields, and they are different for each record.

// Постоянные поля
product_id
title
description
// динамические, у каждой записи они могут быть разные
property_45
property_7

For example, you need to make a selection of the form (or data aggregation):
{
    "range" : {
        "property_7" : {
            "gte" : 10,
            "lte" : 20,
        }
    }
}

Because field type "property_7" string, then the selection will be made according to the string algorithm. How to convert data on the fly to float and only then perform the necessary actions on it?
I googled the solution with the script, but it still didn’t work:
{
    "aggs" : {
        "my_stats" : { "stats" : { 
            "script" : "try { Integer.parseInt(doc['property_7'].value); } catch(Exception e){ return 0;}"
        }}
    }
}

Throws exception: "script_exception: failed to compile groovy script"

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
exenza, 2016-03-19
@by25

It looks like we are talking about Dynamic mapping
In dynamic mapping, the data type can be specified, this is called Dynamic field mapping .
This whole thing can be customized.
The solution with the script - seems like a terrible hack, IMHO.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question