Lucene對於索引的刪改

    我們創建了索引, 當然我還需要對索引的進行修改, 刪除. 下面的一段代碼就是我們對於索引的修改和刪除啦. 在實際開發中這個纔是用的最多的. 哈…..

package com.zero.lucene;

import java.io.File;
import java.io.FileReader;
import java.nio.file.Paths;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.TextField;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.Term;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;

/**
 * 對索引的增刪改
 * @author samuel
 * @date 2016-05-02
 */
public class ChangeIndex {

    /**
     * 建立索引
     * @throws Exception
     */
    public void index() throws Exception {
        IndexWriter indexWriter = getIndexWriter();
        String dataDir = "";        // 需要索引文件的目錄
        File[] files = new File(dataDir).listFiles();
        for (File file : files) {
            Document doc = new Document();
            doc.add(new TextField("title", file.getName(), Store.YES));
            doc.add(new TextField("contents", new FileReader(file)));
            indexWriter.addDocument(doc);
        }
        int maxDoc = indexWriter.maxDoc();
        int numDocs = indexWriter.numDocs();
        System.out.println("maxDoc===>" + maxDoc);
        System.out.println("numDoc===>" + numDocs);
        indexWriter.close();
    }

    /**
     * 標記刪除
     * @throws Exception
     */
    public void delBeforeMerge() throws Exception {
        IndexWriter indexWriter = getIndexWriter();
        // 標記刪除
        indexWriter.deleteDocuments(new Term("title", "pwd.txt"));
        indexWriter.commit();
        indexWriter.close();
    }

    /**
     * 刪除索引
     * 需要消耗大量的IO流
     * @throws Exception
     */
    public void delAfterForce() throws Exception {
        IndexWriter indexWriter = getIndexWriter();
        // 刪除的條件(標記刪除)
        indexWriter.deleteDocuments(new Term("title", "pwd.txt"));
        // 馬上刪除
        indexWriter.forceMergeDeletes();
        indexWriter.commit();
        indexWriter.close();
    }

    /**
     * 修改索引
     * 根據條件查詢索引 如果沒有查詢到結果就執行添加 反之就更新
     * @throws Exception
     */
    public void updateIndex() throws Exception {
        Document doc = new Document();
        doc.add(new TextField("title", "密碼.txt", Store.YES));

        IndexWriter indexWriter = getIndexWriter();
        // Term : 查詢特定的值 相當於查詢 title = pwd.txt的Document
        indexWriter.updateDocument(new Term("title", "pwd.txt"), doc);
        indexWriter.commit();
        indexWriter.close();
    }
    private IndexWriter getIndexWriter() throws Exception {
        String dir = ""; // 索引存儲的位置
        Directory direcory = FSDirectory.open(Paths.get(dir));
        Analyzer analyzer = new StandardAnalyzer();
        IndexWriterConfig config = new IndexWriterConfig(analyzer);
        IndexWriter indexWriter = new IndexWriter(direcory, config);
        return indexWriter;
    }
}

    注意點:
        刪除是有兩種狀態的, 1: 標記刪除 2:立即刪除
        修改也是有兩種形式的. 沒有就增加, 有就修改

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章