`
sungang_1120
  • 浏览: 311282 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类

lucene3.0.0 高亮显示实例

 
阅读更多
lucene3.0.0 高亮显示实例
package com.tx.test;

import java.io.File;
import java.io.StringReader;

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.document.Field;
import org.apache.lucene.document.Field.Index;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriter.MaxFieldLength;
import org.apache.lucene.queryParser.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.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.store.Directory;
import org.apache.lucene.store.SimpleFSDirectory;
import org.apache.lucene.util.Version;
import org.junit.Test;

public class TestHighlightDemo {

private File file = new File("f:" + File.separator + "indexDir5");
private Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_30);
private Directory directory;
private IndexWriter writer;
private IndexSearcher searcher;
//创建索引
@Test
public void createIndex() throws Exception {
writer = new IndexWriter(new SimpleFSDirectory(file), analyzer, true,
MaxFieldLength.LIMITED);

Document docA = new Document();
String fileTextA = "火烧云总是燃烧着消失在因为太阳冲下地平线的时刻,然后便是宁静的自然的天籁," +
"没有谁会在这样的时光的镜片里伤感自语,因为灿烂给人以安静的舒适感";
Field fieldA = new Field("contents", fileTextA, Store.YES, Index.ANALYZED);
docA.add(fieldA);


Document docB = new Document();
String fileTextB = "带有以因为伤痕为代价的美丽风景总是让人不由地惴惴不安," +
"紧接着袭面而来的抑或是病痛因为抑或是灾难,没有谁会能够安逸着恬然,因为模糊让人撕心裂肺地想呐喊";
Field fieldB = new Field("contents", fileTextB, Store.YES, Index.ANALYZED);
docB.add(fieldB);

Document docC = new Document();
String fileTextC = "因为我喜欢上了一个人孤独因为地行游,在梦与海洋的交接地带炽烈燃烧着。"+
"双方都,因为一条孤独的鱼喜欢上了火焰的颜色,真是荒唐地不合逻辑";
Field fieldC = new Field("contents", fileTextC, Store.YES, Index.ANALYZED);
docB.add(fieldC);

writer.addDocument(docA);
writer.addDocument(docB);
writer.addDocument(docC);

writer.close();
}

//查詢
@Test
public void search(String fieldName,String keyword) throws Exception{
searcher = new IndexSearcher(new SimpleFSDirectory(file));
//创建查询解析器
QueryParser parser = new QueryParser(Version.LUCENE_30, "contents", analyzer);

Query query = parser.parse(keyword);

TopDocs tDocs = searcher.search(query, 100);
System.out.println("你要搜索的内容:"+keyword);
//tDocs.totalHits表示一共搜索了多少
System.out.println("一共找到了  " +tDocs.totalHits+"个结果");
System.out.println();

for (int i = 0; i < tDocs.scoreDocs.length; i++) {
ScoreDoc sDoc =  tDocs.scoreDocs[i];
System.out.println("内容索引编号:"+sDoc.doc);
System.out.println("相似度得分:"+sDoc.score);

Document document = searcher.doc(sDoc.doc);
String text = document.get(fieldName);
System.out.println("内容是:"+document.get(fieldName));
//高亮定义
SimpleHTMLFormatter formatter = new SimpleHTMLFormatter("<font color='red'>", "</font>");

Highlighter highlighter = new Highlighter(formatter, new QueryScorer(query));
highlighter.setTextFragmenter(new SimpleFragmenter(text.length()));

if (text != null) {
TokenStream tokenStream = analyzer.tokenStream(fieldName, new StringReader(text));
//高亮显示的内容
String highLightText = highlighter.getBestFragment(tokenStream,text);
System.out.println("高亮显示第"+(i+1)+"条检索结果如下:");
System.out.println(highLightText);
System.out.println();
}
}
searcher.close();
}
public static void main(String[] args) throws Exception{
TestHighlightDemo thd = new TestHighlightDemo();
//创建索引
thd.createIndex();
//查询  高亮显示
thd.search("contents","因为");
}
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics