疲劳是最舒适的枕头,努力工作吧。

按照用户Id分桶后,统计每个id出现的次数之后,如何再按照id出现次数再次分桶,统计某次数范围内包含id的个数

Elasticsearch | 作者 pentakill | 发布于2018年07月31日 | 阅读数:3945

原始数据按照uid分桶以后,获取的数据
uid                            Count
001                           20
002                           10
003                           70
如何得到如下统计数据?
0<=Count<50            2
50<=Count<100        1 
已邀请:

laoyang360 - 《一本书讲透Elasticsearch》作者,Elastic认证工程师 [死磕Elasitcsearch]知识星球地址:http://t.cn/RmwM3N9;微信公众号:铭毅天下; 博客:https://elastic.blog.csdn.net

赞同来自: rochy

POST book_authors/_search
{

"query":{

"terms":{

"authors": ["张三"]

}

},

"size": 0,

"aggs" : {

"authors_agg": {

"terms": {

"field": "authors"

},

"aggs": {

"count_bucket_filter": {

"bucket_selector": {

"buckets_path": {

"cur_doc_cnt": "_count"

},

"script": "params.cur_doc_cnt > 5"

}

}

}

}

}

}

参考举例,bucket_selector试试

kennywu76 - Wood

赞同来自: pentakill

这个聚合需求实际上需要在pipeline aggregation这个层面支持range分桶,但目前的pipeline aggregation没有这个功能。 github上有人提过类似的需求https://github.com/elastic/ela ... 17590 ,看着也不了了之,官方没有计划实现。

pentakill

赞同来自: felixzxk

//目前用这个替代方案//可以获取某个params.cur_doc_cnt > 5的个数

POST book_authors/_search
{

"query":{

"terms":{

"authors": ["张三"]

}

},

"size": 0,

"aggs" : {

"authors_agg": {

"terms": {

"field": "authors"

},

"aggs": {

"count_bucket_filter": {

"bucket_selector": {

"buckets_path": {

"cur_doc_cnt": "_count"

},

"script": "params.cur_doc_cnt > 5"

}

},"count":{
              "cardinality": {
                 "field": "authors"
              }
           }

}

},"total_count":{
          "sum_bucket": {
          "buckets_path": "authors_agg>count.value"
    }
  }

}

}

rochy - rochy_he

赞同来自:

貌似是没有现成的方案,只能自己写代码进行统计。

pentakill

赞同来自:

类似这种功能  
"aggs": {
    "agregation_by_uid": {
      "terms": {
        "field": "uid",
        "size": 500
      }
    },
    "rang_with_count":{
      "range": {
        "field": "_count",//这个字段获取不到
        "ranges": [
          {
            "from": 50,
            "to": 100
          },
             {
            "from": 0,
            "to": 50
          }
        ]
      }
    }
  }

要回复问题请先登录注册