Jim*_*mmy 14 lucene lucene.net
我什么时候应该使用Lucene的RAMDirectory?与其他存储机制相比,它有哪些优势?最后,我在哪里可以找到一个简单的代码示例?
Che*_*ian 17
当您不想永久存储索引数据时.我用它来测试.将数据添加到RAMDirectory,在RAMDir中进行单元测试.
例如
public static void main(String[] args) {
try {
Directory directory = new RAMDirectory();
Analyzer analyzer = new SimpleAnalyzer();
IndexWriter writer = new IndexWriter(directory, analyzer, true);
Run Code Online (Sandbox Code Playgroud)
要么
public void testRAMDirectory () throws IOException {
Directory dir = FSDirectory.getDirectory(indexDir);
MockRAMDirectory ramDir = new MockRAMDirectory(dir);
// close the underlaying directory
dir.close();
// Check size
assertEquals(ramDir.sizeInBytes(), ramDir.getRecomputedSizeInBytes());
// open reader to test document count
IndexReader reader = IndexReader.open(ramDir);
assertEquals(docsToAdd, reader.numDocs());
// open search zo check if all doc's are there
IndexSearcher searcher = new IndexSearcher(reader);
// search for all documents
for (int i = 0; i < docsToAdd; i++) {
Document doc = searcher.doc(i);
assertTrue(doc.getField("content") != null);
}
// cleanup
reader.close();
searcher.close();
}
Run Code Online (Sandbox Code Playgroud)
通常情况下,如果使用RAMDirectory可以解决问题,那么与其他人一起使用它会很好.即永久存储您的索引.
替代它是FSDirectory.在这种情况下,您将不得不处理文件系统权限(这对RAMDirectory无效)
从功能上讲,RAMDirectory与FSDirectory相比没有明显的优势(除了RAMDirectory明显比FSDirectory更快的事实).他们都服务于两种不同的需求.
非常类似于RAM和硬盘.
我不确定RAMDirectory如果超出内存限制会发生什么.我除了一个
OutOfMemoryException:System.SystemException
抛出.