0

Here is a query that I need to send to ElasticSearch :

{
  "query": {
    "match_all":{}
  },
  "sort": {
    "_script": {
      "type":"number",
      "script": {
        "inline":"statusMap[status] || 0",
        "params": {
          "statusMap": {
            "CAN":5,
            "COM":4,
            "HLD":3,
            "PEN":2,
            "INP":1
          }
        }
      },
      "order":"desc"
    }
  }
}

Where status is a field of type string. The parameter statusMap's value may differ from query to query, and I thought I could get around by just specifying the mapping value as is, since this is valid JavaScript anyway. Then I realized that the script is not JS, but Groovy.

The problem is that Groovy does not like the statusMap[status] at all. Is this JavaScript expression equivalent in Groovy? If not, what are the alternatives?

Edit

The error message is

{
   "error": {
      "root_cause": [
         {
            "type": "script_exception",
            "reason": "failed to run inline script [statusMap[status]] using lang [groovy]"
         }
      ],
      "type": "search_phase_execution_exception",
      "reason": "all shards failed",
      "phase": "query",
      "grouped": true,
      "failed_shards": [
         {
            "shard": 0,
            "index": "foo",
            "node": "8AcXwANfSd-HF-nyMXHDLw",
            "reason": {
               "type": "script_exception",
               "reason": "failed to run inline script [statusMap[status]] using lang [groovy]",
               "caused_by": {
                  "type": "missing_property_exception",
                  "reason": "No such property: status for class: 5ea995c6862849ebdbc5e3d4126d81302185a798"
               }
            }
         }
      ]
   },
   "status": 500
}

Note: changing statusMap[status] to statusMap[doc.status.value] yields a null_pointer_exception error message instead.

0

1 Answer 1

1

Your inline script needs to be like this instead:

    "inline":"statusMap[doc.status.value] ?: 0",

Also make sure that you have enabled dynamic scripting.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.