【发布时间】:2015-12-15 15:44:25
【问题描述】:
我有以下 elasticsearch 1.6.2 索引映射:父 item 和子 document。一个项目可以有多个文档。文档不是嵌套的,因为它们包含 base64 数据(mapper-attachments-plugin)并且无法使用项目进行更新。
"mappings" : {
"document" : {
"_parent" : {
"type" : "item"
},
"_routing" : {
"required" : true
},
"properties" : {
"extension" : {
"type" : "string",
"term_vector" : "with_positions_offsets",
"include_in_all" : true
}, ...
},
}
"item" : {
"properties" : {
"prop1" : {
"type" : "string",
"include_in_all" : true
}, ...
}
}
我喜欢在两个索引中搜索,但总是返回items。如果文档中有匹配项,则返回相应的项目。如果项目中有匹配项,则返回该项目。如果两者都为真,则返回该项目。
是否可以结合 has_child 和 has_parent 搜索?
此搜索仅在文档中搜索并返回项目:
{
"query": {
"has_child": {
"type": "document",
"query": {
"query_string":{"query":"her*}
},
"inner_hits" : {
"highlight" : {
"fields" : {
"*" : {}
}
}
}
}
示例
GET index/item/174
{
"_type" : "item",
"_id" : "174",
"_source":{"prop1":"Perjeta construction"}
}
GET index/document/116
{
"_type" : "document",
"_id" : "116",
"_source":{"extension":"pdf","item": {"id":174},"fileName":"construction plan"}
}
__POSSIBLE SEARCH RESULT searching for "constr*"__
{
"hits": {
"total": 1,
"hits": [
{
"_type": "item",
"_id": "174",
"_source": {
"prop1": "Perjeta construction"
},
"highlight": {
"prop1": [
"Perjeta <em>construction<\/em>"
]
},
"inner_hits": {
"document": {
"hits": {
"hits": [
{
"_type": "document",
"_id": "116",
"_source": {
"extension": "pdf",
"item": {
"id": 174
},
"fileName": "construction plan"
},
"highlight": {
"fileName": [
"<em>construction<\/em> plan"
]
}
}
]
}
}
}
}
]
}
}
【问题讨论】:
-
您能否提供一些示例文档以及您希望看到的查询输出?
标签: elasticsearch