【问题标题】:Hibernate Full text Search PaginationHibernate全文搜索分页
【发布时间】:2014-11-13 09:26:45
【问题描述】:

我正在使用 Hibernate 全文搜索。我目前正在使用:

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-search</artifactId>
    <version>4.5.1.Final</version>
</dependency>

我可以搜索。我的问题是:如何对结果进行分页?有什么方法可以让我先说 50 个结果,然后在请求下一页获取下一个 50 个结果时拨打电话?

我正在考虑的一种方法是简单地获取最大 ID,然后从 Max+1 位置开始下一次搜索,假设 ID 是按自动递增顺序生成的。但我认为必须有更优雅的方法。

【问题讨论】:

    标签: java hibernate jakarta-ee pagination hibernate-search


    【解决方案1】:

    使用 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());
    

    【讨论】:

    • 只是一个注释。 PageImp 的最后一个参数应该是 fullTextQuery.getResultSize()
    【解决方案2】:

    来自分页上的Hibernate search-query docs

    org.hibernate.Query fullTextQuery = 
        fullTextSession.createFullTextQuery( luceneQuery, Customer.class );
    fullTextQuery.setFirstResult(15); //start from the 15th element
    fullTextQuery.setMaxResults(10); //return 10 elements
    

    【讨论】:

      【解决方案3】:

      您可以将列表结果从列表转换为页面示例:如果您在列表用户中进行搜索。你可以这样转换:

       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());
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-02-14
        • 1970-01-01
        • 2012-03-18
        • 2018-11-23
        • 1970-01-01
        • 2021-10-24
        • 1970-01-01
        相关资源
        最近更新 更多