RebuildLucene
packagecom.yulong.lucene.biz;
importjava.io.IOException;
importjava.util.Iterator;
importjava.util.List;
importjava.util.Map;
importorg.apache.log4j.Logger;
importorg.apache.lucene.document.Document;
importorg.apache.lucene.document.Field;
importorg.apache.lucene.index.CorruptIndexException;
importorg.apache.lucene.index.IndexWriter;
importcom.yulong.lucene.core.LuceneContacts;
importcom.yulong.lucene.util.RequestUtil;
/**
*LUCENE重新写入大批量索引
*
*@authorfyh
*/
publicclassRebuildLucene
{
privatestaticfinalLoggerLOG=Logger.getLogger(RebuildLucene.class);
privateStringpath="";
publicRebuildLucene(Stringpath)
{
this.path=RequestUtil.ChangePath(path);
}
/**
*写入索引数据
*
*@parammap
*/
publicbooleanwrite1(List<Map<String,String>>list)
{
IndexWriteriwriter=null;
try
{
iwriter=LuceneContacts.iwpool.get(path+"rebuild/",this.toString());
for(Map<String,String>map:list)
{
Documentdoc=newDocument();
for(Iterator<String>it=map.keySet().iterator();it.hasNext();)
{
Stringkey=RequestUtil.VailStr(it.next(),"");
Stringvalue=RequestUtil.VailStr(map.get(key),"");
if(key!="")
doc.add(newField(key,value,Field.Store.YES,Field.Index.ANALYZED));
}
if(map.size()>0)
{
iwriter.addDocument(doc);
}
}
//iwriter.close();
LOG.info("LUCENE重新写入大批量索引成功!");
}
catch(Exceptione)
{
e.printStackTrace();
returnfalse;
}
finally
{
try
{
if(iwriter!=null)
{
iwriter.close();
}
}
catch(CorruptIndexExceptione)
{
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
catch(IOExceptione)
{
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
}
returntrue;
}
publicbooleanwrite(List<Map<String,String>>list)
{
booleanres=this.write1(list);
if(res)
{
res=newMoveLuceneRebuild().move(path);//替换当前重编译后的索引
}
returnres;
}
}