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

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

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

InBlock.gif        indexWriter.optimize();
InBlock.gif        indexWriter.close();
InBlock.gif        reader 
= IndexReader.open(ramDirectory);
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
private void addDoc(IndexWriter indexWriter, String s) throws IOException dot.gif{
InBlock.gif        Document doc 
= new Document();
InBlock.gif        doc.add(
new Field(FIELD_NAME, s, Field.Store.YES, Field.Index.TOKENIZED, Field.TermVector.YES));
InBlock.gif        indexWriter.addDocument(doc);
ExpandedSubBlockEnd.gif    }

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

ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
protected void tearDown() throws Exception dot.gif{
InBlock.gif        
super.tearDown();
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

输出:

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