从多个索引文件搜索及lucene高亮显示

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 class LuceneSearch {
public static void main(String[] args) throws Exception{
   LuceneSearch test = new LuceneSearch();
   //
   Hits h = null;
   h = test.search("显示 ");
   test.printResult(h);
  
  
   h = test.search("jy*");
   test.printResult(h);
  
   h = test.search("djy");
   test.printResult(h);
  

}
public LuceneSearch(){
   try{
    searcher = new IndexSearcher(IndexReader.open("E:\\lucene\\test4\\index"));
   }catch(Exception e){
    e.printStackTrace();
   }
}
//声明一个IndexSearcher对象
private IndexSearcher searcher = null;
//声明一个Query对象
private Query query = null;
ChineseAnalyzer analyzer = new ChineseAnalyzer();
Highlighter highlighter = null;
public final Hits search(String keyword){
   System.out.println("正在检索关键字:"+keyword);
   try{
    QueryParser qp = new QueryParser("title",analyzer);
    query = qp.parse(keyword);
   
    Term term = new Term("content",keyword);
    FuzzyQuery fq = new FuzzyQuery(term);
    Date start = new Date();
   
    //Hits hits = searcher.search(query);
    Hits hits = searcher.search(query);

   
    //高亮显示设置
    SimpleHTMLFormatter simpleHTMLFormatter = new SimpleHTMLFormatter("","");   
    highlighter = new Highlighter(simpleHTMLFormatter,new QueryScorer(query));      
    highlighter.setTextFragmenter(new SimpleFragmenter(10));//这个100是指定关键字字符串的context的长度,你可以自己设定,因为不可能返回整篇正文内容

   
   
    Date end = new Date();
    System.out.println("检索完成,用时"+(end.getTime()-start.getTime())+"毫秒");
    return hits;
   }catch(Exception e){
    e.printStackTrace();
    return null;
   }
  
}


public void printResult(Hits h){
   if(h.length() == 0){
    System.out.println("对不起,没有找到您要的结果.");
   }else{
    for(int i = 0; i < h.length(); i++){
     try{
      Document doc = h.doc(i);
      System.out.println("这个是"+(i+1)+"个结果:"+doc.get("id")+" title:"+doc.get("title")+"        content:"+doc.get("content"));
     
      //高亮出显示
      TokenStream tokenStream =analyzer.tokenStream("content", new StringReader(doc.get("title")));
      System.out.println(highlighter.getBestFragment(tokenStream,h.doc(i).get("title")));

      //System.out.println(doc.get("path"));
     }catch(Exception e){
      e.printStackTrace();
     }
    }
   }
   System.out.println("--------------------------------------");
}

发表评论

返回顶部