在前边的文章中多是用的添加一个document,这里介绍一下其它的文档操作
1,按照编号删除
public void deleteDoc(String indexDir) throws Exception{
IndexReader reader = IndexReader.open(indexDir);
reader.delete(1);
reader.close();
}
2,根据term来删除
public void deleteDocWithTerm(String indexDir) throws Exception{
IndexReader reader = IndexReader.open(indexDir);
reader.delete(new Term("city","beijing"));
reader.close();
}
3,取消删除
public void undeleteDoc(String indexDir) throws Exception{
IndexReader reader = IndexReader.open(indexDir);
reader.delete(1);
reader.undeleteAll();
reader.close();
}
4,删除后优化
public void mergeDoc(String indexDir) throws Exception{
IndexReader reader = IndexReader.open(indexDir);
reader.delete(new Term("city","beijing"));
reader.close();
IndexWriter writer = new IndexWriter(indexDir,new StandardAnalyzer(),true);
writer.optimize();
writer.close();
}
5,把RAMDirectory中的索引合并到FSDirectroy中
public void indexOperator(String indexDir) throws Exception{
FSDirectory fsDir = FSDirectory.getDirectory(indexDir,true);
RAMDirectory ramDir = new RAMDirectory();
IndexWriter fsWriter = new IndexWriter(fsDir,new SimpleAnalyzer(),true);
IndexWriter ramWriter = new IndexWriter(ramDir,new SimpleAnalyzer(),true);
//..ramWriter add doc
fsWriter.addIndexes(new Directory[]{ramDir});
ramWriter.close();
}
6,把索引的内容直接读入内存
RAMDirectory ramDir = new RAMDirectory(indexDir);