_count 和 _search无条件过滤查询的hits.total.value不一致

_count 和 _search无条件过滤查询的hits.total.value不一致

今天遇到一个奇怪的问题之前也没有注意到,记录一下,如下:

req
POST doc_1/_search
{
  "query": {
    "match_all": {}
  }
}

rep
{
	......"hits" : {
	    "total" : {
	      "value" : 10000,
	      "relation" : "gte"
    	},
	    ......
    }......
}
req
POST doc_1/_count

rep
{
  "count" : 1700000,
  ......
}

在 Elasticsearch 中,_count API 和 _search API 的行为存在一些差异:

  1. _count API 的返回结果

    • POST doc_1/_count 返回 count 值,这个值表示索引中符合条件的实际文档总数。例如,你这里得到的 count 为 1,700,000,表示 doc_1 索引实际包含 1,700,000 条文档。
  2. _search API 的返回结果 (hits.total.value)

    • POST doc_1/_search {"query": {"match_all": {}}} 使用默认的 fromsize 参数,其中默认的 size 值为 10,000。如果没有显式地设置 track_total_hits_search API 默认会限制 hits.total.value 为 10,000,以优化性能。
    • 在这种情况下,Elasticsearch 会返回 hits.total.value10,000,并带有 relation: "gte",意思是文档总数 “大于等于 10,000”。

解决方法

为了获得实际的文档总数,可以将查询请求添加参数 track_total_hits,例如设置为 true

POST doc_1/_search
{
  "query": {
    "match_all": {}
  },
  "track_total_hits": true
}

官方解释

track-total-hits

Generally the total hit count can’t be computed accurately without visiting all matches, which is costly for queries that match lots of documents. The track_total_hits parameter allows you to control how the total number of hits should be tracked. Given that it is often enough to have a lower bound of the number of hits, such as “there are at least 10000 hits”, the default is set to 10,000. This means that requests will count the total hit accurately up to 10,000 hits. It is a good trade off to speed up searches if you don’t need the accurate number of hits after a certain threshold.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值