I am following closely to a Lucene Tutorial with Lucene 3.6.
I am able to create and perform searches on Document objects, but I would like to get back the original objects I used to create the Documents. Unfortunately, Lucene seems to be serialising/deserialising the Documents, so I have not been able to create a lookup map between them.
How do I keep a relationship between Lucene’s Documents and my Objects? Is there a preferred Lucene way of doing this?
I should note that the tutorial didn’t work out of the box for me, I had to add a call to IndexWriter.commit() after creating/attaching the Documents and I also have to make calls to IndexWriterConfig.setMaxBufferedDocs() and IndexWriterConfig.setRAMBufferSizeMB() with big numbers to stop Lucene looking on the hard drive.
First, you need a unique reference to the original object. If your objects are rows in a database, you might go with the primary key, let’s assume, it is a unique
ID.Secondly, when creating the searchable
Documents, just add a field likeYou can later retrieve this field from the found documents and retrieve the original document (database entry) based on the id.
If you may have different document types, e.g. database entries and PDFs, just save the document type in the same manner, allowing you to handle the different types differently.