Cannot apply [*] operation to types [org.elasticsearch.index.fielddata.ScriptDocValues.Doubles] and [java.lang.Double]

Viewed 1095

Here is the mappeing of the attribute for the index- "hoi" : { "type" : "float" }

This is the query that I want to execute. I want to change my attribute hoi with dynamic value and sort the doc on top of that, don't want to use runtime mapping as I have lower version of ES-

GET test/_search
{
    "sort" : {
        "_script" : {
            "type" : "number",
            "script" : {
                "lang": "painless",
                "source": "doc['hoi'] * params.factor",
                "params" : {
                    "factor" : 1.1
                }
            },
            "order" : "asc"
        }
    }
}

Error-

{
  "error" : {
    "root_cause" : [
      {
        "type" : "script_exception",
        "reason" : "runtime error",
        "script_stack" : [
          "doc['hoi'] * params.factor",
          "                                        ^---- HERE"
        ],
        "script" : "doc['hoi'] * params.factor",
        "lang" : "painless",
        "position" : {
          "offset" : 40,
          "start" : 0,
          "end" : 47
        }
      }
    ],
    "type" : "search_phase_execution_exception",
    "reason" : "all shards failed",
    "phase" : "query",
    "grouped" : true,
    "failed_shards" : [
      {
        "shard" : 0,
        "index" : "test",
        "node" : "5sfrbDiLQDOg82_sneaU1g",
        "reason" : {
          "type" : "script_exception",
          "reason" : "runtime error",
          "script_stack" : [
            "doc['hoi'] * params.factor",
            "                                        ^---- HERE"
          ],
          "script" : "doc['hoi'] * params.factor",
          "lang" : "painless",
          "position" : {
            "offset" : 40,
            "start" : 0,
            "end" : 47
          },
          "caused_by" : {
            "type" : "class_cast_exception",
            "reason" : "Cannot apply [*] operation to types [org.elasticsearch.index.fielddata.ScriptDocValues.Doubles] and [java.lang.Double]."
          }
        }
      }
    ]
  },
  "status" : 400
}

How to make it work?

1 Answers

doc['hoi'] itself is of type org.elasticsearch.index.fielddata.ScriptDocValues.Doubles and params.factor is of type java.lang.Double.

So the error you get means that some document has multiple values in your hoi field. So you need to reference it with doc['hoi'].value * params.factor instead, which will take the first hoi value from the array to compute the sort.

You can also check how many values there are with doc['hoi'].size().

UPDATE

If some docs don't have any values for hoi then you can adapt the script like this:

doc['hoi'].size() > 0 ? doc['hoi'].value * params.factor : 0
Related