Elasticsearch:路径 [messages] 下的嵌套对象不是嵌套类型
Elasticsearch: nested object under path [messages] is not of nested type
我在 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
这是因为您为类型 "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
我在 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
这是因为您为类型 "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