【问题标题】:Elastic Search - Aggregate over several child doc types弹性搜索 - 聚合多个子文档类型
【发布时间】:2016-01-14 16:52:24
【问题描述】:

在弹性搜索中,我有父子关系。孩子可以是 5 种不同的文档类型,但都有几个共同的字段。我正在寻找一种方法来搜索父文档,然后在一个查询中从每个子文档类型中聚合子文档。目前这需要我 5 次查询。

【问题讨论】:

  • 你能发布一个(最好是简化的)例子吗?

标签: elasticsearch parent-child aggregation


【解决方案1】:

您可以查询父文档,然后使用以下单个查询从每个子文档类型聚合子文档:

POST /index_name/_search            --> leave the type_name blank
{
   "size": 0,
   "query": {
      "has_parent": {
         "type": "parentType",      --> parent doc type
         "query": {
            "match_all": {}         --> search criteria for parent
         }
      }
   },
   "aggs": {
      "agg_by_type": {
         "terms": {
            "size": 0,
            "field": "_type"              --> aggregate by child doc type
         },
         "aggs": {
            "agg_by_field": {
               "terms": {
                  "field": "fieldName"    --> aggregate by required field
               }
            }
         }
      }
   }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-08-25
    • 2017-11-26
    • 1970-01-01
    • 1970-01-01
    • 2015-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多