全文检索总结:
1、 在http://labs.renren.com/apache-mirror//lucene/java/,下载lucene-core-3.0.3.jar、lucene-highlighter-3.0.3.jar(在lucene-3.0.3/lucene-3.0.3/contrib/highlighter中),导入项目中。
2、 在http://code.google.com/p/paoding/downloads/list,下载paoding-analysis-2.0.4-beta,将src中源码导入到项目中。若直接使用paoding-analysis.jar会出现问题,经过网上了解,需将paoding源码加入的项目中,才能够支持lucene3。
3、 将lucene-3.0.3/lucene-3.0.3/src/demo/org/apache/lucene/demo下源码导入的项目中,在IndexHTML类基础上修改为CreateIndex,作为创建索引的action。
4、 Paoding分词的使用,在createindex中,import PaodingAnalyzer;,将原标准分析器替换为paodinganalyzer。
5、 将paoding-analysis-2.0.4-beta下的dic放置在某个位置,在系统环境变量中增加PAODING_DIC_HOME变量,将其值置为dic的路径。这个就是paoding分词的字典。
6、 在创建索引过程中出现的中文乱码问题(只支持gb,不支持utf-8)解决办法:修改HTMLDocument(demo中类)类中Document方法中代码:
由原来: FileInputStream fis = new FileInputStream(f);
HTMLParser parser = new HTMLParser(fis);
改为:FileInputStream fis = new FileInputStream(f);
Reader reader = new InputStreamReader(fis, "UTF-8");
HTMLParser parser = new HTMLParser(reader);
7、 当然在后台生成索引用到paoding,那么在前台搜索中也要用paoding进行分词。
8、 高亮的解决,在前台展现页面中,增加如下代码,将summary中搜索文字高亮为result,将result展现即可。
String text = doc.get("summary");
TokenStream ts = TokenSources.getAnyTokenStream(reader, hits.scoreDocs[i].doc, "summary",analyzer);
SimpleHTMLFormatter sHtmlF = new SimpleHTMLFormatter("<font color='red'>", "</font>");
Highlighter highlighter = new Highlighter(sHtmlF,new QueryScorer(query));
String result = highlighter.getBestFragments(ts,text, 3, "...");
9、 在做高亮时,highlighter的版本要求与lucene版本相符合,不然有问题。