按嵌套数组的长度过滤

Won*_*nay 6 elasticsearch

这是我的映射:

{"field_name": {
    "dynamic": "strict",
    "properties": {...},
    "type": "nested"
}}
Run Code Online (Sandbox Code Playgroud)

我正在尝试仅过滤至少具有一个field_name.

我试过:

{"query": {
    "bool": {
        "filter": [ { "script" : {
              "script" : {
                   "inline": "doc['field_name'].length >= 1",
                   "lang": "painless"
        } } ]
    }
} }
Run Code Online (Sandbox Code Playgroud)

但 Elasticsearch 正在对我大喊大叫No field found for [field_name] in mapping with types [type_name]

我还尝试将之前的查询包装到 a 中,nested但也不起作用:

{ "nested": {
     "path": "field_name",
     "query": {
          "bool": {
              "filter": [ {
                   "script": {
                        "script": {
                            "inline": "doc['field_name'].length >= 1",
                            "lang": "painless"
                        }
                   }
              } ]
         }
    }
} }
Run Code Online (Sandbox Code Playgroud)

这给出了与上面相同的错误。

有任何想法吗?

Lam*_*bda 6

如果所有对象都有相同的字段,您可以使用exist来检查对象是否存在,然后使用sum来计算计数,然后使用脚本分数来选择您想要的条件。就像下面的代码

{
  "query": {
    "function_score": {
      "query": {
        "nested": {
          "path": "field_name",
          "query": {
            "exists": {
              "field": "field_name.same_field"
            }
          },
          "score_mode": "sum"
        }
      },
      "functions": [
        {
          "script_score": {
            "script": {
              "source": "_score >= 1 ? 1 : 0"
            }
          }
        }
      ],
      "boost_mode": "replace"
    }
  },
  "min_score": 1
}
Run Code Online (Sandbox Code Playgroud)


Won*_*nay 3

我最终做的是my_array_length在施工期间添加一个字段。这样我就可以按该字段的值进行过滤。