Elasticsearch:path [messages]下的嵌套对象不是嵌套类型

Dan*_*kle 3 search-engine elasticsearch

我在Elasticsearch中遇到嵌套类型映射的很多问题,我已经运行它来创建我的索引:

curl -XPOST 'http://localhost:9200/thread_and_messages' -d 
'{"mappings" : {
    "message": {
        "properties": {
            "messages": {
                "type": "nested", 
                "include_in_parent": true, 
                "properties": {
                    "message_id": {"type": "string"}, 
                    "message_text": {"type": "string"}, 
                    "message_nick": {"type": "string"}
                }
            }
        }
    }
}}'
Run Code Online (Sandbox Code Playgroud)

那么这就是我为文档编制索引的方式:

curl -XPUT 'http://localhost:9200/thread_and_messages/thread/1' -d 
'{
    "thread_id":"2", 
    "thread_name":"Windows", 
    "created":"Wed Mar 25 2015", 
    "first_nick":"Admin", 
    "messages":[
        {"message_id":"5", "message_text":" Pc with a mouse", "message_nick":"Admin"},
        {"message_id":"6", "message_text":"Keyboard", "message_nick":"Admin"},
        {"message_id":"7", "message_text":"iPhone", "message_nick":"Admin"},
        {"message_id":"8", "message_text":"Gym", "message_nick":"Admin"}]"
}'
Run Code Online (Sandbox Code Playgroud)

这是我的查询:

curl -XGET 'http://localhost:9200/thread_and_messages/thread/_search' -d 
'{"query": {
    "bool": {
        "must": [
            {"match": {"thread_name": "windows"}}, 
            {"nested": {
                "path": "messages", "query": {
                "bool": {
                    "must": [{
                        "match": {"messages.message_text": "gym"}
                    }]
                }
                }
            }}
        ]}
    }
}'
Run Code Online (Sandbox Code Playgroud)

我收到此错误,即使我已将消息明确映射为嵌套类型:

QueryParsingException[[thread_and_messages] [nested] nested object under path [messages] is not of nested type
Run Code Online (Sandbox Code Playgroud)

Slo*_*ens 5

这是因为您为类型定义了映射"message",但随后将文档编入索引作为类型"thread".因此,"thread"类型是动态创建的,但不是嵌套的子类型.我按照发布的方式运行您的代码,然后查看映射:

GET /test_index/_mapping
...
{
   "test_index": {
      "mappings": {
         "message": {
            "properties": {
               "messages": {
                  "type": "nested",
                  "include_in_parent": true,
                  "properties": {
                     "message_id": {
                        "type": "string"
                     },
                     "message_nick": {
                        "type": "string"
                     },
                     "message_text": {
                        "type": "string"
                     }
                  }
               }
            }
         },
         "thread": {
            "properties": {
               "created": {
                  "type": "string"
               },
               "first_nick": {
                  "type": "string"
               },
               "messages": {
                  "properties": {
                     "message_id": {
                        "type": "string"
                     },
                     "message_nick": {
                        "type": "string"
                     },
                     "message_text": {
                        "type": "string"
                     }
                  }
               },
               "thread_id": {
                  "type": "string"
               },
               "thread_name": {
                  "type": "string"
               }
            }
         }
      }
   }
}
Run Code Online (Sandbox Code Playgroud)

当我改变"message""thread"的映射,并开始了,您的查询工作的罚款.

这是我用来测试它的代码:

http://sense.qbox.io/gist/8a06b7849cf49006afd464ed4ee5b4e770759d5a