首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

集成Hibernate Search作全文检索

2013-09-25 
集成Hibernate Search做全文检索版本及依赖:package othertestimport java.util.Iteratorimport java.ut

集成Hibernate Search做全文检索

版本及依赖:

package othertest;import java.util.Iterator;import java.util.List;import javacommon.gather.bean.Pageinfo;import org.apache.lucene.analysis.standard.StandardAnalyzer;import org.apache.lucene.queryParser.ParseException;import org.apache.lucene.queryParser.QueryParser;import org.apache.lucene.search.Query;import org.apache.lucene.util.Version;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.Transaction;import org.hibernate.search.FullTextQuery;import org.hibernate.search.FullTextSession;import org.hibernate.search.Search;import org.junit.Before;import org.junit.BeforeClass;import org.junit.Test;public class SearchTest {private static SessionFactory sf;@BeforeClasspublic static void init() {sf = HibernateConfigTest.sf;//弄一个SessionFactory,不多说}@Before//执行索引public void index(){Session session = sf.openSession();FullTextSession fullTextSession = Search.getFullTextSession(session);//查出结果List<Pageinfo> pageinfos = session.createCriteria(Pageinfo.class).list();session.beginTransaction();//依次建立索引for (Iterator iterator = pageinfos.iterator(); iterator.hasNext();) {Pageinfo pageinfo = (Pageinfo) iterator.next();fullTextSession.index(pageinfo);}session.getTransaction().commit();session.close();System.out.println("index over......");}@Testpublic void searchTest() {Session session = sf.openSession();FullTextSession fullTextSession = Search.getFullTextSession(session);//在字段content中检索QueryParser queryParser = new QueryParser(Version.LUCENE_36, "content", new SmartChineseAnalyzer(Version.LUCENE_36));Query luceneqQuery=null;try {//检索含有“大风”的信息luceneqQuery = queryParser.parse("大风");} catch (ParseException e) {e.printStackTrace();}//执行检索,得到结果集FullTextQuery fullTextQuery = fullTextSession.createFullTextQuery(luceneqQuery, Pageinfo.class);List<Pageinfo> pageinfos = fullTextQuery.list();//查看结果做验证for (Iterator iterator = pageinfos.iterator(); iterator.hasNext();) {Pageinfo pageinfo = (Pageinfo) iterator.next();System.out.println(pageinfo.getContent());}}}


热点排行