使用 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:
直接
count
发布 my_index/_count
POST my_index/_count
应该返回my_index
中的文档数.
使用搜索
这里可以使用 count
作为 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屋!