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:立即删除
        修改也是有两种形式的. 没有就增加, 有就修改

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值