【发布时间】:2020-06-29 21:41:10
【问题描述】:
我正在尝试使用 Hibernate Search 版本 5.5.0.Final 进行全文查询(我已经尝试过使用最新版本,但由于我使用的是旧版本的 Hibernate(5.0 .12) )。
我想得到的最终结果如下:
Display at the top of the list the result that matches on the description field with the following logic:
(Let' assume a user is searching "Milk")
-Results having the word at the beginning (Milk UHT)
-Results having the word in second or third position (Chocolate Milk)
-Results having the word in a phrase(MilkShake)
Then displaying the result matching with the field tags (Lactose free, Gluten Free etc)
这是我到目前为止所做的:
FullTextEntityManager fullTextEntityManager
= Search.getFullTextEntityManager(entityManager);
fullTextEntityManager.createIndexer().startAndWait();
FullTextEntityManager fullTextEntityManager2
= Search.getFullTextEntityManager(entityManager);
QueryBuilder queryBuilder = fullTextEntityManager2.getSearchFactory()
.buildQueryBuilder()
.forEntity(ProductEntity.class)
.get();
Query myQuery = queryBuilder
.bool()
.should(queryBuilder.keyword()
.onField("description").boostedTo(9l).matching(query)
.createQuery())
.should(queryBuilder.phrase()
.onField("description").boostedTo(5l).sentence(query)
.createQuery())
.should(queryBuilder.keyword()
.onField("tags").boostedTo(3l).matching(query)
.createQuery())
.should(queryBuilder.phrase()
.onField("tags").boostedTo(1l).sentence(query)
.createQuery())
.createQuery();
org.hibernate.search.jpa.FullTextQuery jpaQuery
= fullTextEntityManager.createFullTextQuery(myQuery, ProductEntity.class);
return jpaQuery.getResultList();
我已经在互联网上阅读了很多内容,但仍然无法获得想要的结果。 这甚至可能吗?能给我一个提示吗?
提前致谢
【问题讨论】:
标签: hibernate elasticsearch full-text-search hibernate-search