WriteLucene
packagecom.yulong.lucene.biz;
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.IndexWriter;
importcom.yulong.lucene.core.LuceneContacts;
importcom.yulong.lucene.util.RequestUtil;
publicclassWriteLucene
{
/**
*LOG.
*/
privatestaticfinalLoggerLOG=Logger.getLogger(WriteLucene.class);
//当前写入的文件
privateStringpath="";
publicWriteLucene(Stringpath)
{
this.path=RequestUtil.ChangePath(path);
this.path=this.path+"index/";
}
/**
*写入索引数据
*
*@parammap
*/
publicbooleanwrite(List<Map<String,String>>list)
{
IndexWriteriwriter=null;
try
{
iwriter=LuceneContacts.iwpool.get(this.path,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!="")
{
if(key.equalsIgnoreCase("guid"))
doc.add(newField(key,value,Field.Store.YES,Field.Index.NOT_ANALYZED));
elseif(key.equalsIgnoreCase("content"))
doc.add(newField(key,value,Field.Store.YES,Field.Index.ANALYZED));
else
doc.add(newField(key,value,Field.Store.YES,Field.Index.ANALYZED));
}
}
if(map.size()>0)
{
iwriter.addDocument(doc);
}
}
iwriter.close();
}
catch(Exceptione)
{
e.printStackTrace();
returnfalse;
}
LuceneContacts.iwpool.over(this.path,this.toString());
returntrue;
}
/**
*写入索引数据
*
*@parammap
*/
publicbooleanwrite(Map<String,String>map)
{
IndexWriteriwriter=null;
try
{
iwriter=LuceneContacts.iwpool.get(this.path,this.toString());
Documentdoc=newDocument();
for(Iterator<String>it=map.keySet().iterator();it.hasNext();)
{
Stringkey=RequestUtil.VailStr(it.next(),"");
Stringvalue=RequestUtil.VailStr(map.get(key),"");
if(key!="")
{
if(key.equalsIgnoreCase("guid"))
doc.add(newField(key,value,Field.Store.YES,Field.Index.NOT_ANALYZED));
elseif(key.equalsIgnoreCase("content"))
doc.add(newField(key,value,Field.Store.YES,Field.Index.ANALYZED));
else
doc.add(newField(key,value,Field.Store.YES,Field.Index.NOT_ANALYZED));
}
}
if(map.size()>0)
{
iwriter.addDocument(doc);
iwriter.close();
}
}
catch(Exceptione)
{
e.printStackTrace();
returnfalse;
}
LuceneContacts.iwpool.over(this.path,this.toString());
returntrue;
}
}