为了提高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;
}
}