lucene索引的删除和更新

Lucene索引的删除和更新

删除和更新和新增一样,也是通过IndexWriter 对象来操作的,IndexWrite对象的deleteDocuments ()方法用于实现索引的删除,updateDocument()方法用于实现索引的更新。

删除Lucene索引

删除索引的代码如下,该示例实现了根据Term来删除单个或多个Document,删除title中包含关键词“美国”的文档:

import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import org.apache.lucene.analysis.Analyzer;
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;
import tup.lucene.ik.IKAnalyzer6x;
public class DeleteIndex {
    public static void main(String[] args) {
    // 删除title中含有关键词“美国”的文档
    deleteDoc("title", "美国");
}
public static void deleteDoc(String field, String key) {
    Analyzer analyzer = new IKAnalyzer6x();
    IndexWriterConfig icw = new IndexWriterConfig(analyzer);
    Path indexPath = Paths.get("indexdir");
    Directory directory;
    try {
        directory = FSDirectory.open(indexPath);
        IndexWriter indexWriter = new IndexWriter(directory, icw);
        indexWriter.deleteDocuments(new Term(field, key));
        indexWriter.commit();
        indexWriter.close();
        System.out.println("删除完成!");
    } catch (IOException e) {
        e.printStackTrace();
    }
}
}

除此之外IndexWriter还提供了以下方法:

  • DeleteDocuments(Query query):根据Query条件来删除单个或多个Document
  • DeleteDocuments(Query[] queries):根据Query条件来删除单个或多个Document
  • DeleteDocuments(Term term):根据Term来删除单个或多个Document
  • DeleteDocuments(Term[] terms):根据Term来删除单个或多个Document
  • DeleteAll():删除所有的Document

更新Lucene索引

使用IndexWriter进行Document删除操作时,文档并不会立即被删除,而是把这个删除动作缓存起来,当IndexWriter.Commit()或IndexWriter.Close()时,删除操作才会被真正执行。
索引更新操作实质上是先删除索引,再重新建立新的文档,示例代码如下:

import java.nio.file.Path;
import java.nio.file.Paths;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.Term;
mport org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import tup.lucene.ik.IKAnalyzer6x;
public class UpdateIndex {
public static void main(String[] args) {
    Analyzer analyzer = new IKAnalyzer6x();
    IndexWriterConfig icw = new IndexWriterConfig(analyzer);
    Path indexPath = Paths.get("indexdir");
    Directory directory;
    try {
        directory = FSDirectory.open(indexPath);
        IndexWriter indexWriter = new IndexWriter(directory, icw);
        Document doc = new Document();
        doc.add(new TextField("id","2", Store.YES));
        doc.add(new TextField("title", " 北京大学开学迎来4380名新生",    
             Store.YES));
        doc.add(new TextField("content", " 昨天,北京大学迎来4380名来自全  
     国各地及数十个国家的本科新生。其中,农村学生共700余名,为近年最 
     多...", Store.YES));
        indexWriter.updateDocument(new Term("title", "北大"), doc);
        indexWriter.commit();
        indexWriter.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
}

上面的代码中我们新建了一个IndexWriter对象和Document对象,通过updateDocument()方法完成更新操作。Term对象用于定位文档,查找title中含有“北大”的文档,然后用新的文档替换原文档,这样就完成了索引的更新操作。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

esc_ai

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值