Ask Questions and Find Answers
Important:
Ask is now read-only. You can review any existing questions and answers, but not add anything new.
But - don't panic! While ask is no more, we've replaced it with discuss - the new Liferay Discussion Forum! Read more here here or just visit the site here:
discuss.liferay.com
RE: How can I get count of Indexer search?
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?
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?
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.
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.
Copyright © 2025 Liferay, Inc
• Privacy Policy
Powered by Liferay™