我是内存索引概念的新手。我们目前正在使用lucene文件索引技术来索引记录和搜索。 您能否告诉我如何实现内存索引以及这种索引相对于lucene文件索引的优势是什么?
答案 0 :(得分:4)
检查此答案以了解FSDirectory和IN-Memory 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.with一些小的改动。
从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();
}
}