我刚刚开始使用 Hibernate Search。我用来进行搜索的代码取自参考指南:
FullTextEntityManager fullTextEntityManager =
Search.getFullTextEntityManager(em);
EntityTransaction transaction = em.getTransaction();
try
{
transaction.begin();
// create native Lucene query using the query DSL
// alternatively you can write the Lucene query using the
// Lucene query parser or the Lucene programmatic API.
// The Hibernate Search DSL is recommended though
SearchFactory sf = fullTextEntityManager.getSearchFactory();
QueryBuilder qb = sf
.buildQueryBuilder().forEntity(Item.class).get();
org.apache.lucene.search.Query query = qb
.keyword()
.onFields("title", "description")
.matching(queryString)
.createQuery();
// wrap Lucene query in a javax.persistence.Query
javax.persistence.Query persistenceQuery =
fullTextEntityManager.createFullTextQuery(query, Item.class);
// execute search
@SuppressWarnings("unchecked")
List<Item> result = persistenceQuery.getResultList();
transaction.commit();
return result;
}
catch (RuntimeException e)
{
transaction.rollback();
throw e;
}
我注意到查询术语被解释为析取(OR)中的术语。我希望它们被解释为连词。