0

如果字段存在,我正在尝试添加条件,然后根据它进行排序,否则使用另一个字段。因为其中之一将存在。这是我的查询:

GET /my_index/_search
{
  "query": {
    "match_all": {}
  },
  "sort": {
    "_script": {
      "type":"number",
      "script": "if(doc['contextDates.event.date'].value != 0){ return doc['contextDates.event.date'].value} else { return doc['contextDates.start.date'].value}",
    "order": "asc"
    }
  }
}

当我执行此查询时,我收到以下错误:

"failed_shards" : [
      {
        "shard" : 0,
        "index" : "my_inedx",
        "node" : "UxKwS8SIR-uIbzo5_0IbcQ",
        "reason" : {
          "type" : "script_exception",
          "reason" : "runtime error",
          "script_stack" : [
            "return doc['contextDates.event.date'].value} else { ",
            "                                     ^---- HERE"
          ],
          "script" : "if(doc['contextDates.event.date'].value != 0){ return doc['contextDates.event.date'].value} else { return doc['contextDates.start.date'].value}",
          "lang" : "painless",
          "position" : {
            "offset" : 84,
            "start" : 47,
            "end" : 99
          },
          "caused_by" : {
            "type" : "wrong_method_type_exception",
            "reason" : "cannot convert MethodHandle(Dates)JodaCompatibleZonedDateTime to (Object)double"
          }
        }
      }
    ]

我也尝试过Double.parseDouble方法,但它不起作用。这就是我的内部文件contextDates

"contextDates" : {
        "event" : {
         "date" : "2020-06-26T00:00:00.000Z",
         "resolution" : "day",
         "score" : 0,
         "type" : "event"
       }
     }
4

1 回答 1

0

您获得的 doc 值是JodaCompatibleZonedDateTime您尝试与 double 值进行比较的类型,因此您需要像这样修改脚本

if(doc['contextDates.event.date'].value.getMillis() != 0){ return doc['contextDates.event.date'].value.getMillis()} else { return doc['contextDates.start.date'].value.getMillis()}
于 2022-01-26T09:29:10.877 回答