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

请教下,聚合中 如何统计 buckets下一共有多少个key?

Elasticsearch | 作者 lzyue | 发布于2016年09月26日 | 阅读数:11323

如下面这个,下面是我测试的数据量,比如我统计一个时间范围内不重复的stbid个数
数据量小时可以看到   buckets 下有两类 key  ,但是一旦数据量大了,我如何统计buckets下有多少key呢?
 
相当于  select count(stbid)  from table where collecttime>'2016-09-26 15:00:00' and collecttime<'2016-09-26 17:00:00' ;
望高人指教
 {
"took": 2,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 10,
"max_score": 0,
"hits": [ ]
},
"aggregations": {
"aggInfo": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "009903FF00473340000090D8F35D4FE2",
"doc_count": 5
}
,
{
"key": "2710029900701590000030F31D96B797",
"doc_count": 5

}
]
}
}
}
已邀请:

martindu - 搜披露创始人

赞同来自: lzyue leighton_buaa

试试cardinality哈,如果你只关心分类的数量

leighton_buaa

赞同来自:

默认返回10个,你可以设置
 
{
  "aggs": {
    "your_agg_name": {
      "terms": {
        "field": "field_name",
        "size": 10
      }
    }
  }
}

lzyue

赞同来自:

我查询的报文是这样  {
  "query": {
    "range": {
      "currenttime": {
        "from": "2016-09-26 14:25:00",
        "to": "2016-09-26 14:30:00",
        "include_lower": true,
        "include_upper": false
      }
    }
  },
  "size": 0,
  "aggregations": {
    "aggInfo": {
      "terms": {
        "field": "stbid",
        "size": 10
      }
    }
  }
这样查询的结果是 下面那些,其实我想要的统计结果是  2 也就是 buckets 下有两个 key
现在我不知道怎么得到这个 2 数据量小时我可以一个一个看,十万,百万数量量时我没办法一个一个数,所以有没有一种count的方法把这个 2 统计出来呢
 
{
"took": 50,
"timed_out": false,
"_shards": {
"total": 1039,
"successful": 1039,
"failed": 0
},
"hits": {
"total": 80,
"max_score": 0,
"hits": [ ]
},
"aggregations": {
"aggInfo": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "009903FF00473340000090D8F35D4FE2",
"doc_count": 40
}
,
{
"key": "2710029900701590000030F31D96B797",
"doc_count": 40

}
]
}
}
}

leighton_buaa

赞同来自:

我觉得只能通过解析json获得吧,还请其他大牛现身解答一下

lzyue

赞同来自:

估计真正不行的话,简单的做法就是将 curl 命令的结果写入一文件,最后grep -o key 在文件中出现的次数了

要回复问题请先登录注册