【问题标题】:Elasticsearch: nested object under path is not of nested typeElasticsearch:路径下的嵌套对象不是嵌套类型
【发布时间】:2022-03-29 11:07:30
【问题描述】:

我一直在尝试搜索包含嵌套字段的文档。我创建了这样的嵌套映射:

{
  "message": {
    "properties": {
      "messages": {
        "type": "nested",
        "properties": {
          "message_id": { "type": "string" },
          "message_text": { "type": "string" },
          "message_nick": { "type": "string" }
        }
      }
    }
  }
}

我的搜索看起来像这样:

curl -XGET 'localhost:9200/thread_and_messages/thread/_search' \
     -d '{"query": {"bool": {"must": [{"match": {"thread_name": "Banana"}}, {"nested": {"path": "messages", "query": {"bool": {"must": [{"match": {"messages.message_text": "Banana"}}]}}}]}}}}'

但我收到此错误消息:

QueryParsingException[[thread_and_messages] [nested] nested object under path [messages] is not of nested type]

编辑

我仍然收到此错误。我正在通过 Java 执行此操作,因此这是我要创建的文档:

{
  "_id": {
    "path": "3",
    "thread_id": "3",
    "thread_name": "Banana",
    "created": "Wed Mar 25 2015",
    "first_nick": "AdminTech",
    "messages": [
      {
        "message_id": "9",
        "message_text": "Banana",
        "message_nick": "AdminTech"
      }
    ]
  }
}

像这样创建索引:

CreateIndexRequestBuilder indexRequest = client.admin().indices().prepareCreate(INDEX).addMapping("message", mapping);

我认为我可能错误地索引了文档。

【问题讨论】:

    标签: elasticsearch nested


    【解决方案1】:

    TLDR:将"type": "nested", 放入您的嵌套类型中。

    假设我们有一个普通类型,另一个类型嵌套在其中:

    {
       "some_index": {
          "mappings": {
             "normal_type": {
                "properties": {
                   "nested_type": {
                      "type": "nested",
                      "properties": {
                         "address": {
                            "type": "string"
                         },
                         "country": {
                            "type": "string"
                         }
                      }
                   },
                   "first_name": {
                      "type": "string"
                   },
                   "last_name": {
                      "type": "string"
                   }
                }
             }
          }
       }
    }
    

    "type": "nested", 行是嵌套查询工作所必需的,其中将"path": 分配给nested_type,如下所示:

    GET /some_index/normal_type/_search
    {
      "query": {
        "nested": {
          "query": {
            "bool": {}
          },
          "path": "nested_type"
        }
      }
    }
    

    "type": "nested", 行似乎仅在较新的 Elasticsearch 版本中是必需的(从 1.1.1 开始?)。

    【讨论】:

    • 但是 OP 已经有了“type”:“nested”。是不是在错误的地方?我也有同样的问题
    【解决方案2】:

    查询 DSL 中的语法错误。必须阻止query->bool->must的错误关闭

    {
        "query": {
            "bool": {
                    "must": [
    
                }// Should be ] 
            }
        }
    }
    

    正确的版本查询是:

    curl -XGET 'localhost:9200/thread_and_messages/thread/_search' -d '{
       "query": {
          "bool": {
             "must": [
                {
                   "match": {
                      "thread_name": "Banana"
                   }
                },
                {
                   "nested": {
                      "path": "messages",
                      "query": {
                         "bool": {
                            "must": [
                               {
                                  "match": {
                                     "messages.message_text": "Banana"
                                  }
                               }
                            ]
                         }
                      }
                   }
                }
             ]
          }
       }
    }'
    

    【讨论】:

    • 我已经对我的原始帖子进行了编辑,因为我仍然收到错误。
    • 此代码错误。 “路径”字段应该是“消息”,而不是“消息”。
    【解决方案3】:

    如果您的字段是object 类型,您将需要使用<object name>.<object key> 的扁平名称(如果它是普通变量)。例如:

    不是这个

    {
      "nested":
      {
        "path":"album",
        "query":{
          "bool":{
            "boost":5,
            "should":[{"match":{"album.name":"lady"}}]
          }
        }
      }
    },
    

    同意

    {
      "match":{
        "genre":{
          "query":"lady",
          "boost":2
        }
      }
    },
    

    模因形式:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-11-17
      • 1970-01-01
      • 2020-11-21
      • 1970-01-01
      • 1970-01-01
      • 2016-08-03
      • 2021-08-08
      相关资源
      最近更新 更多