Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to check for key exist in elastic-search painless parameters?

How to check for key exists in painless script map parameters. In below query check a.toString() key exist in params I've tried everything but didn't get it to work. Please help me

mapping :

"id": {
   "type": "long"
}

query:

{
  "query":{
    "bool":{
      "filter":[
        {
          "script": {
            "script": {
               "lang": "painless",
               "params": {
                 "29232":2541,
                 "minDistance": 0
               },
               "source": "def a=doc['id'].getValue();double distance=params[a.toString()]; return distance <= 1000 && distance >= params['minDistance']"
            }
          }
        }
      ]
    }
  }
}
like image 728
slifer2015 Avatar asked Jul 01 '19 10:07

slifer2015


People also ask

How do you debug a painless script?

Painless Debuggingedit For now the best way to debug embedded scripts is by throwing exceptions at choice places. While you can throw your own exceptions ( throw new Exception('whatever') ), Painless's sandbox prevents you from accessing useful information like the type of an object.

What is painless script in Elasticsearch?

Painless is a simple, secure scripting language designed specifically for use with Elasticsearch. It is the default scripting language for Elasticsearch and can safely be used for inline and stored scripts.

What is CTX in Elasticsearch?

ctx is a special variable that allows you to access the source of the object that you want to update. The ctx. _source is a writable version of the source . NOTE: You can modify this document in the script and the modified source will be persisted as the new version of the document.

How do you write an elastic search script?

Wherever scripting is supported in the Elasticsearch APIs, the syntax follows the same pattern; you specify the language of your script, provide the script logic (or source), and add parameters that are passed into the script: "script": { "lang": "...", "source" | "id": "...", "params": { ... } }


1 Answers

The params is just a Java Map object. So, the following checks if the key exists in the params and exits early with a false if it does not exist.

GET test/_search
{
  "query":{
    "bool":{
      "filter":[
        {
          "script": {
            "script": {
               "lang": "painless",
               "params": {
                 "29232":2541,
                 "minDistance": 0
               },
               "source": """
               def a=doc['id'].getValue();
               if (!params.containsKey(a.toString())) {
                 return false;
               }
               double distance=params[a.toString()]; 
               return distance <= 1000 && distance >= params['minDistance']
               """
            }
          }
        }
      ]
    }
  }
}
like image 59
Benjamin Trent Avatar answered Oct 10 '22 00:10

Benjamin Trent