迁移到 Elasticsearch 6:使用脚本和参数的请求不再有效

Migration to Elasticsearch 6 : request with script and params not working anymore

我正在将我的 Elasticsearch 从 v5.16 迁移到 6.8(然后迁移到 7.16),但是我对这种类型的请求有一些问题(见下文):在脚本中使用 params['_source'] 我不明白为什么。我没有在文档中发现任何重大更改。你能帮我么 ? 仅供参考:索引映射不包含“evts”

谢谢

{
  "query": {
    "bool": {
      "must": [
        { "match": { "closed": "false" }
        },
        {
          "script": {
            "script": {
              "source": "(params['_source']['evts'] !== null) && (params['_source']['evts']).length > 0",
              "lang": "painless"
            }
          }
        }
      ]
    }
  }}

回应

{
  "took": 6,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 2,
    "skipped": 0,
    "failed": 3,
    "failures": [
      {
        "shard": 1,
        "index": "myIndex",
        "node": "XXX",
        "reason": {
          "type": "script_exception",
          "reason": "runtime error",
          "script_stack": [
            "(params['_source']['evts'] !== null) && (params['_source']['evts']).length > 0",
            "       ^---- HERE"
          ],
          "script": "(params['_source']['evts'] !== null) && (params['_source']['piecesJointes']).length > 0",
          "lang": "painless",
          "caused_by": {
            "type": "null_pointer_exception",
            "reason": "Cannot invoke \"Object.getClass()\" because \"callArgs[0]\" is null"
          }
        }
      }
    ]
  },
  "hits": {
    "total": 0,
    "max_score": null,
    "hits": []
  }
}

评论摘要:

如解释 here since since version 6.X its no longer possible to access the source in query to filter documents. Only doc_values are accessible ( see here ).

在没有脚本的情况下执行此类查询的简洁方法是将 evts 添加到映射并使用 exists 查询与 must_not 子句相结合。

在这种情况下,由于 evts 是一个嵌套字段,因此工作查询可以是

{
      "query": {
        "bool": {
          "must": [
            {
              "nested": {
                "path": "evts",
                "query": {
                  "exists": {
                    "field": "evts"
                  }
                }
              }
            }
          ]
        }
      }
    }