1

I am trying to use the following Painless script query in one of my applications, but it does not seem to be working. Any problem with the syntax? I noticed that the part that does not really work is this Math.max(doc["level"].value, 1) - 1. Please, note that "id" field is a keyword and "level" is a byte.

{script: {script: {
        inline: 'doc["id"].value == params.parentDocIds[Math.max(doc["level"].value, 1) - 1]',
        params: {parentDocIds: parentDocIds}
}}}

Thank you!

geeko
  • 2,649
  • 4
  • 32
  • 59

1 Answers1

0

The following query will work. Change the index name to match your index and try it in Kibana.

GET todo-index-name-here/_search
{
  "query": {
    "script": {
      "script": {
        "inline": "def idx = (int)Math.max(doc['level'].value, 1) - 1; return (doc['id'].value == params.parentDocIds[idx])",
        "lang": "painless",
        "params": {
          "parentDocIds": [
            "x",
            "y",
            "z"
          ]
        }
      }
    }
  }
}
Sunil Purushothaman
  • 8,435
  • 1
  • 22
  • 20