0
我正在創建一個REST API,Angular 2在前端和Spring在後端的Web站點。當我查看請求結果時,有無限滾動。我正在使用全文搜索來查找我的請求的匹配項。我的問題是如何在我的Hibernate搜索中創建分段請求?如果可能,請介紹一些代碼示例。休眠搜索分區請求
的代碼和平,我使用的全文檢索
@Repository
@Transactional
public class HibernateSearch {
@PersistenceContext
private EntityManager entityManager;
public List<Manual> fulltextSearching(String keyword){
if(keyword.equals("")) return Collections.emptyList();
FullTextEntityManager fullTextEntityManager =
org.hibernate.search.jpa.Search.getFullTextEntityManager(entityManager);
QueryBuilder qb = fullTextEntityManager.getSearchFactory()
.buildQueryBuilder().forEntity(Manual.class).get();
javax.persistence.Query jpaQuery =
fullTextEntityManager.createFullTextQuery(createQuery(qb, keyword), Manual.class);
@SuppressWarnings("unchecked")
List<Manual> result = jpaQuery.getResultList();
return result;
}
private org.apache.lucene.search.Query createQuery(QueryBuilder qb, String keyword){
return qb
.keyword()
.onFields("name", "introduction", "user.username", "tags.name")
.matching(keyword)
.createQuery();
}
}
謝謝,它對我很有用 –
要獲得第二頁,你需要創建一個新的'FullTextQuery'還是可以重用'jpaQuery'對象? – nuoritoveri
@nuoritoveri只要您的EntityManager打開,您可以重複使用該查詢。 –