一、精确搜索
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);
}