Elastic Search - 聚合多个 child 文档类型

Elastic Search - Aggregate over several child doc types

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

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

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