lucene入门代码五(在搜索结果中使用高亮)

1.本代码需要的jar包有:
[color=green]lucene-core-3.0.0.jar
lucene-analyzers-3.0.0.jar
lucene-highlighter-3.0.0.jar[/color]
代码如下:

package com.yale.lucene;


import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.SimpleAnalyzer;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.search.highlight.Highlighter;
import org.apache.lucene.search.highlight.QueryScorer;
import org.apache.lucene.search.highlight.SimpleSpanFragmenter;
import org.apache.lucene.search.highlight.TokenSources;
import org.apache.lucene.store.RAMDirectory;
import org.apache.lucene.util.Version;
/**
*
* 为了简单起见,创建内存目录,存放索引,然后在contents高亮搜索关键字
*
*/
public class HighlightAtSearchResult
{
private static final String text =
"In this section we'll show you how to make the simplest " +
"programmatic query, searching for a single term, and then " +
"we'll see how to use QueryParser to accept textual queries. " +
"In the sections that follow, we’ll take this simple example " +
"further by detailing all the query types built into Lucene. " +
"We begin with the simplest search of all: searching for all " +
"documents that contain a single term.";
public static void main(String[] args) throws Exception
{
//创建索引
RAMDirectory dir = new RAMDirectory();
IndexWriter writer = new IndexWriter(dir,new StandardAnalyzer(
Version.LUCENE_30), IndexWriter.MaxFieldLength.UNLIMITED);
Document doc = new Document();
doc.add(new Field("title","highlight matches in search results",Field.Store.NO,Field.Index.NOT_ANALYZED_NO_NORMS));
doc.add(new Field("contents",text,Field.Store.YES,Field.Index.ANALYZED));
writer.addDocument(doc);
IndexReader reader = writer.getReader();
//查询索引
IndexSearcher searcher = new IndexSearcher(reader);
TermQuery query = new TermQuery(new Term("contents", "term"));
TopDocs hits = searcher.search(query,10);
QueryScorer scorer = new QueryScorer(query, "contents");
Highlighter highlighter = new Highlighter(scorer);
highlighter.setTextFragmenter(new SimpleSpanFragmenter(scorer));
Analyzer analyzer = new SimpleAnalyzer();
for(ScoreDoc sd :hits.scoreDocs){
Document docs = searcher.doc(sd.doc);
String contents = docs.get("contents");
TokenStream tokens = TokenSources.getAnyTokenStream(searcher.getIndexReader(),sd.doc,"contents",analyzer);
String fragment =
highlighter.getBestFragments(tokens, contents,3,"...");
System.out.println(fragment);
}
reader.close();
searcher.close();
writer.close();

}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值