使用高亮显示时,需要单独引入lucene-highlighter-2.4.0.jar。这个jar包在目录 lucene-2.4.0/contrib/highlighter中,把它复制到项目的bin文件夹,并在Java Build Path中添加其引用。可以参考下面几个网页:
http://www.javaeye.com/wiki/topic/73588
http://hi.baidu.com/lotusxyhf/blog/item/cc06f634558516b4d0a2d329.html
导入的package有:

2

3

4

使用Highlighter的搜索代码:

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

顺便附加一个getBestFragment函数的API:
getBestFragment
public final String getBestFragment(TokenStream tokenStream, String text) throws IOException
-
Highlights chosen terms in a text, extracting the most relevant section. The document text is analysed in chunks to record hit statistics across the document. After accumulating stats, the fragment with the highest score is returned
-
-
Parameters:
-
tokenStream
- a stream of tokens identified in the text parameter, including offset information. This is typically produced by an analyzer re-parsing a document's text. Some work may be done on retrieving TokenStreams more efficently by adding support for storing original text position data in the Lucene index but this support is not currently available (as of Lucene 1.4 rc2). -
text
- text to highlight terms in
Returns:
- highlighted text fragment or null if no terms found Throws:
-
IOException
-
分词可以使用 JE-Analyzer 1.5.1或者其他的中文分词工具,可以从网上下载这个jar包。基本的使用方法和StandardAnalyzer类似。