package cn.itcast.cc.highlighter; import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.document.*; import org.apache.lucene.document.Field.*; import org.apache.lucene.index.IndexWriter; import org.apache.lucene.index.IndexWriter.MaxFieldLength; import org.apache.lucene.queryParser.*; import org.apache.lucene.search.*; import org.apache.lucene.search.highlight.*; import org.apache.lucene.search.highlight.Scorer; import org.junit.Test; import jeasy.analysis.MMAnalyzer; public class HighLighterTesst { private String path = "./index/";// lucene的索引数据被保存在这个目录中 private Analyzer analyzer = new MMAnalyzer();// 词库分词器 @Test public void test() { try { // 创建索引数据 IndexWriter indexWriter = new IndexWriter(this.path, this.analyzer, MaxFieldLength.LIMITED); Document doc = new Document(); doc.add(new Field("content", "中国人民加油!", Store.YES, Index.ANALYZED)); indexWriter.addDocument(doc); indexWriter.close(); // 查询数据 IndexSearcher indexSearcher = new IndexSearcher(this.path); QueryParser queryParser = new MultiFieldQueryParser( new String[] { "content" }, this.analyzer); Query query = queryParser.parse("加油"); TopDocs topDocs = indexSearcher.search(query, null, 10); // 创建高亮 // SimpleHTMLFormatter的默认值为"<a></a>" Formatter formatter = new SimpleHTMLFormatter("<span>", "</span>"); Scorer scorer = new QueryScorer(query); Highlighter highlighter = new Highlighter(formatter, scorer); // 打印结果 for (ScoreDoc scoreDoc : topDocs.scoreDocs) { // 取出查询结果 Document searchdoc = indexSearcher.doc(scoreDoc.doc); String searchstr = searchdoc.getField("content").stringValue(); // 添加高亮 String histr = highlighter.getBestFragment(this.analyzer, "content", searchstr); System.out.println(histr); } indexSearcher.close(); } catch (Exception e) { e.printStackTrace(); } } } |