1. Modify Index
The index is updated to delete the add, it is recommended to use this method to update the needs, and to ensure the implementation of existing update the index, you can check out, update records to determine the existence of an update operation.
If you update the index of the target document object does not exist, do add.
/ ** * Modify the index database * / @Test public void testUpdateIndex () throws IOException { // create analyzer (tokenizer) StandardAnalyzer standard word breaker, the English word good effect, Chinese is the word word Analyzer Analyzer = new new StandardAnalyzer ( ); // create IndexWriterConfig class configuration information, specify word segmentation word used IndexWriterConfig config = new new IndexWriterConfig (Analyzer); // create the Directory object, the statement index database storage location Directory dir = FSDirectory.open (Paths.get ( " D : \\ Lucene " )); // create IndexWriter writing object, config initialize the object location and use of specified written IndexWriter IndexWriter = new newIndexWriter (dir, config); // need to change the contents of the Document the Document = new new the Document (); // create a domain object, and the object in the document document.add ( new new StringField ( "the above mentioned id", "100 000 003 140" , TextField. Store.YES)); document.add ( new new TextField ( "name", "Vivo" , TextField.Store.YES)); // perform the update, will all eligible Document delete, add it again. indexWriter.updateDocument ( new new Term ( "ID", "100 000 003 140" ), Document); // release resources indexWriter.close (); }
2. Delete Index
/ ** * Remove index database * / @Test public void testDeleteIndex () throws IOException { // create analyzer (tokenizer) StandardAnalyzer standard word breaker, the English word good effect, Chinese is the word word Analyzer Analyzer = new new StandardAnalyzer ( ); // create IndexWriterConfig class configuration information, specify word segmentation word used IndexWriterConfig config = new new IndexWriterConfig (Analyzer); // create the Directory object, the statement index database storage location Directory dir = FSDirectory.open (Paths.get ( " D : \\ Lucene " )); // create IndexWriter writing object, config initialize the object location and use of specified written IndexWriter IndexWriter = new new IndexWriter (dir, config); //According Term delete the index database // indexWriter.deleteDocuments (new new Term ( "the above mentioned id", "100 000 003 140")); // delete all indexes (caution) indexWriter.deleteAll (); // release resources indexWriter.close (); }