码迷,mamicode.com
首页 > Web开发 > 详细

Hibernate search-搜索

时间:2016-10-24 15:56:16      阅读:330      评论:0      收藏:0      [点我收藏+]

标签:mat   hibernate   print   style   apache   str   close   ssi   ext   

一、精确搜索

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-搜索

标签:mat   hibernate   print   style   apache   str   close   ssi   ext   

原文地址:http://www.cnblogs.com/cz2012/p/5993237.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!