【问题标题】:Elasticsearch Java API - How to get the number of documents without retrieving the documentsElasticsearch Java API - 如何在不检索文档的情况下获取文档数量
【发布时间】:2021-09-21 12:26:28
【问题描述】:

我需要获取索引中的文档数。不是文件本身,而是这个“多少”。

最好的方法是什么?

https://www.elastic.co/guide/en/elasticsearch/reference/current/search-count.html。但我希望在 Java 中做到这一点。

还有https://www.elastic.co/guide/en/elasticsearch/client/java-api/2.4/count.html,不过好像老了。

我可以获取给定索引中的所有文档并得出“多少”。但一定有更好的办法。

【问题讨论】:

    标签: java elasticsearch


    【解决方案1】:

    使用搜索 API,但将其设置为不返回任何文档并从它返回的 SearchResponse 对象中检索命中数。

    例如:

    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();
    

    【讨论】:

    【解决方案2】:

    只是对@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;
    

    我们需要添加 .value 以获得总命中的长值,否则它将是一个字符串值,如“6 hits”

    long hitsCount = hits.getTotalHits().value;
    

    long hitsCount = hits.getTotalHits().value;

    【讨论】:

      【解决方案3】:

      Elastic - Indices Stats

      索引级别统计信息提供不同操作的统计信息 发生在索引上。 API 提供索引级别的统计信息 范围(尽管大多数统计数据也可以使用节点级别检索 范围)。

      prepareStats(indexName) client.admin().indices().prepareStats(indexName).get().getTotal().getDocs().getCount();

      【讨论】:

      • 这经常给我一个与我预期不同的结果。我有一个删除索引的集成测试,然后重新创建它并加载 20 个文档。使用这种方法,计数总是比我预期的要高,通常是 20 的倍数。
      【解决方案4】:

      7.0 之后的重大更改;您需要在搜索请求中明确将 track_total_hits 设置为 true。

      https://www.elastic.co/guide/en/elasticsearch/reference/current/breaking-changes-7.0.html#track-total-hits-10000-default

      【讨论】:

        【解决方案5】:

        我们还可以从 highLevelClient 获取 lowLevelClient 并调用“_count”rest API,例如“GET /twitter/_doc/_count?q=user:kimchy”。

        【讨论】:

          【解决方案6】:

          2021 解决方案

          我浏览了发布的解决方案,但没有一个令人信服。您可以通过将搜索请求的size 设置为0 来完成工作,但这不是正确的方法。出于计数目的,我们应该使用 count API,因为 count 消耗的资源/带宽更少,并且不需要获取文档、评分和其他内部优化。

          您必须使用 Count API for Java(下面附上的链接)来获取文档的计数。以下代码应该可以完成工作。

          • 使用 QueryBuilder 构建查询

          • 将查询和索引列表传递给 CountRequest() 构造函数

          • 通过client.count(countReq)获取CountResponse()对象

          • 通过执行 countResp.getCount() 提取/返回值

            CountRequest countReq = new CountRequest(indexes, query);

            CountResponse countResp = client.count(countReq, RequestOptions.DEFAULT);

            return countResp.getCount();

          阅读第二个链接了解更多信息。

          重要链接

          计数 API 与搜索 API:Counting number of documents using Elasticsearch

          Count API for Java:https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-high-count.html

          【讨论】:

            猜你喜欢
            • 2023-02-25
            • 1970-01-01
            • 2018-02-13
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2023-04-02
            • 2020-08-01
            相关资源
            最近更新 更多