lucene-搜索高亮提示(四)

2017-01-05 11:09:21来源:oschina作者:StayY人点击


hightlighter介绍

我们在查询的时候,希望对我们的搜索结果与搜索内容相近的地方进行着重显示,效果如下:image


这样做的好处:1.视觉上让人便于查找有搜索对应的文本块。2.界面展示更友好


lucene提供了highlighter插件来体现类似的效果


示例代码
package net.begincode.lucene.test;
import java.io.StringReader;
import java.nio.file.Paths;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.search.highlight.Fragmenter;
import org.apache.lucene.search.highlight.Highlighter;
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.store.Directory;
import org.apache.lucene.store.FSDirectory;
import net.begincode.lucene.analyzer.MyIkAnalyzer;
public class LuceneDemo3 {
public static void main(String[] args) throws Exception {
MyIkAnalyzer ik = new MyIkAnalyzer();
Directory directory = FSDirectory.open(Paths.get("d:/begincodeIndex/test"));
IndexWriterConfig config = new IndexWriterConfig(ik);
IndexWriter iwriter = new IndexWriter(directory, config);
// 读取索引
DirectoryReader ireader = DirectoryReader.open(directory);
// 创建索引检索对象 统一利用IndexSearcher来进行
IndexSearcher searcher = new IndexSearcher(ireader);
// 创建Query
QueryParser parser = new QueryParser("title", ik);
Query query = parser.parse("mysql");
TopDocs hits = searcher.search(query, 100);
QueryScorer scorer = new QueryScorer(query);// 查询得分
SimpleHTMLFormatter simpleHTMLFormatter = new SimpleHTMLFormatter("", "");// 对查询的数据格式化;无参构造器的默认是将关键字加粗
Highlighter highlighter = new Highlighter(simpleHTMLFormatter, scorer);// 根据得分和格式化
highlighter.setTextFragmenter(new SimpleFragmenter(20));// 设置每次返回的字符数
for (int i = 0; i < hits.scoreDocs.length; i++) {
Document doc = searcher.doc(hits.scoreDocs[i].doc);
String str = highlighter.getBestFragment(ik, "content", doc.get("content"));
System.out.println(str);
}
}
}

输出


加载扩展词典:mydict.dic
加载扩展停止词典:surname.dic
null
null
为什么mysql安装失败为什么mysql安装失败为什么mysql安装失败
mysql 反对撒发射点发射点

最新文章

123

最新摄影

微信扫一扫

第七城市微信公众平台