Elasticsearch-rest-high-level-client SearchRequest returns partial result

I have some java code as follows:

    SearchRequest searchRequest = new SearchRequest(index);
    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
    searchSourceBuilder.query(QueryBuilders.matchAllQuery());
    searchRequest.source(searchSourceBuilder);
    SearchResponse response = client.search(searchRequest,RequestOptions.DEFAULT);

    var hits = response.getHits();
    for(var hit : hits){
        log.info(hit.toString());
    }

Expected number of docs is 36, Total hits is 36, but the list has only 10 actual docs as shown in the screenshot:
40

Is there some other setting required to fetch all 36?

Hi @Avinash_D_Silva,

by default a search returns only the top 10 hits. You can set that by something like:

searchSourceBuilder.size(20)

However, notice that if you want all results out of a search, you may want to look at scroll search, which allows you to scroll through a large search result.

See also:
https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-from-size.html
https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-scroll.html

1 Like

Thank you @HenningAndersen
searchSourceBuilder.size(20) did the trick!

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.