Lucene关键词高亮显示

Lucene5 实现搜索引擎功能,高亮代码


package
search.searchengine.cs.swjtu; import java.io.IOException; import java.nio.file.Path; import java.nio.file.Paths; import java.util.ArrayList; import java.util.List; import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.analysis.TokenStream; import org.apache.lucene.analysis.standard.StandardAnalyzer; import org.apache.lucene.document.Document; import org.apache.lucene.index.DirectoryReader; import org.apache.lucene.index.IndexReader; import org.apache.lucene.queryparser.classic.ParseException; import org.apache.lucene.queryparser.classic.QueryParser; import org.apache.lucene.search.IndexSearcher; import org.apache.lucene.search.ScoreDoc; import org.apache.lucene.search.TopScoreDocCollector; import org.apache.lucene.search.highlight.Formatter; import org.apache.lucene.search.highlight.Fragmenter; import org.apache.lucene.search.highlight.Highlighter; import org.apache.lucene.search.highlight.InvalidTokenOffsetsException; import org.apache.lucene.search.highlight.QueryScorer; import org.apache.lucene.search.highlight.SimpleFragmenter; import org.apache.lucene.search.highlight.SimpleHTMLFormatter; import org.apache.lucene.search.highlight.SimpleSpanFragmenter; import org.apache.lucene.search.highlight.TokenSources; import org.apache.lucene.store.Directory; import org.apache.lucene.store.FSDirectory; public class Query { /* * * 索引后,进行查询,将查询结果封装为QueryBean对象,加入session,为JSP提供页面结果 * * */ private static String lighterStr(org.apache.lucene.search.Query query,Analyzer a,String fieldName,String txt) throws IOException, InvalidTokenOffsetsException { String str = null;//设定放回结果 QueryScorer queryScorer = new QueryScorer(query);//如果有需要,可以传入评分 //设置高亮标签 Formatter formatter = new SimpleHTMLFormatter("<span style='color:red;'>", "</span>"); //高亮分析器 Highlighter hl = new Highlighter(formatter, queryScorer); Fragmenter fragmenter = new SimpleSpanFragmenter(queryScorer); hl.setTextFragmenter(fragmenter); //获取返回结果 str = hl.getBestFragment(a, fieldName,txt); if(str == null){ return txt; } return str; } public static List query(String queryString,String indexUrl) throws IOException, ParseException, InvalidTokenOffsetsException { /**/ /* 这里放索引文件的位置 */ Path path = Paths.get(indexUrl); Directory indexDir = FSDirectory.open(path); org.apache.lucene.search.Query query = null; IndexReader reader = DirectoryReader.open(indexDir); IndexSearcher searcher = new IndexSearcher(reader); TopScoreDocCollector collector = TopScoreDocCollector.create(10); Analyzer analyzer = new StandardAnalyzer(); QueryScorer queryScorer = new QueryScorer(query);//如果有需要,可以传入评分 //设置高亮标签 Formatter formatter = new SimpleHTMLFormatter("<span style='color:red;'>", "</span>"); //高亮分析器 Highlighter hl = new Highlighter(formatter, queryScorer); Fragmenter fragmenter = new SimpleSpanFragmenter(queryScorer); hl.setTextFragmenter(fragmenter); try { QueryParser qp = new QueryParser( "content", analyzer); query = qp.parse(queryString); } catch (ParseException e) { } searcher.search(query, collector); ScoreDoc[] hits = collector.topDocs().scoreDocs; List<String> rstList = new ArrayList<String>();//结果列表(已高亮处理) System.out.println("Found " + hits.length + " hits."); for(int i=0;i<hits.length;i++) { int docId = hits[i].doc; Document d = searcher.doc(docId); String rst = ""; String title = d.get("title"); String content = d.get("content"); title = lighterStr(query, analyzer, "title", title); content = lighterStr(query, analyzer, "content", content); rst = d.get("url")+"\t"+title+"\t"+content; rstList.add(rst); //System.out.println((i + 1) +". " + d.get("url") +" "+title+"\t"+content); System.out.println((i + 1) +". " + rst); } return rstList; } }

 

posted @ 2015-07-07 16:15  MERRU  阅读(492)  评论(0编辑  收藏  举报