Hibernate Full text Search Pagination
Asked Answered
J

3

5

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.

Jillene answered 18/9, 2014 at 21:40 Comment(0)
B
10

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
Bridgettbridgette answered 18/9, 2014 at 21:47 Comment(0)
D
0

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());
Diarist answered 23/10, 2017 at 22:25 Comment(0)
H
0

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());
Heel answered 1/4, 2020 at 23:45 Comment(1)
Just a note. Last parameter to PageImp should be fullTextQuery.getResultSize()Largehearted

© 2022 - 2024 — McMap. All rights reserved.