Hibernate search-搜索
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Hibernate search-搜索相关的知识,希望对你有一定的参考价值。
一、精确搜索
FullTextSession fullTextSession = Search.getFullTextSession(session);
Transaction tx = fullTextSession.beginTransaction();
QueryBuilder qb = fullTextSession.getSearchFactory()
.buildQueryBuilder().forEntity(Book.class).get();
org.apache.lucene.search.Query query = qb
.keyword()
.onFields("title", "subtitle", "authors.name")
.matching("义 吴承恩")
.createQuery();
org.hibernate.Query hibQuery = fullTextSession.createFullTextQuery(query, Book.class);
// execute search
List<Book> result = hibQuery.list();
tx.commit();
session.close();
for (Book book : result) {
System.out.println(book);
}
二、模糊搜索
FullTextSession fullTextSession = Search.getFullTextSession(session);
Transaction tx = fullTextSession.beginTransaction();
QueryBuilder qb = fullTextSession.getSearchFactory()
.buildQueryBuilder().forEntity(Book.class).get();
org.apache.lucene.search.Query query = qb
.keyword()
.fuzzy().withEditDistanceUpTo(2)
.onFields("title", "subtitle", "authors.name")
.matching("罗本,罗")
.createQuery();
org.hibernate.Query hibQuery = fullTextSession.createFullTextQuery(query, Book.class);
// execute search
List<Book> result = hibQuery.list();
tx.commit();
session.close();
for (Book book : result) {
System.out.println(book);
}
以上是关于Hibernate search-搜索的主要内容,如果未能解决你的问题,请参考以下文章
如何使用Hibernate Search查询DSL匹配多个术语?
“ java.lang.NoClassDefFoundError:javax / persistence / criteria / Selection”在JavaEE Maven项目中使用Hibern