lucene最新版本为1.9,从apache svn中checkout 出来已经包括了Highlighter
Lucene Highlighter
            
    
    
        luceneApacheSVNjunit 
测试一下:

Lucene Highlighter
            
    
    
        luceneApacheSVNjunit package org.apache.lucene.search.highlight;
Lucene Highlighter
            
    
    
        luceneApacheSVNjunit 
Lucene Highlighter
            
    
    
        luceneApacheSVNjunit 
import junit.framework.TestCase;
Lucene Highlighter
            
    
    
        luceneApacheSVNjunit 
import org.apache.lucene.analysis.Analyzer;
Lucene Highlighter
            
    
    
        luceneApacheSVNjunit 
import org.apache.lucene.analysis.TokenStream;
Lucene Highlighter
            
    
    
        luceneApacheSVNjunit 
import org.apache.lucene.analysis.standard.StandardAnalyzer;
Lucene Highlighter
            
    
    
        luceneApacheSVNjunit 
import org.apache.lucene.document.Document;
Lucene Highlighter
            
    
    
        luceneApacheSVNjunit 
import org.apache.lucene.document.Field;
Lucene Highlighter
            
    
    
        luceneApacheSVNjunit 
import org.apache.lucene.index.IndexReader;
Lucene Highlighter
            
    
    
        luceneApacheSVNjunit 
import org.apache.lucene.index.IndexWriter;
Lucene Highlighter
            
    
    
        luceneApacheSVNjunit 
import org.apache.lucene.queryParser.QueryParser;
Lucene Highlighter
            
    
    
        luceneApacheSVNjunit 
import org.apache.lucene.search.Hits;
Lucene Highlighter
            
    
    
        luceneApacheSVNjunit 
import org.apache.lucene.search.IndexSearcher;
Lucene Highlighter
            
    
    
        luceneApacheSVNjunit 
import org.apache.lucene.search.Query;
Lucene Highlighter
            
    
    
        luceneApacheSVNjunit 
import org.apache.lucene.search.Searcher;
Lucene Highlighter
            
    
    
        luceneApacheSVNjunit 
import org.apache.lucene.store.RAMDirectory;
Lucene Highlighter
            
    
    
        luceneApacheSVNjunit 
Lucene Highlighter
            
    
    
        luceneApacheSVNjunit 
import java.io.IOException;
Lucene Highlighter
            
    
    
        luceneApacheSVNjunit 
import java.io.StringReader;
Lucene Highlighter
            
    
    
        luceneApacheSVNjunit 
Lucene Highlighter
            
    
    
        luceneApacheSVNjunit Lucene Highlighter
            
    
    
        luceneApacheSVNjunit 
/** *//**
Lucene Highlighter
            
    
    
        luceneApacheSVNjunit  * 
@author martin
Lucene Highlighter
            
    
    
        luceneApacheSVNjunit  
*/

Lucene Highlighter
            
    
    
        luceneApacheSVNjunit Lucene Highlighter
            
    
    
        luceneApacheSVNjunit 
public class WordsHighlighterTest extends TestCase Lucene Highlighter
            
    
    
        luceneApacheSVNjunit {
Lucene Highlighter
            
    
    
        luceneApacheSVNjunit     
private IndexReader reader;
Lucene Highlighter
            
    
    
        luceneApacheSVNjunit     RAMDirectory ramDirectory;
Lucene Highlighter
            
    
    
        luceneApacheSVNjunit     
final private static String FIELD_NAME = "contents";
Lucene Highlighter
            
    
    
        luceneApacheSVNjunit     
final private static String queryString = "索引";
Lucene Highlighter
            
    
    
        luceneApacheSVNjunit 
Lucene Highlighter
            
    
    
        luceneApacheSVNjunit Lucene Highlighter
            
    
    
        luceneApacheSVNjunit     String [] words 
= Lucene Highlighter
            
    
    
        luceneApacheSVNjunit {
Lucene Highlighter
            
    
    
        luceneApacheSVNjunit             
"1:索引内容结构:Document,以及包含于Document的多个Field索",
Lucene Highlighter
            
    
    
        luceneApacheSVNjunit             
"2:索引内容优先性调整因子,boost(可对整个Document或Field指定).",
Lucene Highlighter
            
    
    
        luceneApacheSVNjunit             
"3:索引的写入IndexWriter,索引的写入目标Directory,实现包括FsDirectory跟RamDirectory等",
Lucene Highlighter
            
    
    
        luceneApacheSVNjunit             
"4:索引创建速度的调整"
Lucene Highlighter
            
    
    
        luceneApacheSVNjunit     }
;
Lucene Highlighter
            
    
    
        luceneApacheSVNjunit 
Lucene Highlighter
            
    
    
        luceneApacheSVNjunit 
Lucene Highlighter
            
    
    
        luceneApacheSVNjunit Lucene Highlighter
            
    
    
        luceneApacheSVNjunit     
protected void setUp() throws Exception Lucene Highlighter
            
    
    
        luceneApacheSVNjunit {
Lucene Highlighter
            
    
    
        luceneApacheSVNjunit         ramDirectory 
= new RAMDirectory();
Lucene Highlighter
            
    
    
        luceneApacheSVNjunit         IndexWriter indexWriter 
= new IndexWriter(ramDirectory, new StandardAnalyzer(), true);
Lucene Highlighter
            
    
    
        luceneApacheSVNjunit Lucene Highlighter
            
    
    
        luceneApacheSVNjunit         
for (String s : words) Lucene Highlighter
            
    
    
        luceneApacheSVNjunit {
Lucene Highlighter
            
    
    
        luceneApacheSVNjunit             addDoc(indexWriter, s);
Lucene Highlighter
            
    
    
        luceneApacheSVNjunit         }

Lucene Highlighter
            
    
    
        luceneApacheSVNjunit         indexWriter.optimize();
Lucene Highlighter
            
    
    
        luceneApacheSVNjunit         indexWriter.close();
Lucene Highlighter
            
    
    
        luceneApacheSVNjunit         reader 
= IndexReader.open(ramDirectory);
Lucene Highlighter
            
    
    
        luceneApacheSVNjunit     }

Lucene Highlighter
            
    
    
        luceneApacheSVNjunit 
Lucene Highlighter
            
    
    
        luceneApacheSVNjunit Lucene Highlighter
            
    
    
        luceneApacheSVNjunit     
private void addDoc(IndexWriter indexWriter, String s) throws IOException Lucene Highlighter
            
    
    
        luceneApacheSVNjunit {
Lucene Highlighter
            
    
    
        luceneApacheSVNjunit         Document doc 
= new Document();
Lucene Highlighter
            
    
    
        luceneApacheSVNjunit         doc.add(
new Field(FIELD_NAME, s, Field.Store.YES, Field.Index.TOKENIZED, Field.TermVector.YES));
Lucene Highlighter
            
    
    
        luceneApacheSVNjunit         indexWriter.addDocument(doc);
Lucene Highlighter
            
    
    
        luceneApacheSVNjunit     }

Lucene Highlighter
            
    
    
        luceneApacheSVNjunit 
Lucene Highlighter
            
    
    
        luceneApacheSVNjunit Lucene Highlighter
            
    
    
        luceneApacheSVNjunit     
public void testSimpleWords() throws Exception Lucene Highlighter
            
    
    
        luceneApacheSVNjunit {
Lucene Highlighter
            
    
    
        luceneApacheSVNjunit         Query query 
= new QueryParser(FIELD_NAME, new StandardAnalyzer()).parse(queryString);
Lucene Highlighter
            
    
    
        luceneApacheSVNjunit         query 
= query.rewrite(reader);
Lucene Highlighter
            
    
    
        luceneApacheSVNjunit         System.out.println(
"Searching for: " + query.toString(FIELD_NAME));
Lucene Highlighter
            
    
    
        luceneApacheSVNjunit         Searcher searcher 
= new IndexSearcher(ramDirectory);
Lucene Highlighter
            
    
    
        luceneApacheSVNjunit         Hits hits 
= searcher.search(query);
Lucene Highlighter
            
    
    
        luceneApacheSVNjunit         Highlighter highlighter 
= new Highlighter(new SimpleHTMLFormatter("<font color="red">""</font>"), new QueryScorer(query));
Lucene Highlighter
            
    
    
        luceneApacheSVNjunit         highlighter.setTextFragmenter(
new SimpleFragmenter(20));
Lucene Highlighter
            
    
    
        luceneApacheSVNjunit         Analyzer analyzer 
= new StandardAnalyzer();
Lucene Highlighter
            
    
    
        luceneApacheSVNjunit Lucene Highlighter
            
    
    
        luceneApacheSVNjunit         
for (int i = 0; i < hits.length(); i++Lucene Highlighter
            
    
    
        luceneApacheSVNjunit {
Lucene Highlighter
            
    
    
        luceneApacheSVNjunit             String text 
= hits.doc(i).get(FIELD_NAME);
Lucene Highlighter
            
    
    
        luceneApacheSVNjunit             TokenStream tokenStream 
= analyzer.tokenStream(FIELD_NAME, new StringReader(text));
Lucene Highlighter
            
    
    
        luceneApacheSVNjunit             String result 
= highlighter.getBestFragments(tokenStream, text, 2"Lucene Highlighter
            
    
    
        luceneApacheSVNjunit ");
Lucene Highlighter
            
    
    
        luceneApacheSVNjunit             System.out.println(
" " + result);
Lucene Highlighter
            
    
    
        luceneApacheSVNjunit         }

Lucene Highlighter
            
    
    
        luceneApacheSVNjunit     }

Lucene Highlighter
            
    
    
        luceneApacheSVNjunit 
Lucene Highlighter
            
    
    
        luceneApacheSVNjunit Lucene Highlighter
            
    
    
        luceneApacheSVNjunit     
protected void tearDown() throws Exception Lucene Highlighter
            
    
    
        luceneApacheSVNjunit {
Lucene Highlighter
            
    
    
        luceneApacheSVNjunit         
super.tearDown();
Lucene Highlighter
            
    
    
        luceneApacheSVNjunit     }

Lucene Highlighter
            
    
    
        luceneApacheSVNjunit }

输出:

Searching for: "索 引"
4:索引创建速度的调整
3:索引的写入IndexWriter,索引的写入目标Directory,实现包
1:索引内容结构:Document,以索
2:索引内容优先性调整因子,boost(可对整个Document或Field指