如果需要针对ES索引统计某个字段上出现的不同值的个数时,可以使用cardinality聚合查询完成:
GET /urlAttributes/_search?search_type=count
{
"aggs": {
"uniq_attr": {
"cardinality": {
"field": "domain"
}
}
}
}
注释:如上,是查询索引urlAttributes中domain字段上的不同值个数, "uniq_attr"是用户自己取的bucket名字。
得到的响应如下所示:
{"took": 28,
"timed_out": false,
"_shards": {
"total": 6,
"successful": 6,
"failed": 0
},
"hits": {
"total": 689562,
"max_score": 0,
"hits": []
},
"aggregations": {
"uniq_attr": {
"value": 121618
}
}
}
返回结果表示该字段出现过121618个不同的domain名字。
在Elasticsearch中,要统计某个字段的唯一值数量,可以利用Cardinality聚合查询。例如,查询urlAttributes索引中domain字段的去重个数,通过发送GET请求并设置搜索类型为count,配合聚合查询JSON结构,可以获取到该字段的不同值个数。响应结果将显示具体的不同domain数量。
&spm=1001.2101.3001.5002&articleId=50783523&d=1&t=3&u=27ea7fde0ca940f08e12eadf865b5b45)
7374

被折叠的 条评论
为什么被折叠?



