简体   繁体   English

子聚合一个多级嵌套复合聚合

[英]Sub-aggregate a multi-level nested composite aggregation

I'm trying to set up a search query that should composite aggregate a collection by a multi-level nested field and give me some sub-aggregation metrics from this collection.我正在尝试设置一个搜索查询,该查询应该通过多级嵌套字段组合聚合集合,并从该集合中给我一些子聚合指标。 I was able to fetch the composite aggregation with its buckets as expected but the sub-aggregation metrics come with 0 for all buckets.我能够按预期使用其存储桶获取复合聚合,但子聚合指标对于所有存储桶都为0 I'm not sure if I am failing to correctly point out what fields the sub-aggregation should consider or if it should be placed inside a different part of the query.我不确定我是否未能正确指出子聚合应考虑哪些字段,或者是否应将其放置在查询的不同部分中。

My collection looks similar to the following:我的收藏看起来类似于以下内容:

{
  id: '32ead132eq13w21',
  statistics: {
    clicks: 123,
    views: 456
  },
  categories: [{ //nested type
    name: 'color',
    tags: [{ //nested type
      slug: 'blue'
    },{
      slug: 'red'
    }]
  }]
}

Bellow you can find what I have tried so far.贝娄你可以找到我到目前为止所尝试的。 All buckets come with clicks sum as 0 even though all documents have a set clicks value.即使所有文档都有一个设置的clicks值,所有存储桶的clicks总和都为0

GET /acounts-123321/_search
{
  "size": 0,
  "aggs": {
    "nested_categories": {
     "nested": {
        "path": "categories"
     },
     "aggs": {
           "nested_tags": {
             "nested": {
                "path": "categories.tags"
              },
              "aggs": {
                "group": {
                  "composite": {
                     "size": 100,
                     "sources": [
                       { "slug": { "terms" : { "field": "categories.tags.slug"} }}
                     ]
                   },
                   "aggregations": {
                     "clicks": {
                       "sum": {
                         "field": "statistics.clicks"
                       }
                     }
                   }
                }
              }
            }
          }
       }
  }
}

The response body I have so far:到目前为止我的响应主体:

{
  "took" : 6,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1304,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  },
  "aggregations" : {
    "nested_categories" : {
      "doc_count" : 1486,
      "nested_tags" : {
        "doc_count" : 1486,
        "group" : {
          "buckets" : [
            {
              "key" : {
                "slug" : "red"
              },
              "doc_count" : 268,
              "clicks" : {
                "value" : 0.0
              }
            }, {
              "key" : {
                "slug" : "blue"
              },
              "doc_count" : 122,
              "clicks" : {
                "value" : 0.0
            },
            .....
          ]
        }
      }
    }
  }
}

In order for this to work, all sources in the composite aggregation would need to be under the same nested context.为了使其工作, composite聚合中的所有源都需要位于相同的nested上下文中。

I've answered something similar a while ago.前段时间我回答过类似的问题。 The asker needed to put the nested values onto the top level.提问者需要将嵌套值放在顶层。 You have the opposite challenge -- given that the stats.clicks field is on the top level, you'd need to duplicate it across each entry of the categories.tags which, I suspect, won't be feasible because you're likely updating these stats every now and then…您面临着相反的挑战 - 鉴于stats.clicks字段位于顶层,您需要在categories.tags的每个条目中复制它,我怀疑这是不可行的,因为您可能不时更新这些统计数据……

If you're OK with skipping the composite approach and using the terms agg without it, you could make the summation work by jumping back to the top level thru reverse_nested :如果您可以跳过composite方法并在没有它的情况下使用 agg terms ,您可以通过reverse_nested跳回顶层来进行求和:

{
  "size": 0,
  "aggs": {
    "nested_tags": {
      "nested": {
        "path": "categories.tags"
      },
      "aggs": {
        "by_slug": {
          "terms": {
            "field": "categories.tags.slug",
            "size": 100
          },
          "aggs": {
            "back_to_parent": {
              "reverse_nested": {},
              "aggs": {
                "clicks": {
                  "sum": {
                    "field": "statistics.clicks"
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

This'll work just as fine but won't offer pagination .这可以正常工作,但不会提供 pagination


Clarification澄清

If you needed a color filter, you could do:如果你需要一个color器,你可以这样做:

{
  "size": 0,
  "aggs": {
    "categories_parent": {
      "nested": {
        "path": "categories"
      },
      "aggs": {
        "filtered_by_color": {
          "filter": {
            "term": {
              "categories.name": "color"
            }
          },
          "aggs": {
            "nested_tags": {
              "nested": {
                "path": "categories.tags"
              },
              "aggs": {
                "by_slug": {
                  "terms": {
                    "field": "categories.tags.slug",
                    "size": 100
                  },
                  "aggs": {
                    "back_to_parent": {
                      "reverse_nested": {},
                      "aggs": {
                        "clicks": {
                          "sum": {
                            "field": "statistics.clicks"
                          }
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM