简体   繁体   English

Elasticsearch Java API - 如何在不检索文档的情况下获取文档数量

[英]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.

相关问题 Elasticsearch-Java RestHighLevelClient-如何使用滚动API获取所有文档 - Elasticsearch - Java RestHighLevelClient - how to get all documents using scroll api Elasticsearch Java中的类似文档 - Elasticsearch similar documents in Java 如何从其他文档(ElasticSearch)中按字段值获取嵌套文档? - How to get nested documents by field value from other documents (ElasticSearch)? 如何在Java中使用Lightcouch API从CouchDB中获取所有文档 - How to get all documents from couchdb using lightcouch api in java ElasticSearch java API 使用 item id 跨索引(使用别名)获取多个文档 - ElasticSearch java API to get multiple documents across indexes (using alias) using item id 如何使用java在elasticsearch SearchQuery中指定“开始”文档? (跳过文件数) - How to specify “start” document in the elasticsearch SearchQuery using java? (skip number of documents) Elasticsearch Java API MoreLike与“ _search”其余端点相比,此文档不返回文档 - Elasticsearch Java API MoreLikeThis not returning documents compared to “_search” rest endpoint 删除所有文档而不删除 elasticsearch java API 中的索引 - deleting all documents with out dropping index in elasticsearch java API 如何使用java API在现有的elasticsearch索引中添加更多文档? - How to add more documents in already existing elasticsearch index using java API? 获取类别的文档数 - Get number of documents of a category
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM