Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use ABS function in ElasticSearch equery?

I'm using ElasticSearch version 5.1.

I'm trying to retrieve all documents, where the subtraction of two fields < |X|

Here is the query that I run:

post /my_idx/_search
{
  "query": {
    "bool": {
      "must": {
        "script": {
          "script": {
            "inline": "doc[\"f1\"].value - doc[\"f2\"].value < 9 && doc[\"f1\"].value - doc[\"f2\"].value > -9",
            "lang": "painless"
           }
         }
       }
     }
   }
}

I tried using the common mathematic function 'abs' without success.

How can I use this function in my query?

Thank you

like image 232
Nir Avatar asked Dec 06 '22 14:12

Nir


1 Answers

You can simply use Math.abs(...) method in painless scripts.

like image 57
Mohammad Mazraeh Avatar answered Dec 19 '22 15:12

Mohammad Mazraeh