小编典典

Elasticsearch:路径下的嵌套对象不是嵌套类型

elasticsearch

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

{
  "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);

我认为我可能对文档编制了错误的索引。


阅读 549

收藏
2020-06-22

共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开始)。

2020-06-22