检查此答案以比较FSDirectory和内存中的RAMDIrectory
Comparison
如何使用RAMDirectory:
RAMDirectory idx = new RAMDirectory();
IndexWriterConfig iwc = new IndexWriterConfig(analyzer);
iwc.setOpenMode(OpenMode.CREATE_OR_APPEND);
IndexWriter writer = new IndexWriter(idx, iwc);
Iterator<Map.Entry<String, String>> it = inputCategoryMap.entrySet().iterator();
while (it.hasNext()) {
Document doc = new Document();
Map.Entry<String, String> pair = it.next();
FieldType contentType = new FieldType();
contentType.setIndexOptions(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS);
contentType.setStored(true);
contentType.setTokenized(true);
contentType.setStoreTermVectors(true);
doc.add(new Field(TITLE, pair.getKey(), TextField.TYPE_STORED));
doc.add(new Field(CONTENT, pair.getValue(),contentType ));
dcmnts.add(doc);
}
writer.addDocuments(dcmnts);
writer.commit();
System.out.println("No of documents added in the index "+writer.maxDoc());
writer.close();
System.out.println("Size consumed by Ram "+idx.ramBytesUsed()+"\nTime Consumed By Lucene "+stopwatch.getTime(TimeUnit.SECONDS));
使用FSDirectory的方法相同。有一些小改动。
从FSDirectory复制索引的其他一些方法。。
private RAMDirectory(FSDirectory dir, boolean closeDir, IOContext context) throws IOException {
this();
for (String file : dir.listAll()) {
if (!Files.isDirectory(dir.getDirectory().resolve(file))) {
copyFrom(dir, file, file, context);
}
}
if (closeDir) {
dir.close();
}
}