lucene学习---高亮显示

5 篇文章 0 订阅

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("<read>","</read>");   
   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("--------------------------------------");
 }
}
 

评论 14
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值