Join 在单个elasticsearch查询中从父项和子项获取多个字段数据
是否可以在单个elasticsearch查询中同时从父级和子级获取字段数据?本质上,我正试图通过过滤在一次迭代中捕获多个父字段和多个子字段的数据。我尝试了各种方法将信息绑定到一个查询中,但没有找到一种方法。以下是我的映射的外观:- 家长:Join 在单个elasticsearch查询中从父项和子项获取多个字段数据,join,elasticsearch,parent-child,Join,elasticsearch,Parent Child,是否可以在单个elasticsearch查询中同时从父级和子级获取字段数据?本质上,我正试图通过过滤在一次迭代中捕获多个父字段和多个子字段的数据。我尝试了各种方法将信息绑定到一个查询中,但没有找到一种方法。以下是我的映射的外观:- 家长: _id_parent : values {1} _source: {_date (20160316), _time (20160316010000), _id_source (test), _year (2016), _month (1)} 儿童: _id_
_id_parent : values {1}
_source: {_date (20160316), _time (20160316010000), _id_source (test), _year (2016), _month (1)}
儿童:
_id_child : values {1}
_source: {_id_child (1), _id_parent (1), _child_question (q1), _child_answer (This needs to be done.)}
预期输出(类似于以下内容):
链接:
这听起来像是一份工作。此功能允许您获得使
具有子项
或具有父项
匹配的点击
在您的情况下,您可以使用一个简单的has\u child
(即match\u all)查询父对象,反之亦然,例如
{
"query" : {
"has_child" : {
"type" : "child",
"query" : {
"match_all": {}
},
"inner_hits" : {}
}
}
}
我做了一些假设。。。让我知道它们是否正确
第一个查询是“获取id为1的父文档”
第二个查询是“获取父项id为1的所有子文档” 您可以使用Elasticsearch的“多搜索API”在一个网络调用中将这两个请求发送到Elasticsearch
您可以使用下面的示例从父索引和子索引中进行搜索。希望这能对你有所帮助
GET indexname/_search
{
"query": {
"bool": {
"must": [
{
"bool": {
"must": [
{
"term": {
"parentField": {
"value": "valuetosearch"
}
}
}
]
}
},
{
"has_child": {
"type": "childindex",
"query": {
"range" : {
"childindexField" : {
"lte": "value"
}
}
}
}
}
]
}
}
}
这对您有用吗?您将如何在同一查询中的父属性上添加筛选器?您能否在筛选其子项之前筛选特定字段上的父记录?这应该只是一个“普通”筛选/查询,即创建一个包含has_子项和(例如)父项上的术语查询/筛选器的布尔查询
{
"query" : {
"has_child" : {
"type" : "child",
"query" : {
"match_all": {}
},
"inner_hits" : {}
}
}
}
GET indexname/_search
{
"query": {
"bool": {
"must": [
{
"bool": {
"must": [
{
"term": {
"parentField": {
"value": "valuetosearch"
}
}
}
]
}
},
{
"has_child": {
"type": "childindex",
"query": {
"range" : {
"childindexField" : {
"lte": "value"
}
}
}
}
}
]
}
}
}