Lucene索引的帮助类

为了提高Lucene的性能。尤其是在lucene的IndexReader的创建和销毁都是需要暂时大量的资源。

所以实用Lucene中的NRTManager来管理Lucene。  searcherManager.acquire();不说了,帖代码。

 

package com.gdcn.core.lucene.helper;

import java.io.File;
import java.io.IOException;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.NRTManager;
import org.apache.lucene.search.NRTManagerReopenThread;
import org.apache.lucene.search.SearcherManager;
import org.apache.lucene.search.SearcherWarmer;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;

import com.gdcn.core.lucene.common.LuceneCommon;

/**
 * 创建索引的帮助类
 * @author liu787427876@126.com
 * @date 2014-7-17
 */
public class IndexUtils {
 
 private static IndexUtils instance = null;
 
 private static IndexWriter indexWriter = null;
 private static Analyzer analyzer = null;
 private static NRTManager nrtManager = null;
 private static SearcherManager searcherManager = null;
 private static Directory directory = null;
 
 
 private IndexUtils(){
  
 }
 
 /**
  * 单例模式
  * @return IndexUtils
  * @author liu787427876@126.com
  * @date 2014-7-17
  */
 public static IndexUtils getInstance(){
  if(instance == null){
   init();
   instance = new IndexUtils();
  }
  return instance;
 }
 
 /**
  * 初始化索引信息
  * @author liu787427876@126.com
  * @date 2014-7-17
  */
 private static void init(){
  try {
   directory = FSDirectory.open(new File(LuceneCommon.INDEX_PATH));
   analyzer = (Analyzer)SpringHelper.getBean("analyzer");
   indexWriter = new IndexWriter(directory, new IndexWriterConfig(LuceneCommon.VERSION, analyzer));
   nrtManager = new NRTManager(indexWriter, new SearcherWarmer() {
    @Override
    public void warm(IndexSearcher arg0) throws IOException {
     System.out.println("reopen index");
    }
   });
   
   searcherManager = nrtManager.getSearcherManager(true);
   NRTManagerReopenThread thread = new NRTManagerReopenThread(nrtManager, LuceneCommon.MAX_STALTSEC, LuceneCommon.MIN_STALTSEC);
   thread.setName("NRTManager Reopen Thread");
   thread.setDaemon(true);   //设置为后台线程
   thread.start();
  } catch (IOException e) {
   e.printStackTrace();
  }  
 }
 
 /**
  * 获取IndexSearcher
  * @return
  * @author liu787427876@126.com
  * @date 2014-7-17
  */
 public IndexSearcher getIndexSearcher(){
  return searcherManager.acquire();
 }
 
 /**
  * 重启启动IndexSearcher
  * @author liu787427876@126.com
  * @date 2014-7-17
  */
 public void releaseSearcher(IndexSearcher searcher) {
  try {
   searcherManager.release(searcher);
  } catch (IOException e) {
   e.printStackTrace();
  }
 }
 
 /**
  * 提交索引
  * @author liu787427876@126.com
  * @date 2014-7-17
  */
 public void  commitIndex(){
  try {
   indexWriter.commit();
   indexWriter.forceMerge(3);
  } catch (CorruptIndexException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }
 }
 
 /**
  *
  * @return
  * @author liu787427876@126.com
  * @date 2014-7-17
  */
 public NRTManager getNrtManager(){
  return nrtManager;
 }

 /**
  * 获取分词信息
  * @return
  * @author liu787427876@126.com
  * @date 2014-7-18
  */
 public Analyzer getAnalyzer(){
  return analyzer;
 }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值