Maintenance Lucene- index Library

Field attribute domain

  Whether analysis: whether the content of the domain are word processing, provided that we want to query the contents of domain;

  Whether the index: the word after the analysis or the entire Field Field value index, only the index only search;

    For example: product name, product introduction after analysis of the index; analyze different order number and ID number but also an index, which in the future should be as a query;

  Whether storage: will, Field stored in the document can be obtained from the Field Documenr value stored in the document;

    For example: product name, order number, any future acquired from the Document Field to be stored. Whether storage of standard vertebral: Do you want content displayed to the user;

  Field categories:

    StringField (fieldName, fieldValue, Stroe.YES / NO) is stored as a string data type that contains the index, according to whether the memory Stroe defined, does not pass through the analyzer;

    StroeField (fieldName, fieldValue) supports multiple data types, no analysis, no index, the index Library default save them;

    LongPoint (name, value) will be analyzed, will create an index, but the index will not save them;

    TextField (fieldName, fieldValue, Stroe.YES / NO) analyzes, indexes are created, depending on whether or not to save Stroe;

    

Add index

Copy the code
package com.wn.Document;

import org.apache.lucene.document.Field;
import org.apache.lucene.document.LongPoint;
import org.apache.lucene.document.StoredField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.wltea.analyzer.lucene.IKAnalyzer;

import java.io.File;
import java.io.IOException;

public class AddDocument {
    / * Add an index * /
    public static void main(String[] args)throws IOException {
        Directory directory= FSDirectory.open(new File("E:\\Lucene\\temp\\index").toPath());
        IndexWriterConfig config = new IndexWriterConfig (new IKAnalyzer ());
        // Create a indexwriter objects
        IndexWriter index writer = new IndexWriter (directory, config);
        // Create a Document object
        org.apache.lucene.document.Document document=new org.apache.lucene.document.Document();
        // different document can have a different domain, the same domain document can have the same domain
        document.add(new TextField("fieldName","hhh", Field.Store.YES));
        document.add (new TextField ( "fieldContent", "add new content hhh document", Field.Store.YES));
        // LongPoint create an index
        document.add(new LongPoint("fieldSize",123));
        // storeField store data
        document.add(new StoredField("fieldSize",123));
        // do not need to create an index on the use of storage storeFiled
        document.add(new StoredField("fieldPath","E:\\Lucene\\temp\\hhh.txt"));
        // add an index the document's library
        indexWriter.addDocument (document);
        // close indexwriter
        indexWriter.close ();
    }
}
Copy the code

  The effect of implementation:

    

Index Delete

  1. Remove all

Copy the code
package com.wn.Document;

import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.store.FSDirectory;
import org.wltea.analyzer.lucene.IKAnalyzer;

import java.io.File;
import java.io.IOException;

public class DeleteDocument {
    / * Remove All * /
    public static void main(String[] args)throws IOException {
        IndexWriter indexWrite=new IndexWriter(FSDirectory.open(new File("E:\\Lucene\\temp\\index").toPath()),new IndexWriterConfig(new IKAnalyzer()));
        // Delete Index
        indexWrite.deleteAll ();
        // Close the resource
        indexWrite.close();
    }

}
Copy the code

    Results achieved

      

  2. Delete according to domain or keyword

    Before deleting

      

Copy the code
package com.wn.Document;

import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.store.FSDirectory;
import org.wltea.analyzer.lucene.IKAnalyzer;

import java.io.File;
import java.io.IOException;

/ * Delete * based on the domain and keyword /
public class DeleteDanDocument {
    public static void main(String[] args)throws IOException {
        IndexWriter indexWrite=new IndexWriter(FSDirectory.open(new File("E:\\Lucene\\temp\\index").toPath()),new IndexWriterConfig(new IKAnalyzer()));
        // delete the definition of a condition, define a query object
        Query query=new TermQuery(new Term("fieldName","text01.txt"));
        //删除索引
        indexWrite.deleteDocuments(query);
        //关闭资源
        indexWrite.close();
    }
}
Copy the code

    删除后

      

索引修改

  原理是先删除和添加

  修改前的索引

    

Copy the code
package com.wn.Document;

import org.apache.commons.io.FileUtils;
import org.apache.lucene.document.*;
import org.apache.lucene.index.*;
import org.apache.lucene.search.*;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.wltea.analyzer.lucene.IKAnalyzer;

import java.io.File;
import java.io.IOException;
public class UpdateDocument {
    /*修改索引库*/
    public static void main(String[] args)throws IOException {
        IndexWriter indexWrite=new IndexWriter(FSDirectory.open(new File("E:\\Lucene\\temp\\index").toPath()),new IndexWriterConfig(new IKAnalyzer()));
        // create a document
        Document document=new Document();
        document.add(new TextField("fieldName","hhh", Field.Store.YES));
        document.add(new StoredField("fieldPath","c://hhh.txt"));
        document.add(new LongPoint("fieldSize",456));
        document.add(new StoredField("fieldSize",456));
        document.add (new TextField ( "fieldContent", "fieldName modify the document text search, document replacement, first delete the fieldName two documents full-text search, and then add a new document for the new fileName of", Field.Store .YES));

        // modify a parameter: condition parameters II: modified document value
        indexWrite.updateDocument(new Term("fieldName","全文"),document);
        
        // Close the resource
        indexWrite.close();
    }
}
Copy the code

   The revised index

     

Guess you like

Origin www.cnblogs.com/mayuan01/p/12391866.html