我在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"}
}
}
}
}
}}'
然后,这就是我为文档编制索引的方式:
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"}]"
}'
这是我的疑问:
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"}
}]
}
}
}}
]}
}
}'
我收到此错误,即使我已将消息清楚地映射为嵌套类型:
QueryParsingException[[thread_and_messages] [nested] nested object under path [messages] is not of nested type
答案 0 :(得分: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"
}
}
}
}
}
}
当我在映射中将"message"
更改为"thread"
并重新开始时,您的查询工作正常。
以下是我用来测试它的代码:
http://sense.qbox.io/gist/8a06b7849cf49006afd464ed4ee5b4e770759d5a