在单个elasticsearch查询中从父级和子级获取多个字段数据

Kau*_*sty 6 join parent-child elasticsearch

是否可以在单个elasticsearch查询中从父级和子级获取字段数据?基本上我试图通过过滤在一次迭代中捕获多个父字段和多个子字段的数据.我尝试了将信息绑定到单个查询中的各种方法,但却无法找到一种方法.这是我的映射的样子: -

父:

_id_parent : values {1}
_source: {_date (20160316), _time (20160316010000), _id_source (test), _year (2016), _month (1)}
Run Code Online (Sandbox Code Playgroud)

儿童:

_id_child : values {1}
_source: {_id_child (1), _id_parent (1), _child_question (q1), _child_answer (This needs to be done.)}
Run Code Online (Sandbox Code Playgroud)

预期产出(类似于下文):

    (PARENT)
      "hits" : {
        "total" : 1,
        "max_score" : 1.0,
        "hits" : [ {
          "_index" : "index",
          "_type" : "parent",
          "_id" : "1",
          "_score" : 1.0,
          "_source":{_id_parent":"1","_id_source":"test","_date":"20160316","_time":"20160316010000","_year":2016,"_month":"1"}
        } ]
      }
        (CHILD)
          "hits" : {
            "total" : 1,
            "max_score" : 1.0,
            "hits" : [ {
              "_index" : "index",
              "_type" : "child",
              "_id" : "1",
              "_score" : 1.0,
              "_source":{"_id_child":"1", "_child_question":"q1","_child_answer":"This needs to be done."}
            } ]
          }
Run Code Online (Sandbox Code Playgroud)

链接:

http://rore.im/posts/elasticsearch-joins/

https://github.com/elastic/elasticsearch/issues/761

https://www.elastic.co/guide/en/elasticsearch/guide/current/children-agg.html

    curl -XGET "$ELASTICSEARCH_ENDPOINT/index/parent/_search?pretty=true" -d "
    {
        "query": {
            "match": {
                "_id_parent": "1"
                }
            },
            "size" : 10,
            "aggs": {
                "_id_parent": {
                    "terms": {
                        "field":"_id_parent",
                        "field":"_id_source",
                        "field":"_date",
                        "field":"_time",
                        "field":"_year",
                        "field":"_month",
                        },
                    "aggs": {
                        "child": {
                            "children": {
                                "type": "child"
                                },
                            "aggs": {
                                "child": {
                                    "terms": {
                                        "field": "child._id_child",
                                        "field": "child._child_question",
                                        "field": "child._child_answer",
                                }
                            }
                        }
                    }
                }
            }
        }
    }"
Run Code Online (Sandbox Code Playgroud)

Fre*_*ung 9

这听起来像内心命中的工作.此功能允许您获得创建has_childhas_parent匹配的匹配.

在你的情况下你要对父进行查询,有一个简单的has_child(即match_all),反之亦然,例如像

{
    "query" : {
        "has_child" : {
            "type" : "child",
            "query" : {
                "match_all": {}
            },
            "inner_hits" : {} 
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


abh*_*tia 3

我做了一些假设...让我知道它们是否正确

  1. 您需要来自父映射的文档,其文档 id = 1
  2. 您还需要所有具有parent_id = 1的子文档
  3. 您正在将此parent_id从代码传递到elasticsearch。
  4. 您并不是要求 Elasticsearch 过滤多个父文档。在使用 elasticsearch 之前,您已经知道您想要 id = 1 的父文档

如果是这种情况,您可以创建两个单独的查询

第一个查询是“获取 id = 1 的父文档”
第二个查询是“获取parent_id = 1 的所有子文档”

您可以使用 Elasticsearch 的“多搜索 API”在一次网络调用中将这两个请求发送到 Elasticsearch

多重搜索API