Reputation: 931
I am using Hibernate Fulltext search. I am currently using:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-search</artifactId>
<version>4.5.1.Final</version>
</dependency>
I am able to search all right. The question I have is this: How can I paginate my results? Is there any way I can get say 50 results first and then make calls when next page is requested for the next 50 results?
One way I'm thinking of would be to simply get the max ID and then start next search from Max+1 position, assuming ID's are generated in auto incremental order. But I think there must be more elegant approach.
Upvotes: 5
Views: 5385
Reputation: 334
To paginate with Pageable
FullTextQuery fullTextQuery = getJpaQuery(bool.createQuery(), IconV2.class);
fullTextQuery.setFirstResult(pageable.getPageSize() * pageable.getPageNumber())
.setMaxResults(pageable.getPageSize());
List<IconV2> results = fullTextQuery.getResultList();
List<IconResourceV2> iconResult = results.stream()
.map(iconV2 -> new IconResourceV2.IconResourceV2Builder(iconV2).build())
.collect(Collectors.toList());
return new PageImpl<>(iconResult, pageable, fullTextQuery.getResultSize());
Upvotes: 0
Reputation: 11440
From the Hibernate search-query docs on pagination.
org.hibernate.Query fullTextQuery =
fullTextSession.createFullTextQuery( luceneQuery, Customer.class );
fullTextQuery.setFirstResult(15); //start from the 15th element
fullTextQuery.setMaxResults(10); //return 10 elements
Upvotes: 10
Reputation: 501
You can convert your list result from List to Page example : if your get the search in List users . you can convert like this :
NBPAGE: pages number you want
Pageable page=new PageRequest(0,NBPAGE,Sort.Direction.DESC,"date");
Page<Annonce> PageList= new PageImpl<Annonce>(users ,page,users.size());
Upvotes: 0