lucene高亮代码


lucene高亮:lucene高亮显示 疯狂代码 http://www.crazycoder.cn/ ĵ:http:/www.crazycoder.cn/DeveloperUtil/Article54068.html package src; import java.io.StringReader; import java.util.Date; import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.analysis.SimpleAnalyzer; import org.apache.lucene.analysis.TokenStream; import org.apache.lucene.analysis.cjk.CJKAnalyzer; import org.apache.lucene.analysis.cn.ChineseAnalyzer; import org.apache.lucene.analysis.standard.StandardAnalyzer; import org.apache.lucene.document.Document; import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.Term; import org.apache.lucene.queryParser.QueryParser; import org.apache.lucene.search.FuzzyQuery; import org.apache.lucene.search.Hits; import org.apache.lucene.search.IndexSearcher; import org.apache.lucene.search.Query; import java.util.Date; import org.apache.lucene.analysis.standard.StandardAnalyzer; import org.apache.lucene.document.Document; import org.apache.lucene.index.IndexReader; import org.apache.lucene.queryParser.QueryParser; import org.apache.lucene.search.Hits; import org.apache.lucene.search.IndexSearcher; import org.apache.lucene.search.Query; import org.apache.lucene.search.highlight.Highlighter; import org.apache.lucene.search.highlight.QueryScorer; import org.apache.lucene.search.highlight.SimpleFragmenter; import org.apache.lucene.search.highlight.SimpleHTMLFormatter; public LuceneSearch { public void (String args) throws Exception{   LuceneSearch test = LuceneSearch;   //   Hits h = null;   h = test.search("显示 ");   test.prResult(h);   h = test.search("jy*");   test.prResult(h);   h = test.search("djy");   test.prResult(h); } public LuceneSearch{   try{   searcher = IndexSearcher(IndexReader.open("E:lucene  est4index"));   }catch(Exception e){   e.prStackTrace;   } } //声明个IndexSearcher对象 private IndexSearcher searcher = null; //声明个Query对象 private Query query = null; ChineseAnalyzer analyzer = ChineseAnalyzer; Highlighter highlighter = null; public final Hits search(String keyword){   .out.prln("正在检索关键字:"+keyword);   try{   QueryParser qp = QueryParser("title",analyzer);   query = qp.parse(keyword);   Term term = Term("content",keyword);   FuzzyQuery fq = FuzzyQuery(term);   Date start = Date;   //Hits hits = searcher.search(query);   Hits hits = searcher.search(query);   //高亮显示设置   SimpleHTMLFormatter simpleHTMLFormatter = SimpleHTMLFormatter("","");    highlighter = Highlighter(simpleHTMLFormatter, QueryScorer(query));      highlighter.TextFragmenter( SimpleFragmenter(10));//这个100是指定关键字串context长度你可以自 己设定不可能返回整篇正文内容   Date end = Date;   .out.prln("检索完成,用时"+(end.getTime-start.getTime)+"毫秒");    hits;   }catch(Exception e){   e.prStackTrace;    null;   } } public void prResult(Hits h){   (h.length 0){   .out.prln("对不起,没有找到您要结果.");   }{   for( i = 0; i < h.length; i){    try{    Document doc = h.doc(i);    .out.prln("这个是"+(i+1)+"个结果:"+doc.get("id")+" title:"+doc.get("title")+"     content:"+doc.get("content"));    //高亮出显示    TokenStream tokenStream =analyzer.tokenStream("content", StringReader(doc.get("title")));    .out.prln(highlighter.getBestFragment(tokenStream,h.doc(i).get("title")));    //.out.prln(doc.get("path"));    }catch(Exception e){    e.prStackTrace;    }   }   }   .out.prln("--------------------------------------"); } } 2009-1-15 22:30:33 疯狂代码 http://www.crazycoder.cn/
还剩3页未读

继续阅读

下载pdf到电脑,查找使用更方便

pdf的实际排版效果,会与网站的显示效果略有不同!!

需要 3 金币 [ 分享pdf获得金币 ] 0 人已下载

下载pdf

pdf贡献者

saidasaida727

贡献于2012-12-04

下载需要 3 金币 [金币充值 ]
亲,您也可以通过 分享原创pdf 来获得金币奖励!
下载pdf