0

谁能帮我构建以下查询。运行此查询时出现以下错误。ES版本为7.9.0;在我的模型中有一个字段“repliedBy”,它是一个数组字段。它的值总是用空数组初始化。但在某些实体上,它有一个或几个对象。我需要编写一个查询来获取所有带有空数组的项目。

   GET myTable/_search
    {
      "query": {
        "bool": {
          "must": [
            {
              "script": {
                "script": {
                  "source": "doc['repliedBy'].size() == params.val",
                  "params": {
                    "val": 0
                  }
                }
              }
            },
            {
              "range": {
                "receivedDate": {
                  "gte": "2020-09-15T07:51:21.000Z",
                  "lte": "2020-12-01T07:51:21.000Z"
                }
              }
            }
          ]
        }
      }
    }

错误:

  "error" : {
    "root_cause" : [
      {
        "type" : "script_exception",
        "reason" : "runtime error",
        "script_stack" : [
          "org.elasticsearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:90)",
          "org.elasticsearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:41)",
          "doc['repliedBy'].size() == params.val",
          "    ^---- HERE"
        ],
        "script" : "doc['repliedBy'].size() == params.val",
        "lang" : "painless",
        "position" : {
          "offset" : 4,
          "start" : 0,
          "end" : 37
        }
      }
    ],
4

1 回答 1

1

这是bool/must_not/exists查询组合的工作,如下所示:

{
  "query": {
    "bool": {
      "must_not": [
        {
          "exists": {
            "field": "repliedBy.id"
          }
        }
      ],
      "filter": [
        {
          "range": {
            "receivedDate": {
              "gte": "2020-09-15T07:51:21.000Z",
              "lte": "2020-12-01T07:51:21.000Z"
            }
          }
        }
      ]
    }
  }
}
于 2020-12-15T11:51:09.727 回答