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

Elasticsearch: nested object under path is not of nested type

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

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

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

查询 DSL 中存在语法错误。 must 块 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"
                              }
                           }
                        ]
                     }
                  }
               }
            }
         ]
      }
   }
}'

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

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

不是这个

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

是的

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

以模因形式: