Search engines Learning (Five) Lucene indexing operation

One, code analysis

/ ** 
 * Getting Lucene 
 * Operating index 
 * / 
public  class ManageIndex { 

    public IndexWriter getIndexWriter () throws Exception {
         // set index database location 
        Directory Directory = FSDirectory.open ( new new File ( "E: \\ \\ zhanghaoBF luceneSolr \ \ indexLibrary " ) .toPath ()); 
        Analyzer Analyzer = new new StandardAnalyzer (); // create word object (the official recommended standard tokenizer) 
        indexWriterConfig indexWriterConfig = new new indexWriterConfig (Analyzer); // word set using 
        return  new new IndexWriter (directory, indexWriterConfig);// index object 
    } 

    / ** 
     * Full delete ( PS: index inside the document will be deleted) 
     * 
     * @throws Exception
      * / 
    @Test 
    public  void deleteAllIndex () throws Exception { 
        IndexWriter IndexWriter = getIndexWriter (); // Gets stream object index 
        indexWriter.deleteAll (); // delete all index 
        indexWriter.close (); // off flow 
    } 

    / ** 
     * conditional deletion ( PS: index inside the document will be deleted) 
     * 
     * @throws Exception
      * /
    @Test 
    public  void deleteIndex () throws Exception { 
        the IndexWriter IndexWriter = getIndexWriter (); // get a stream object index 
        Query Query = new new TermQuery ( new new Term ( "fileContent", "Lucene")); // the PS: TermQuery is accurate matching 
        indexWriter.deleteDocuments (Query); // conditional delete an index 
        indexWriter.close (); // off flow 
    } 

    / ** 
     * Review ( the PS: after removing the first added, and the same inside the database is not modified, a distinction) 
     * 
     * @throws Exception
      * / 
    @Test 
    public void updateIndex () throws Exception {
         // Get index stream object 
        the IndexWriter IndexWriter = getIndexWriter (); 

        // build a document object 
        the Document DOC = new new the Document (); 
        doc.add ( new new the TextField ( "fileName", "new file name " , Field.Store.YES)); 
        doc.add ( new new TextField (" fileContent "," new file content " , Field.Store.YES)); 

        // call update, delete the first qualifying index off (including the term and document exists index library), and then just added the document object is added to the index database 
        indexWriter.updateDocument ( new new Term ( "fileName", "is"), doc);// conditionally remove the index, then add the new index
         // PS:Deleted document will still occupy the corresponding document ID, the new document at the last (and when the database ID from the growing delete add one more reason) 

        // off flow 
        indexWriter.close (); 
    } 
}

Second, note

1, delete the time, the corresponding index at the library ID Term and documents will be deleted .

2, modify the operation is actually first qualifying term and documentation will be deleted , and then add the new document.

3, delete the document, the document ID will not be released, or occupied.

4, the flow must remember to run off.

Guess you like

Origin www.cnblogs.com/riches/p/11450606.html