[英]Elasticsearch Java API - How to get the number of documents without retrieving the documents
I need to get the number of documents in an index.我需要获取索引中的文档数。 not the documents themselves, but just this "how many" .
不是文件本身,而只是这个“多少”。
What's the best way to do that?这样做的最佳方法是什么?
There is https://www.elastic.co/guide/en/elasticsearch/reference/current/search-count.html .有https://www.elastic.co/guide/en/elasticsearch/reference/current/search-count.html 。 but I'm looking to do this in Java.
但我希望在 Java 中做到这一点。
There also is https://www.elastic.co/guide/en/elasticsearch/client/java-api/2.4/count.html , but it seems way old.还有https://www.elastic.co/guide/en/elasticsearch/client/java-api/2.4/count.html ,但它似乎很旧。
I can get all the documents in the given index and come up with "how many".我可以获取给定索引中的所有文档并得出“有多少”。 But there must be a better way.
但必须有更好的方法。
Use the search API, but set it to return no documents and retrieve the count of hits from the SearchResponse object it returns.使用搜索 API,但将其设置为不返回任何文档并从它返回的 SearchResponse 对象中检索命中数。
For example:例如:
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.index.query.QueryBuilders.*;
SearchResponse response = client.prepareSearch("your_index_goes_here")
.setTypes("YourTypeGoesHere")
.setQuery(QueryBuilders.termQuery("some_field", "some_value"))
.setSize(0) // Don't return any documents, we don't need them.
.get();
SearchHits hits = response.getHits();
long hitsCount = hits.getTotalHits();
Elastic - Indices Stats 弹性 - 指数统计
Indices level stats provide statistics on different operations happening on an index.
索引级别的统计信息提供了在索引上发生的不同操作的统计信息。 The API provides statistics on the index level scope (though most stats can also be retrieved using node level scope).
API 提供有关索引级别范围的统计信息(尽管也可以使用节点级别范围检索大多数统计信息)。
prepareStats(indexName) client.admin().indices().prepareStats(indexName).get().getTotal().getDocs().getCount();
prepareStats(indexName)
client.admin().indices().prepareStats(indexName).get().getTotal().getDocs().getCount();
Breaking changes after 7.0; 7.0 之后的重大变化; you need to set track_total_hits to true explicitly in the search request.
您需要在搜索请求中将 track_total_hits 显式设置为 true。
https://www.elastic.co/guide/en/elasticsearch/reference/current/breaking-changes-7.0.html#track-total-hits-10000-default https://www.elastic.co/guide/en/elasticsearch/reference/current/break-changes-7.0.html#track-total-hits-10000-default
Just an addition to @evanjd's answer只是@evanjd 答案的补充
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.index.query.QueryBuilders.*;
SearchResponse response = client.prepareSearch("your_index_goes_here")
.setTypes("YourTypeGoesHere")
.setQuery(QueryBuilders.termQuery("some_field", "some_value"))
.setSize(0) // Don't return any documents, we don't need them.
.get();
SearchHits hits = response.getHits();
long hitsCount = hits.getTotalHits().value;
we need to add .value to get long value of total hits otherwise it will be a string value like "6 hits"我们需要添加 .value 以获得总点击次数的长值,否则它将是一个字符串值,如“6 次点击”
long hitsCount = hits.getTotalHits().value;
long hitsCount = hits.getTotalHits().value; long hitsCount = hits.getTotalHits().value;
我们还可以从 highLevelClient 获取 lowLevelClient 并调用“_count”rest API,如“GET /twitter/_doc/_count?q=user:kimchy”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.