【问题标题】:Get mutiple field data from both parent and child in a single elasticsearch query在单个 elasticsearch 查询中从父级和子级获取多个字段数据
【发布时间】:2016-07-05 19:42:15
【问题描述】:

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

父母:

_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.)}

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

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

【问题讨论】:

  • 这对你有用吗?

标签: join elasticsearch parent-child


【解决方案1】:

这听起来像是inner hits 的工作。此功能可让您获得与has_childhas_parent 匹配的点击。

在您的情况下,您可以使用简单的has_child(即 match_all)对父级进行查询,反之亦然,例如

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

【讨论】:

  • 如何在同一查询中对父属性添加过滤器?
  • 您能否在过滤子项之前过滤特定字段上的父记录?
  • 这应该只是一个“普通”过滤器/查询,即创建一个布尔查询,其中包含 has_child 和(例如)父项查询/过滤器
【解决方案2】:

我做了一些假设...如果它们正确,请告诉我

  1. 您需要来自父映射的文档 ID = 1 的文档
  2. 您还需要所有 parent_id = 1 的子文档
  3. 您正在将此 parent_id 从您的代码传递给 elasticsearch。
  4. 您没有要求 elasticsearch 过滤多个父文档。在你点击 elasticsearch 之前,你已经知道你想要 id = 1 的父文档

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

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

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

MultiSearch API

【讨论】:

    【解决方案3】:

    您可以使用以下示例从父索引和子索引中进行搜索。希望这会对您有所帮助。

        GET indexname/_search
        {
          "query": {
          "bool": {
          "must": [
            {
              "bool": {
                "must": [
                  {
                    "term": {
                      "parentField": {
                        "value": "valuetosearch"
                      }
                    }
                  }
                ]
              }
            },
            {
              "has_child": {
                "type": "childindex",
                "query": {
                   "range" : {
                      "childindexField" : {
                          "lte": "value"
                      }
                   }
                }
              }
            }
          ]
          }
        }
       }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-02-18
      • 2020-02-26
      • 1970-01-01
      • 2020-12-27
      • 1970-01-01
      • 1970-01-01
      • 2020-06-20
      相关资源
      最近更新 更多