不要急,总有办法的

count api 和 直接设置size = 0 , 来统计条数的区别。

Elasticsearch | 作者 easesstone | 发布于2019年10月31日 | 阅读数:2915

官方提供了count api 来获取匹配的结果, 如下:
GET /twitter/tweet/_count
{
"query" : {
"term" : { "user" : "kimchy" }
}
}
那么,按照下面这种方法,和count 方法有何区别:
 
GET /twitter/tweet/_search
{
"size": 0,
"query" : {
"term" : { "user" : "kimchy" }
}
}
结果都可以得到相应的总的匹配的条数。 
已邀请:

hapjin

赞同来自:

GET /twitter/tweet/_search
{
"size": 0,
"query" : {
"term" : { "user" : "kimchy" }
}
}
你的意思是:这个返回的条数,是通过 响应里面的 totalhits 获得的吧?。totalhits 和 count 还是有一些区别的。。。

kepmoving - 90后

赞同来自:

一个会返回数据的,一个只会返回count,网络 io都会有差别

trycatchfinal

赞同来自:

如果只是文档计数,建议使用count api。
Es 7.x版本,search api为了性能,已经不返回准确的total hit,而是一个估算值。
 
search api
POST kibana_sample_data_logs/_search
{
"size": 0,
"query": {
"match_all": {}
}
}

//response
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 10000, //>10000
"relation" : "gte"
},
"max_score" : null,
"hits" : [ ]
}
}

 

liuxg - Elastic

赞同来自:

上面返回值中有一个"relation" : "gte"表示返回的文档数超过10000个。不是精确的,如果你想得到精确的返回值,你可以这么做:
 
GET _search

{

"track_total_hits": true

}
这样我们就可以返回所有的文档

hapjin

赞同来自:

如果是想统计命中个数的话,参考一下,我以前发的这个帖子:https://elasticsearch.cn/question/8562

要回复问题请先登录注册