I have a query method in my ElasticsearchRepository and I've noticed that it sends two requests to my elastic search index. I can pull out the json used for the query in my debugger and I can see that the first query is sent with "size": 0 and "track_total_hits" : 2147483647 and the second query is run with "size": 103, no "track_total_hits". Why is it doing this?
This happens when the repository method is supposed to return multiple objects and when no Pageable
is set on the query (you can do that with a Pageable
parameter of the method for example).
In this case, if no from
or size
parameters are set, Elasticsearch would only return the first 10 documents found for a query.
So this first request is a count request sent to get the number of documents that would be returned. Setting size
to zero will make Elasticsearch to only return the metainformation like the number of found documents, but not the documents themselves; track_total_hits
value makes sure, the number is not capped at 10.000 (Elasticsearch's default limit of data to return).
The second call you see is then the "real" call for data having set the size
so that all documents are returned.