Can I get the total results in advance?


#1

Currently I use hibernate search api enabling search pagination as

jpaQuery.setFirstResult((pageNumber - 1) * pageSize).setMaxResults(pageSize).getResultList();

Now I just want to know how many results in total, or how many pages in total in advance without querying for a second time like

  jpaQuery.getResultList();

Because that is not ideal and not necessarily to hit the ES second time.

Any one could help?


#2

You can just call jpaQuery.getResultSize(). If you fetched a page from the same query object (using getResultList(), the result size will be cached and it won’t trigger a new request.

By the way, this method is mentioned in the documentation.


#3

Cool, that means we just hit ES just only once right? Because of cache


#4

Yes. Unless you change some query parameters such as max results/first result, in which case the cache gets cleared, and the next call will retrieve both the results list and the result size.