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

Lucene关键词高亮显示

时间:2015-07-07 18:29:20      阅读:147      评论:0      收藏:0      [点我收藏+]

标签:

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; } }

 

Lucene关键词高亮显示

标签:

原文地址:http://www.cnblogs.com/merru/p/4627332.html

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