RE: How can I get count of Indexer search?

Jim Miller, modified 12 Years ago. New Member Posts: 10 Join Date: 9/20/12 Recent Posts
I have an Indexer search using a SearchContext to do a custom search of the Document Library.

Indexer indexer = IndexerRegistryUtil.getIndexer(DLFileEntry.class);

SearchContext searchContext = SearchContextFactory.getInstance(request);

searchContext.setEnd(searchContainer.getEnd());
searchContext.setKeywords(keywords);
searchContext.setStart(searchContainer.getStart());

Hits results = indexer.search(searchContext);

int total = results.getLength();


The problem is that results.getLength() or even results.toList().length return a value based on the number of rows displayed in the search container. For pagination, the search container needs a value for the total number of rows so that it can display something like 'Showing 1-10 of 42 results'. Without setting searchContext.setEnd() to some huge number, how do I get a count of all items that match the search?
Jim Miller, modified 12 Years ago. New Member Posts: 10 Join Date: 9/20/12 Recent Posts
To answer my own question, when I put SearchContainer.DEFAULT_DELTA in as the delta parameter of the SearchContainer constructor the results total will be wrong. If I put some number like 10 or 15 as the number of rows I want to initially display, the total documents/hits number is correct.
thumbnail
Enrique Valdes Lacasa, modified 5 Years ago. Junior Member Posts: 92 Join Date: 7/29/14 Recent Posts
In Liferay 7.2, you can do:
searchContext.setStart(page);
searchContext.setEnd(pageSize);
And then in the search response:
SearchHits searchHits = searchResponse.getSearchHits();
long totalHits = searchHits.getTotalHits();
The totalHits gives you the number of ALL results but the searchHits objects returned are only paginated to the page # and size.