高级休息客户仅获得匹配的大小

时间:2018-11-06 07:02:14

标签: java elasticsearch

我知道运输客户可以这样获得点击数:

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 个答案:

答案 0 :(得分:0)

这是REST客户端的版本:

SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder()
   .query(queryBuilder)
   .size(1);
SearchRequest searchRequest = new SearchRequest("your_index_goes_here");
searchRequest.source(searchSourceBuilder);
SearchResponse response = highLevelClient.search(searchRequest, RequestOptions.DEFAULT);
System.out.println("Total: " + response.getResponse().getHits().totalHits);

答案 1 :(得分:0)

如果使用的是Elasticsearch 6.6或更高版本。也许您想使用Count API:https://www.elastic.co/guide/en/elasticsearch/reference/6.6/search-count.html

在高级休息客户(https://www.elastic.co/guide/en/elasticsearch/client/java-rest/6.6/java-rest-high-count.html)中:

CountRequest countRequest = new CountRequest("your_index_goes_here");
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(QueryBuilders.termQuery("some_field", "some_value"));
countRequest.source(searchSourceBuilder);

CountResponse countResponse = client.count(countRequest, RequestOptions.DEFAULT);
long hitsCount = countResponse.getCount();