使用高亮显示时,需要单独引入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有:

1发一个Lucene 2.4.0对搜索结果高亮显示的代码import org.apache.lucene.search.highlight.Highlighter;
2发一个Lucene 2.4.0对搜索结果高亮显示的代码import org.apache.lucene.search.highlight.QueryScorer;
3发一个Lucene 2.4.0对搜索结果高亮显示的代码import org.apache.lucene.search.highlight.SimpleFragmenter;
4发一个Lucene 2.4.0对搜索结果高亮显示的代码import org.apache.lucene.search.highlight.SimpleHTMLFormatter;

 

使用Highlighter的搜索代码:

 1    }

 

顺便附加一个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类似。

相关文章:

  • 2022-01-26
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-08-01
  • 2021-08-04
  • 2021-06-27
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-07-17
  • 2021-08-15
  • 2021-12-24
  • 2021-09-01
  • 2021-11-18
相关资源
相似解决方案