lucene4.9之highlight

测试高亮

import java.io.File;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.cn.smart.SmartChineseAnalyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.queryparser.classic.MultiFieldQueryParser;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
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.SimpleHTMLFormatter;
import org.apache.lucene.search.highlight.SimpleSpanFragmenter;
import org.apache.lucene.search.highlight.TextFragment;
import org.apache.lucene.search.highlight.TokenSources;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
import org.junit.Test;

/**
 * 高亮测试
 * 
 * @author Administrator
 * 
 */
public class HighLightTest {
	String indexPath = "F:\\eclipse\\LuceneTest\\luceneIndex";
	// 分析器
	Analyzer analyzer = new SmartChineseAnalyzer(Version.LUCENE_4_9);

	@Test
	public void test() throws Exception {
		String queryString = "房间";
		String[] fields = { "name", "content" };
		QueryParser parser = new MultiFieldQueryParser(Version.LUCENE_4_9, fields, analyzer);
		Query query = parser.parse(queryString);

		IndexReader reader = DirectoryReader.open(FSDirectory.open(new File(indexPath)));
		IndexSearcher indexSearcher = new IndexSearcher(reader);
		TopDocs topDocs = indexSearcher.search(query, 100);
		
		//当有匹配的文档时才能用高亮
		if (topDocs.totalHits > 0) {

			// 默认为<B></B>高亮
			// SimpleHTMLFormatter htmlFormatter = new SimpleHTMLFormatter();
			// 自定义高亮
			SimpleHTMLFormatter htmlFormatter = new SimpleHTMLFormatter("<strong>", "</strong>");
			Highlighter highlighter = new Highlighter(htmlFormatter, new QueryScorer(query));
			for (int i = 0; i < 10; i++) {
				int id = topDocs.scoreDocs[i].doc;
				Document doc = indexSearcher.doc(id);
				String text = doc.get("content");
				TokenStream tokenStream = TokenSources.getAnyTokenStream(indexSearcher.getIndexReader(), id, "content", analyzer);

				TextFragment[] frag = highlighter.getBestTextFragments(tokenStream, text, false, 1);// highlighter.getBestFragments(tokenStream,
																									// text,
																									// 3,
																									// "...");
				for (int j = 0; j < frag.length; j++) {
					if ((frag[j] != null) && (frag[j].getScore() > 0)) {
						System.out.println((frag[j].toString()));
					}
				}
				// Term vector
				text = doc.get("name");
				tokenStream = TokenSources.getAnyTokenStream(indexSearcher.getIndexReader(), topDocs.scoreDocs[i].doc, "name",
						analyzer);
				frag = highlighter.getBestTextFragments(tokenStream, text, false, 10);
				for (int j = 0; j < frag.length; j++) {
					if ((frag[j] != null) && (frag[j].getScore() > 0)) {
						System.out.println((frag[j].toString()));
					}
				}
				System.out.println("-------------");
			}
			
		}
	}
}

 

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