lucene基础

来源:互联网 发布:印度进出口数据2016 编辑:程序博客网 时间:2024/05/16 08:09

1.保存索引 

  public static void save(Document doc){
      IndexWriter indexWriter=null;
      try {
       indexWriter=new IndexWriter(path,analyzer,MaxFieldLength.LIMITED);
       indexWriter.addDocument(doc);
  } catch (Exception e) {
   throw new RuntimeException(e);
  }finally{
   if(indexWriter!=null){
    try {
     indexWriter.close();
    } catch (CorruptIndexException e) {
     e.printStackTrace();
    } catch (IOException e) {
     e.printStackTrace();
    }
   }
  }
     }

 

 

2.创建要保存的document

String title = "IndexWriter.addDocument 's javadoc";
 String content = "Adds  a document to this index. If the document contains more than setMaxFieldLength(int) terms ;

 

 Field t = new Field("content", content,Store.YES,Index.ANALYZED);
  Field c=new Field("title",title,Store.YES,Index.ANALYZED);
  Document document=new Document();
  document.add(t);
  document.add(c);
  save(document);

 

 

 

3.查询

 public static RearchResult search(String queryString) {
      QueryParser queryParse=new QueryParser("title",analyzer);
     
      IndexSearcher indexSeacrher=null;
      int totalHits=0;
      List<Document> documents=new ArrayList<Document>();
      try {
   Query query=queryParse.parse(queryString);
   indexSeacrher= new IndexSearcher(path);
   TopDocs topDocs=indexSeacrher.search(query, null, 1000);
   totalHits=topDocs.totalHits;
   ScoreDoc[] scoreDoc=topDocs.scoreDocs;
   for(ScoreDoc sdoc : scoreDoc){
    Document document=indexSeacrher.doc(sdoc.doc);
    documents.add(document);
   }
  } catch (ParseException e) {
   e.printStackTrace();
  } catch (CorruptIndexException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }
      return new RearchResult(totalHits,documents);
     }

 

 

 

 

 

4.更新索引

 public static void update(Term term,Document doc){
      IndexWriter indexWriter=null;
      // 先删除包含指定 term 的 Documents,再给doc重新建立索引
      try {
   indexWriter=new IndexWriter(path,analyzer,MaxFieldLength.LIMITED);
   indexWriter.updateDocument(term, doc);
  } catch (CorruptIndexException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (LockObtainFailedException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }finally{
   if(indexWriter!=null){
    try {
     indexWriter.close();
    } catch (CorruptIndexException e) {
     e.printStackTrace();
    } catch (IOException e) {
     e.printStackTrace();
    }
   }
  }
     
     
     }

 

 

5.高亮

package cn.itcast.lucene;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import cn.itcast.lucene.RearchResult;

 

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.search.highlight.Formatter;
import org.apache.lucene.search.highlight.Fragmenter;
import org.apache.lucene.search.highlight.Highlighter;
import org.apache.lucene.search.highlight.QueryScorer;
import org.apache.lucene.search.highlight.Scorer;
import org.apache.lucene.search.highlight.SimpleFragmenter;
import org.apache.lucene.search.highlight.SimpleHTMLFormatter;
import org.junit.Test;

 

public class HightLight {
  private static Analyzer analyzer=new StandardAnalyzer();
    static String dir="c:/index/";
 
    @Test
 public static RearchResult search(Query query, int firstResult, int maxResults) {
  IndexSearcher indexSearcher = null;
  try {
   indexSearcher = new IndexSearcher(dir);

   TopDocs topDocs = indexSearcher.search(query, null, firstResult + maxResults);
   List<Document> docs = new ArrayList<Document>();

   // <b> </b>
   Formatter formatter = new SimpleHTMLFormatter("<font color='red'>", "</font>");
   Scorer scorer = new QueryScorer(query);
   Highlighter highlighter = new Highlighter(formatter, scorer);

   Fragmenter fragmenter = new SimpleFragmenter(50);
   highlighter.setTextFragmenter(fragmenter);

   int end = Math.min(firstResult + maxResults, topDocs.totalHits);
   for (int i = firstResult; i < end; i++) {
    ScoreDoc scoreDoc = topDocs.scoreDocs[i];
    Document doc = indexSearcher.doc(scoreDoc.doc);

    // 如果这个field中没有出现搜索的关键字,则返回null
    String ht = highlighter.getBestFragment(analyzer, "content", doc.get("content"));
    if (ht == null) {
     String value = doc.get("content");
     ht = value.length() > 50 ? value.substring(0, 50) : value;
    }
    doc.getField("content").setValue(ht);

    docs.add(doc);
   }

   return new RearchResult(topDocs.totalHits, docs);
  } catch (Exception e) {
   throw new RuntimeException(e);
  } finally {
   try {
    indexSearcher.close();
   } catch (IOException e) {
    e.printStackTrace();
   }
  }
 }
   
   public static void main(String[] args) {
  Term term=new Term("content","document");
  Query query=new TermQuery(term);
  RearchResult result=search(query,0,11);
  for(Document doc : result.getDocuments()){
   System.out.println(doc);
  }
  }
}

原创粉丝点击