elasticsearch 如何在Elasticsearch 7.5中过滤嵌套对象?,elasticsearch,filter,nested,date-range,elasticsearch,Filter,Nested,Date Range" /> elasticsearch 如何在Elasticsearch 7.5中过滤嵌套对象?,elasticsearch,filter,nested,date-range,elasticsearch,Filter,Nested,Date Range" />

elasticsearch 如何在Elasticsearch 7.5中过滤嵌套对象?

elasticsearch 如何在Elasticsearch 7.5中过滤嵌套对象?,elasticsearch,filter,nested,date-range,elasticsearch,Filter,Nested,Date Range,我有一个映射: "ntol-2020-05" : { "mappings" : { { "properties": { "_createdAt": { "type": "date" }, "_logType": { "type": "text",

我有一个映射:

      "ntol-2020-05" : {
        "mappings" : {
          {
            "properties": {
              "_createdAt": {
                "type": "date"
              },
              "_logType": {
                "type": "text",
                "fields": {
                  "keyword": {
                    "type": "keyword",
                    "ignore_above": 256
                  }
                }
              },
              "device": {
                "properties": {
                  ...
                }
              },
              "resp": {
                "type": "nested",
                "properties": {
                  "data": {
                    "type": "nested",
                    "properties": {
                      ...
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
我使用三个条件进行筛选:

  • “日志类型”是“爬虫”
  • “2020-05-23”上的“创建数据”
  • “resp”的大小=0
我正在尝试使用查询进行筛选:

{“查询”:{“bool”:{“must”:[{“term”:{“logType”:{“value”:“crawler”}}}},{“range”:{“createdAt”:{“gte”:“2020-05-23”,“lte”:“2020-05-23”,“时区”:“+07:00”}},{“嵌套”:{“路径”:“resp”,“查询”:{“脚本”:{“源代码”:“doc resp”['resp']].size()>0”}},},{“大小”}},}

它返回错误:

  "type": "script_exception",
  "reason": "runtime error",
  "script_stack": [
    "org.elasticsearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:94)",
    "org.elasticsearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:41)",
    "doc['resp'].size() > 0",
    "    ^---- HERE"
  ],
  "script": "doc['resp'].size() > 0",
  "lang": "painless",
  "caused_by": {
    "type": "illegal_argument_exception",
    "reason": "No field found for [resp] in mapping with types []"
  }
}
如果我使用script
“doc.containsKey('resp')&&doc['resp'].size()>0”
,那么它将返回hits length=0

帮帮我。谢谢


您可以使用exists返回“嵌套”字段“resp”有值的文档

{
  "query": {
    "bool": {
      "must": [
        {
          "nested": {
            "path": "resp",
            "query": {
              "bool": {
                "filter": {
                  "exists": {
                    "field": "resp"
                  }
                }
              }
            }
          }
        }
      ]
    }
  },
  "from": 0,
  "size": 10
}

您可以使用exists返回“嵌套”字段“resp”有值的文档

{
  "query": {
    "bool": {
      "must": [
        {
          "nested": {
            "path": "resp",
            "query": {
              "bool": {
                "filter": {
                  "exists": {
                    "field": "resp"
                  }
                }
              }
            }
          }
        }
      ]
    }
  },
  "from": 0,
  "size": 10
}

@塔诺比很高兴能成为你的朋友help@TaNoBi很高兴能帮上忙