如何在设置 from 和 size 字段后获取 ES 查询的总点击数

How to get Total hits for an ES query after setting from and size field

searchSourceBuilder.query(query).sort(sb).from(fromField).size(sizeField);
searchRequest.indices(elasticsearchUserIndex).source(searchSourceBuilder);
SearchResponse searchResponse = elasticSearchConfig.client().search(searchRequest, RequestOptions.DEFAULT);
SearchHit[] hits = searchResponse.getHits().getHits();
Long totalCount = searchResponse.getHits().getTotalHits();
List<Map<String, Object>> list = new ArrayList<>();
for (SearchHit value: hits) {
    list.add(value.getSourceAsMap());
    }

这就是我正在使用的代码。一旦设置了 from 和 size 参数,我就会根据设置的大小获得匹配。但是要添加总页数作为响应,我需要该查询的总点击数。

尝试使用该线程的解决方案,但我得到的总点击数为 0,尽管我根据大小获得了点击数。 totalCount 返回 0

您需要将track_total_hits设置为true,您将能够获得响应的总点击数。

SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(QueryBuilders.matchAllQuery()).from(0).size(10);
searchSourceBuilder.trackTotalHits(true);
searchRequest.source(searchSourceBuilder);
Long totalCount = searchResponse.getHits().getTotalHits();

将我的 ES 版本更改为 7.3.0。它也必须将我的 rest-high-level-client 更新到 7.3.0。

参考文献:https://discuss.elastic.co/t/issue-with-high-level-rest-client-api/195853