1

I am new to In-memory indexing concept. We are currently using lucene file indexing technology for indexing records and searching. Could you please tell me how we can implement In-memory indexing and what is the advantage of this type of indexing over lucene file indexing?

vishnu
  • 165
  • 1
  • 16

1 Answers1

4

Check this answer for Comparison Between FSDirectory And IN-Memory RAMDIrectory Comparison

How to USE 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));

Same Way you can use FSDirectory.with some minor changes.

Some Additional methods to copy index from 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();
}
}
Prakhar Nigam
  • 678
  • 3
  • 15