首页 > 解决方案 > 结合嵌套查询获取非法状态异常未能找到路径下的嵌套对象

问题描述

我正在 Elasticsearch 上创建一个查询,用于通过所有索引查找文档。
我需要在 Elasticsearch 上结合应该、必须和嵌套查询,我得到正确的结果,但结果中出现错误。

这是我正在使用的查询

GET _all/_search
{
  "query": {
      "bool": {
        "minimum_should_match": 1,
        "should": [
          { "term": { "trimmed_final_url": "https://www.repubblica.it/t.../" } }
        ],
        "must": [
          {
            "nested": {
              "path": "entities",
              "query": {
                "bool": {
                  "must": [
                    { "term": { "entities.id": "138511" } }
                  ]
                }
              }
            }
          },
          {
            "term": {
              "language": { "value": "it" }
             }
          }
        ]
      }
  }

这就是结果

{
  "_shards" : {
    "total" : 38,
    "successful" : 14,
    "skipped" : 0,
    "failed" : 24,
    "failures" : [
      {
        "shard" : 0,
        "index" : ".kibana_1",
        "node" : "7twsq85TSK60LkY0UiuWzA",
        "reason" : {
          "type" : "query_shard_exception",
          "reason" : """
            failed to create query: {
            ...
              "index_uuid" : "HoHi97QFSaSCp09iSKY1DQ",
              "index" : ".reporting-2019.06.02",
              "caused_by" : {
                "type" : "illegal_state_exception",
                "reason" : "[nested] failed to find nested object under path [entities]"
              }
            }
          },
            ...
  "hits" : {
    "total" : {
      "value" : 50,
      "relation" : "eq"
    },
    "max_score" : 16.90015,
    "hits" : [
      {
        "_index" : "i_201906_v1",
        "_type" : "_doc",
        "_id" : "MugcbmsBAzi8a0oJt96Q",
        "_score" : 16.90015,
        "_source" : {
          "language" : "it",
          "entities" : [
            {
              "id" : 101580,
            },
            {
              "id" : 156822,
            },
            ...

由于代码太长,我没有写一些字段

标签: elasticsearchnested

解决方案


我是 StackOverFlow 的新手(让这个帐户来回答这个问题:D)所以如果这个答案不符合我的要求。我最近一直在涉足 Elasticsearch 中的嵌套字段,所以我对这个错误是如何出现的有一些想法。您是否为文档类型定义了映射?如果您不在映射中告诉它这样做,我不相信 Elasticsearch 会将该字段识别为嵌套:

PUT INDEX_NAME
{
  "mappings": {
      "DOC_TYPE": {
          "properties": {
              "entities": {"type": "nested"}
          }
      }
   }
}

您可能必须为每个索引和文档类型指定此映射。不确定是否有一种方法可以通过一个请求完成所有操作。

我还注意到您有一个“应该”子句,最小匹配项设置为 1。我相信这与“必须”子句完全相同,所以我不确定它的目的是什么(如果我错了,请纠正我)。如果指定了映射,则查询应如下所示:

GET /_all/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "nested": {
            "path": "entities",
            "query": {
              "term": {
                "entities.id": {
                  "value": "138511"
                }
              }
            }
          }
        },
        {
          "term": {
            "language": {
              "value": "it"
            }
          }
        },
        {
          "term": {
            "trimmed_final_url": {
              "value": "https://www.repubblica.it/t.../"
            }
          }
        }
      ]
    }
  }
}

推荐阅读