lucene入门代码六(使用FastVectorHighlighter高亮)

1.本代码需要的jar包有:
lucene-fast-vector-highlighter-3.0.0.jar
lucene-core-3.0.0.jar
lucene-analyzers-3.0.0.jar


2.为什么要使用FastVectorHighlighter?
Highlighter是流行和广泛使用的Lucene应用,但索引大文件时,Highlighter是相当费时的,如果增加字符数分析与setMaxDocCharsToAnalyze。替代Highlighter,FastVectorHighlighter首次增加在Lucene的2.9版本,并提供更快的性能。超过Highlighte的FastVectorHighlighter的优势不仅是速度但也在功能上。首先,FastVectorHighlighter可以支持字段是记号化ngram断词。Highlighter不能支持的非常好。二,更有趣的是,FastVectorHighlighter可以输出五彩标记凸显出来,FastVectorHighlighter可以支持“per phrase”标记,而不是“per term”的标签。
代码如下:
package com.yale.lucene;

import java.io.FileWriter;

import org.apache.lucene.analysis.Analyzer;
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.document.Field.TermVector;
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.vectorhighlight.BaseFragmentsBuilder;
import org.apache.lucene.search.vectorhighlight.FastVectorHighlighter;
import org.apache.lucene.search.vectorhighlight.FieldQuery;
import org.apache.lucene.search.vectorhighlight.FragListBuilder;
import org.apache.lucene.search.vectorhighlight.FragmentsBuilder;
import org.apache.lucene.search.vectorhighlight.ScoreOrderFragmentsBuilder;
import org.apache.lucene.search.vectorhighlight.SimpleFragListBuilder;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.RAMDirectory;
import org.apache.lucene.util.Version;
/**
 * 
 * 使用FastVectorHighlighter高亮
 *
 */
public class FastVectorHighlighterSample
{
	static final String[] DOCS = {
			"the quick brown fox jumps over the lazy dog",
			"the quick gold fox jumped over the lazy black dog",
			"the quick fox jumps over the black dog",
			"the red fox jumped over the lazy dark gray dog" };
	static final String QUERY = "quick OR fox OR \"lazy dog\"~1";
	static final String F = "f";
	static Directory dir = new RAMDirectory();
	static Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_30);
	static String FILENAME = "F://新建文件夹//luceneTest//dataSource//highlight.html";

	public static void main(String[] args) throws Exception
	{
		// 创建索引
		makeIndex();
		// 搜索
		searchIndex(FILENAME);
	}

	static void makeIndex() throws Exception
	{
		IndexWriter writer = new IndexWriter(dir, analyzer, true,
				MaxFieldLength.UNLIMITED);
		for (String d : DOCS)
		{
			Document doc = new Document();
			doc.add(new Field(F, d, Store.YES, Index.ANALYZED,
					TermVector.WITH_POSITIONS_OFFSETS));
			writer.addDocument(doc);
		}
		writer.close();

	}

	static void searchIndex(String filename) throws Exception
	{
		QueryParser parser = new QueryParser(Version.LUCENE_30, F, analyzer);
		Query query = parser.parse(QUERY);
		FastVectorHighlighter highlighter = getHighlighter();
		FieldQuery fieldQuery = highlighter.getFieldQuery(query);
		IndexSearcher searcher = new IndexSearcher(dir);
		TopDocs docs = searcher.search(query, 10);
		FileWriter writer = new FileWriter(filename);
		writer.write("<html>");
		writer.write("<body>");
		writer.write("<p>QUERY : " + QUERY + "</p>");
		for (ScoreDoc scoreDoc : docs.scoreDocs)
		{
			String snippet = highlighter.getBestFragment(fieldQuery,
					searcher.getIndexReader(), scoreDoc.doc, F, 100);
			if (snippet != null)
			{
				writer.write(scoreDoc.doc + " : " + snippet + "<br/>");
			}
		}
		writer.write("</body></html>");
		writer.close();
		searcher.close();
		System.out.println("完成");
	}

	static FastVectorHighlighter getHighlighter()
	{
		FragListBuilder fragListBuilder = new SimpleFragListBuilder();
		FragmentsBuilder fragmentsBuilder = new ScoreOrderFragmentsBuilder(
				BaseFragmentsBuilder.COLORED_PRE_TAGS,
				BaseFragmentsBuilder.COLORED_POST_TAGS);
		return new FastVectorHighlighter(true, true, fragListBuilder,
				fragmentsBuilder);
	}

}


效果图如下:

lucene入门代码六(使用FastVectorHighlighter高亮)_第1张图片

你可能感兴趣的:(apache,Lucene)