When using Spring Data for Apache Solr, I have noticed that repository queries run a count query against Solr before they run the actual query if the query does not use a Pageable or a Limit parameter.
First, the query is run using "rows=0", then a second query is run using "rows=x", where x is the hits value from the count query.
Why is the query not simply run once without any "rows" parameter? What is the benefit of running two queries?
The Javadoc of the inner class AbstractSolrQuery.CollectionExecution says "If not pageable argument is set count operation will be executed to determine total number of entities to be fetched", but it doesn't explain why this is necessary or useful.
Any ideas?
An easy workaround is to just use a very high limit instead of no limit, then only one query is run.
The corresponding code can be seen in AbstractSolrQuery.CollectionExecution.execute(). It calls count(query), which does the additional call.
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public Object execute(Query query) {
if (!isLimiting()) {
query.setPageRequest(pageable.isPaged() ? pageable : new SolrPageRequest(0, (int) count(query)));
return executeFind(query).getContent();
}
...
}