2
votes

Situation: I use Apache-Lucene Indices with Hibernate Search in my Java Application. For Pagination I want to count all hits of a query on a single index.

Right now I'm evaluating a migration away from Apache-Lucene, to Elasticsearch (ES) as the index backend. Hibernate Search should still provide abstraction, just like with Apache-Lucene.

Question: When using fullTextQuery.getResultSize() it seems like the query is executed and the whole result stored. The size is computed afterwards on the retrieved result. This seems to be the case in the Lucene as well as in the ES implementation.

In ES there is the _count API for this purpose. Can this API be used via Hibernate Search? One Problem when retrieving all the documents in the index is the window result size restriction of ES. I expect the performance to improve as well when using the _count API.

Is there a count function for Apache-Lucene via Hibernate Search?

Thank you.

1

1 Answers

1
votes

In Hibernate Search 5, you have to set the max result size to 0, so that Hibernate Search doesn't load anything:

FullTextQuery fullTextQuery = session.createFullTextQuery( luceneQuery, ScientificArticle.class );
fullTextQuery.setMaxResults(0);
int count = fullTextQuery.getResultSize();

setMaxResults() doesn't affect the value returned by getResultSize().

This behavior is a bit strange, granted, but will have to stay that way in Search 5 for historical reasons related to how results are cached. In Hibernate Search 6, we will change the behavior and you won't have to worry about calling setMaxResults() just to retrieve the result size.