Hibernate Search 高亮显示

 一、类库及版本说明

    因为各种版本差异比较大,所以有必要说明下,这篇文章所使用几个核心类库的版本。

    lucene-analyzers-3.0.2.jar、lucene-core-3.0.2.jar、lucene-highlighter-3.0.2.jar、hibernate-search-3.3.0.jar、

    hibernate-search-analyzers-3.3.0.jar、hibernate3.jar、IKAnalyzer3.2.3Stable.jar

 

二、Hibernate.cfg.xml配置

 主要是添加两个属性。

<prop key="hibernate.search.default.directory_provider"> <!-- 存储索引类库的类型 --> org.hibernate.search.store.FSDirectoryProvider </prop> <prop key="hibernate.search.default.indexBase"> <!-- 索引文件的位置 --> D:/apply </prop>  

 

三、需要索引的Bean

 

    @Indexed(index="ApplyCourse") @Analyzer(impl=IKAnalyzer.class) public class ApplyCourse implements java.io.Serializable { //主键 @DocumentId private Integer courseId; //相关的配置,大家可以去看下API,各个版本的差别比较大 @Field(name = "courseName", index = Index.TOKENIZED, store = Store.YES) private String courseName; //...省略getter,setter }

 

四、主要查询方法

public class CourseDaoImpl extends HibernateDaoImpl<ApplyCourse> implements CourseDao { //建立索引 @SuppressWarnings("unchecked") public void createIndex() { this.getHibernateTemplate().execute(new HibernateCallback(){ public Object doInHibernate(Session session) { Criteria criteria = session.createCriteria(ApplyCourse.class); //条件 List<ApplyCourse> courses = criteria.list(); FullTextSession fts = Search.getFullTextSession(session); for(int i = 0; i < courses.size(); i++) { fts.index(courses.get(i)); } System.out.println("建立成功"); return null; } }); } //keyword字段 @SuppressWarnings("unchecked") public PageModel findSearch(final int pageNo, final int pageSize, final String keyword, final String like) { final PageModel pm = new PageModel(); List<ApplyCourse> results = (List<ApplyCourse>)this.getHibernateTemplate().execute(new HibernateCallback(){ public Object doInHibernate(Session session) throws HibernateException, SQLException { List<ApplyCourse> result = null; try { org.apache.lucene.search.Query luceneQuery = IKQueryParser.parse(keyword, like); FullTextSession fts = Search.getFullTextSession(session); FullTextQuery fullTextQuery = fts.createFullTextQuery(luceneQuery, ApplyCourse.class); //分页显示 pm.setTotal(fullTextQuery.getResultSize()); fullTextQuery.setFirstResult((pageNo - 1) * pageSize); fullTextQuery.setMaxResults(pageSize); //开始查询 result = fullTextQuery.list(); //高亮设置 SimpleHTMLFormatter formatter = new SimpleHTMLFormatter( "<b><font color=/"red/">", "</font></b>"); QueryScorer qs = new QueryScorer(luceneQuery); Highlighter highlighter = new Highlighter( formatter, qs); for(int i = 0; i < result.size(); i++) { String findResult = ""; ApplyCourse course = result.get(i); Analyzer analyzer = new IKAnalyzer(); try { findResult = highlighter.getBestFragment(analyzer, keyword, course.getCourseName()); } catch (InvalidTokenOffsetsException e) { e.printStackTrace(); } //重新封装 course.setRemarks(findResult); } } catch (IOException e) { e.printStackTrace(); } return result; } }); pm.setDatas(results); pm.setPageNo(pageNo); pm.setPageSize(pageSize); return pm; } }

   

你可能感兴趣的:(Hibernate,String,session,object,Integer,search)