Sub-aggregate 一个 multi-level 嵌套复合聚合

Sub-aggregate a multi-level nested composite aggregation

我正在尝试设置一个搜索查询,该搜索查询应通过 multi-level 嵌套字段复合聚合 collection,并根据此 [=31= 提供一些 sub-aggregation 指标].我能够按预期获取带有桶的复合聚合,但 sub-aggregation 指标随所有桶的 0 一起提供。我不确定我是否没有正确指出 sub-aggregation 应该考虑哪些字段,或者它是否应该放在查询的不同部分。

我的 collection 看起来类似于以下内容:

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

您可以在下面找到我到目前为止尝试过的内容。所有存储桶都带有 clicks 总和 0,即使所有文档都有一个集合 clicks 值。

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

我目前的响应正文:

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

为了使其工作,composite 聚合 would need to 中的所有来源都在相同的 nested 上下文中。

我刚才回答过 something similar。提问者需要将嵌套值放在顶层。您面临相反的挑战——假设 stats.clicks 字段 在顶层,您需要在 categories.tags 的每个条目中复制它,我怀疑,这不可行,因为您可能不时更新这些统计数据……

如果您可以跳过 composite 方法并在没有它的情况下使用 terms agg,则可以通过 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"
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

这会很好用,但是 won't offer pagination


澄清

如果您需要 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"
                          }
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}