lucene建立索引时候的用到的一些文档和目录操作
在前边的文章中多是用的添加一个document,这里介绍一下其它的文档操作
1,按照编号删除
5,把RAMDirectory中的索引合并到FSDirectroy中
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);