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

标签 join elasticsearch parent-child

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

parent :

_id_parent : values {1}
_source: {_date (20160316), _time (20160316010000), _id_source (test), _year (2016), _month (1)}

child :

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

预期输出(类似于下面的内容):

    (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."}
            } ]
          }

链接:

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",
                                }
                            }
                        }
                    }
                }
            }
        }
    }"

最佳答案

这听起来像是 inner hits 的工作.此功能允许您获得匹配 has_childhas_parent 的命中。

在您的情况下,您将使用一个普通的 has_child(即 match_all)或反之亦然,例如像这样的东西对父级进行查询

{
    "query" : {
        "has_child" : {
            "type" : "child",
            "query" : {
                "match_all": {}
            },
            "inner_hits" : {} 
        }
    }
}

关于join - 在单个 elasticsearch 查询中从父项和子项获取多个字段数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36094770/

相关文章:

C - 终止的好方法

javascript - 每次在 jQuery 中单击按钮(子)时,如何删除选定的 <tr>(父)div?

在 Google bigquery 的 On 子句中使用 <= 进行连接

elasticsearch - 查询具有多个数值范围的elasticsearch

php - 无法在 Zend 中的分支文件夹之间执行 JOIN

elasticsearch - 在 PCF 抛出错误中创建的 Syslog Drain

elasticsearch - 不用curl直接删除所有elasticsearch索引

mysql - 如何使用父表中的值将连接表连接到另一个连接表?

sql - 对多个表进行联接和逆透视

Azure Log Analytics - 加入失败 - 数据类型不一致