首页 > 解决方案 > “[nested] 未能在路径 [steps] 下找到嵌套对象”

问题描述

我已将数据映射到此架构中:

curl -X PUT "localhost:9200/data?pretty" -H 'Content-Type: application/json' -d 
'{
   "settings":{
      "number_of_shards":"1",
      "number_of_replicas":"1"
   },
   "mappings":{
      "properties":{
         "routines":{
            "type":"nested",
            "properties":{
               "title":{
                  "type":"text"
               },
               "sources":{
                  "type":"keyword"
               },
               "flags":{
                  "type":"keyword",
                  "null_value":"NULL"
               },
               "steps":{
                  "type":"nested",
                  "properties":{
                     "time":{
                        "type":"keyword"
                     },
                     "products":{
                        "type":"nested",
                        "properties":{
                           "name":{
                              "type":"text"
                           },
                           "link":{
                              "type":"keyword",
                              "null_value":"NULL"
                           },
                           "type":{
                              "type":"keyword",
                              "null_value":"NULL"
                           },
                           "ingredients":{
                              "type":"keyword",
                              "null_value":"NULL"
                           },
                           "flags":{
                              "type":"keyword",
                              "null_value":"NULL"
                           }
                        }
                     }
                  }
               }
            }
         }
      }
   }
}'

现在,我正在尝试搜索两个字段data.titledata.steps.products.name使用以下查询:

curl -X GET "localhost:9200/data/_search?pretty" -H 'Content-Type: application/json' -d'
{
  "query": {
    "bool": {
      "should": [
        {
          "nested": {
            "path": "data",
            "query": {
              "nested": {
                "path": "steps",
                "query": {
                  "nested": {
                    "path": "products",
                    "query": {
                      "multi_match": {
                        "query": "XXX",
                        "fields": [
                          "name"
                        ]
                      }
                    }
                  }
                }
              }
            }
          }
        },
        {"multi_match": {
                "query": "XXX",
                "fields": [
                  "data.title"
                ]
              }
            }
      ]
    }
  }
}'

它抛出错误,它未能在步骤下找到路径:

{
  "error" : {
    "root_cause" : [
      {
        "type" : "query_shard_exception",
        "reason" : "failed to create query: [nested] failed to find nested object under path [steps]",
        "index_uuid" : "SjQgt4BHStC_APsMnZk8BQ",
        "index" : "data"
      }
    ],
    "type" : "search_phase_execution_exception",
    "reason" : "all shards failed",
    "phase" : "query",
    "grouped" : true,
    "failed_shards" : [
      {
        "shard" : 0,
        "index" : "data",
        "node" : "L1azdi09QxanYGnP-y0xbQ",
        "reason" : {
          "type" : "query_shard_exception",
          "reason" : "failed to create query: [nested] failed to find nested object under path [steps]",
          "index_uuid" : "SjQgt4BHStC_APsMnZk8BQ",
          "index" : "data",
          "caused_by" : {
            "type" : "illegal_state_exception",
            "reason" : "[nested] failed to find nested object under path [steps]"
          }
        }
      }
    ]
  },
  "status" : 400
}

你能帮忙找出映射/查询的错误吗?

更新:

我的 json 数据:https ://jsonkeeper.com/b/PSVS

标签: elasticsearch

解决方案


查看多级嵌套查询的 Elastic 文档。

您在查询中忘记的是每个子级嵌套查询中嵌套对象的完整路径。(另外,您使用了data映射中不存在的字段,而您想要使用routines

所以你的查询看起来像

curl -X GET "localhost:9200/data/_search?pretty" -H 'Content-Type: application/json' -d'
{
  "query": {
    "bool": {
      "should": [
        {
          "nested": {
            "path": "routines",
            "query": {
              "nested": {
                "path": "routines.steps",
                "query": {
                  "nested": {
                    "path": "routines.steps.products",
                    "query": {
                      "multi_match": {
                        "query": "XXX",
                        "fields": [
                          "routines.steps.products.name"
                        ]
                      }
                    }
                  }
                }
              }
            }
          }
        },
        {"multi_match": {
                "query": "XXX",
                "fields": [
                  "routines.title"
                ]
              }
            }
      ]
    }
  }
}'

无论如何,请重新考虑首先拥有多级嵌套字段是否是一个好主意,因为它们对性能有重大影响。例如,有一个索引routines,也许有一个data标识符,有意义吗?

编辑:将完整路径添加到第一个应该块的多匹配字段


推荐阅读