使用Elasticsearch计算文档数 [英] Counting number of documents using Elasticsearch
问题描述
如果要计算索引(Elasticsearch的索引)中的文档数,则(至少?)有两种可能性:
If one wants to count the number of documents in an index (of Elasticsearch) then there are (at least?) two possibilities:
-
直接
个计数
Direct
count
POST my_index / _count
POST my_index/_count
应返回 my_index
中的文档数。
使用搜索
Using search
这里可以使用计数
作为 search_type
或其他某种类型。在这两种情况下,都可以从字段 ['hits'] ['total']
Here one can use the count
as the search_type
or some other type. In either of the cases the total count can be extracted from the field ['hits']['total']
我的问题是:
-
不同方法之间有什么区别?我应该选择哪一个
?
what is the difference between the different approaches? Which one should I prefer?
我提出这个问题是因为根据所选择的方法,我遇到了不同的结果
。我现在正在调试
的问题,然后弹出该问题。
I raise this question because I'm experiencing different results depending on the chosen method. I'm now in the process of debugging the issue, and this question popped up.
推荐答案
可能 _count
有点快,因为它不必执行带有排名和结果获取的完整查询,并且只需返回大小即可。
Probably _count
is a bit faster since it doesn't have to execute a full query with ranking and result fetching and can simply return the size.
了解更多有关如何获得不同结果的知识会很有趣。为此,我需要更多信息,例如您要发送的确切查询以及索引上是否有任何索引。
It would be interesting to know a bit more about how you manage to get different results though. For that I need more information like what exact queries you are sending and if any indexing is going on on the index.
但是假设您执行以下操作
But suppose that you do the following
- 为某些文档编制索引
- 刷新索引
_search
和 _count
(全部匹配)将返回相同的总数。如果没有,那将是很奇怪。
_search
and _count
(with a match all query) should return the same total. If not, that'd be very weird.
这篇关于使用Elasticsearch计算文档数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!