Update document is another important operation as part of indexing process. This operation is used when already indexed contents are updated and indexes become invalid. This operation is also known as re-indexing.
We update Document(s) containing Field(s) to IndexWriter where IndexWriter is used to update indexes.
We will now show you a step-wise approach and help you understand how to update document using a basic example.
Follow this step to update a document to an index −
Step 1 − Create a method to update a Lucene document from an updated text file.
Follow these steps to create an IndexWriter −
Step 1 − IndexWriter class acts as a core component which creates/updates indexes during the indexing process.
Step 2 − Create object of IndexWriter.
Step 3 − Create a Lucene directory which should point to location where indexes are to be stored.
Step 4 − Initialize the IndexWriter object created with the index directory, a standard analyzer having version information and other required/optional parameters.
Following are the two ways to update the document.
updateDocument(Term, Document) − Delete the document containing the term and add the document using the default analyzer (specified when index writer is created).
updateDocument(Term, Document,Analyzer) − Delete the document containing the term and add the document using the provided analyzer.
To test the indexing process, let us create a Lucene application test.
This class is used to provide various constants to be used across the sample application.
This class is used as a .txt file filter.
This class is used to index the raw data so that we can make it searchable using the Lucene library.
This class is used to test the indexing capability of the Lucene library.
Here, we have used 10 text files from record1.txt to record10.txt containing names and other details of the students and put them in the directory E:\Lucene\Data. Test Data. An index directory path should be created as E:\Lucene\Index. After running this program, you can see the list of index files created in that folder.
Once you are done with the creation of the source, the raw data, the data directory and the index directory, you can proceed with the compiling and running of your program. To do this, keep the LuceneTester.Java file tab active and use either the Run option available in the Eclipse IDE or use Ctrl + F11 to compile and run your LuceneTester application. If your application runs successfully, it will print the following message in Eclipse IDE's console −
Once you've run the above program successfully, you will have the following content in your index directory −
Lucene Related Interview Questions
|Core Java Interview Questions||MySQL Interview Questions|
|JSP Interview Questions||Hadoop Interview Questions|
|Quark Xpress Interview Questions||Hibernate Interview Questions|
|Sqoop Interview Questions||Apache Solr Interview Questions|
|HBase Interview Questions||MongoDB Interview Questions|
|Spring MVC Framework Interview Questions||Hadoop Testing Interview Questions|
All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd
Wisdomjobs.com is one of the best job search sites in India.