1,按照编号删除
public void deleteDoc(String indexDir) throws Exception{
IndexReader reader = IndexReader.open(indexDir);
reader.delete(1);
reader.close();
}
2,根据term来删除IndexReader reader = IndexReader.open(indexDir);
reader.delete(1);
reader.close();
}
public void deleteDocWithTerm(String indexDir) throws Exception{
IndexReader reader = IndexReader.open(indexDir);
reader.delete(new Term("city","beijing"));
reader.close();
}
3,取消删除IndexReader reader = IndexReader.open(indexDir);
reader.delete(new Term("city","beijing"));
reader.close();
}
public void undeleteDoc(String indexDir) throws Exception{
IndexReader reader = IndexReader.open(indexDir);
reader.delete(1);
reader.undeleteAll();
reader.close();
}
4,删除后优化IndexReader reader = IndexReader.open(indexDir);
reader.delete(1);
reader.undeleteAll();
reader.close();
}
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();
}
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,把索引的内容直接读入内存
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();
}
RAMDirectory ramDir = new RAMDirectory(indexDir);