Lucene小练八(实现了索引和搜索)(二)

2014-11-24 09:19:26 · 作者: · 浏览: 3
tore.YES, Field.Index.ANALYZED));
document.add(new Field("name",names[i],
Field.Store.YES,Field.Index.NOT_ANALYZED_NO_NORMS));
//为数字添加索引
document.add(new NumericField("attach", Field.Store.YES,true).
setIntValue(attachs[i]));
//为日期添加索引
document.add(new NumericField("date", Field.Store.YES,true)
.setLongValue(dates[i].getTime()));//记住要getTime


String str=emails[i].substring(emails[i].lastIndexOf("@")+1);
System.out.println(str);
if(scores.containsKey(str))
{
document.setBoost(scores.get(str));
}else{
document.setBoost(0.5f);
}
writer.addDocument(document);
writer.commit();//提交writer
}
} 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{
try {
writer.close();
} catch (CorruptIndexException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//遍历各种视频
public void query()
{
try {
IndexReader reader=IndexReader.open(directory);
System.out.println("numdocs:"+reader.numDocs());//文档总数
System.out.println("maxDocs:"+reader.maxDoc());//可存储文章做大数目
System.out.println("detelemaxDocs:"+reader.numDeletedDocs());
reader.close();
} catch (CorruptIndexException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//用writer删除索引,但并没有完全删除,可以恢复的
public void delete01()
{
try {
writer=new IndexWriter(directory, new IndexWriterConfig(Version.LUCENE_36,
new StandardAnalyzer(Version.LUCENE_36)));
writer.deleteDocuments(new Term("id","1"));
} 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{
try {
writer.close();
} catch (CorruptIndexException e) {
// TODO Auto-generate