Benoit Hamon
Benoit Hamon

Reputation: 89

ElasticSearch Numeric Distance

I'm looking for a way to make a query that would return a score.

This score would be the numerical distance between 2 numbers.

Is there a way to do this in elasticsearch ?

for example if my data looks like this: { value: INT }

in my query I want a default parameters (an other INT)

and as result I want my data sorted with the score ( |object.value - default.value| )

Upvotes: 0

Views: 259

Answers (2)

Val
Val

Reputation: 217334

Using a function_score query (with script_score), you can achieve what you need:

GET /_search
{
    "query": {
        "function_score": {
            "query": {
                "match_all": { }
            },
            "script_score" : {
                "script" : {
                  "source": "Math.abs(doc['value'].value - params.default)",
                  "params": {
                    "default": 10
                  }
                }
            }
        }
    }
}

Upvotes: 1

Marios Karatisoglou
Marios Karatisoglou

Reputation: 223

If i understood correctly,you want something like this? It searches in the field range and returns all hits with score value between 0 and 6.

query = { 'query': { 'range': { 'score': { 'lte': 6, 'gte': 0 } } } }

Upvotes: 0

Related Questions