Elasticsearch聚合度量聚合结果值

时间:2014-06-24 07:17:19

标签: elasticsearch nested aggregation

我想列出一些东西,比如有多少用户拥有不同数量的产品 e.g。

10 users has 5 products
5 users has 4 products
8 users has 3 products

给定映射

{
  "user_id"
  "product_id"
}

但是,对于嵌套聚合函数,我只能找到嵌套聚合可以“分组”每个桶的映射字段,而不是桶的统计信息(例如value_count,sum ... e.t.c) 我能想象的查询逻辑是:(当然它不起作用)

{
  "aggs": {
    "test_aggr" :{
      "terms": { "field": "user_id" },
      "aggs" : {
          "user_own_qty": {
              "value_count" : {"field" : "user_id" },
              "aggs": {
                "group_by_user_own_qty": {
                    "terms": { "field": "user_own_qty.value" },
                    "aggs": {
                        "user_qty_vs_user_own_qty": {
                            "value_count" : { "field" : "user_own_qty.value" 
                        }
                    }
                }
              }
          }
      }
    }
  }
}

SQL(Oracle)似乎可以轻松完成:

with user_product_count as(
select user, count(product) as own_product_count
  from product
  group by user
)
select count(user), own_product count
  from user_product_count
  group by own_product_count

请指教。非常感谢:)

0 个答案:

没有答案