引用官方文档中提到的建立亲子关系的例子-https://www.elastic.co/guide/en/elasticsearch/reference/current/parent-join.html
该链接提供了问答连接关系,其中问题是父类型,答案是子类型。
如果您需要在一个查询中搜索匹配某些文本的所有 parent 以及他们的 child 匹配某些特定于 child 的文本,您会怎么做?
假设他们在 json 文档中有键值对,如下所示,我们搜索与问题中的文本匹配的父项和与 answer-text 中的值匹配的子项。
Parent --> question --> "question-text" : "Geography: Where is Mt. Everest?"
Child --> answer --> "answer-text" : "Nepal?"
我们不希望结果中只包含父项或子项,而是所有父项及其与查询匹配的关联子项。
我知道内部命中是一种选择,但无法弄清楚用法 - https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-inner-hits.html
我希望问题很清楚,如果需要可以添加更多细节。
我还尝试了此处提到的父子内部命中示例:
例子:
POST test_index/_search
{
"query": {
"has_child": {
"type": "child",
"query": {
"match": {
"number": 2
}
},
"inner_hits": {}
}
}
}
这给了我所有 parent 的匹配 child 。如何向父级添加过滤器并仅查询同一查询中的特定父级(以及子级过滤器)?是否可以在同一查询中的特定字段上过滤父记录?
像这样的东西——
POST test_index/_search
{
"query": {
<match parents first on number:1 and then match the children below>
"has_child": {
"type": "child",
"query": {
"match": {
"number": 2
}
},
"inner_hits": {}
}
}
}
最佳答案
这对我有用。唯一的缺点是 kibana Discover 仪表板不支持 has_child 或 has_parent,因此我们无法构建可视化。
POST test_index/_search
{
"query": {
"bool": {
"must": [{
"has_child": {
"type": "childname",
"query": {
"match": {
"properties.name": "hello"
}
},
"inner_hits": {}
}
},
{
"match": {
"account.name": "random",
"version": {"2.0"}
}
},{
"match": {
"account.name": "random",
"version": {"2.0"}
}
]
}
}
}
关于elasticsearch - 在 Elasticsearch 中的一个查询中搜索父级及其所有子级,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49147218/