无法将 [*] 操作应用于类型 [org.elasticsearch.index.fielddata.ScriptDocValues.Doubles] 和 [java.lang.Double]

San*_*hit 3 elasticsearch

这是索引属性的映射 - "hoi" : { "type" : "float" }

这是我要执行的查询。我想用动态值更改我的属性 hoi 并在其之上对文档进行排序,不想使用运行时映射,因为我有较低版本的 ES-

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

错误-

{
  "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
}
Run Code Online (Sandbox Code Playgroud)

如何让它发挥作用?

Val*_*Val 6

doc['hoi']本身是 typeorg.elasticsearch.index.fielddata.ScriptDocValues.Doublesparams.factor是 type java.lang.Double

因此,您收到的错误意味着某些文档在您的hoi字段中有多个值。因此,您需要使用它来引用它doc['hoi'].value * params.factor,这将从数组中获取第一个hoi值来计算排序。

您还可以检查有多少个值doc['hoi'].size()

更新

如果某些文档没有任何值,hoi那么您可以像这样调整脚本:

doc['hoi'].size() > 0 ? doc['hoi'].value * params.factor : 0
Run Code Online (Sandbox Code Playgroud)