如何在LuceneNet中使用indexWriter删除文档 [英] How To Delete a Document using indexWriter in LuceneNet

查看:76
本文介绍了如何在LuceneNet中使用indexWriter删除文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在控制器中调用了此方法,该方法必须删除特定的Document.我在一些文章中读到,删除文档的最佳方法是使用IndexWriter.但是我不能使它工作.这是我的代码

I have this method that I invoke in my controller that have to delete a specific Document. I read in some articles that the best way to delete a Document is using a IndexWriter. But I can't make it work. This is my code

我的索引:

    var article1 = new Document();
               article1.Add(new Field("Id", "1", Field.Store.YES, Field.Index.ANALYZED));
               article1.Add(new Field("Author", "Author", Field.Store.YES, Field.Index.ANALYZED));
               article1.Add(new Field("Title", "Title", Field.Store.YES, Field.Index.ANALYZED));

  var directory = FSDirectory.Open(new DirectoryInfo(IndexRoute));
           var analyzar = new StandardAnalyzer(Version.LUCENE_29);

           var writer = new IndexWriter(directory, analyzar, true,  IndexWriter.MaxFieldLength.LIMITED);

           writer.AddDocument(article1);
           writer.Optimize();
           writer.Commit();
           writer.Close();

方法删除:

public void Delete(string id)
{
    var directory = FSDirectory.Open(new DirectoryInfo(IndexRoute));
    var analyzar = new StandardAnalyzer(Version.LUCENE_29);
    var writer = new IndexWriter(directory, analyzar, true, IndexWriter.MaxFieldLength.LIMITED);
    var term = new Term("Id", id);
    writer.DeleteDocuments(term);
    writer.Optimize();
    writer.Commit();
    writer.Close();
}

控制器中调用删除" void的方法:

The method in the controller that invoke the "delete" void:

  public ActionResult Delete()
    {
        _carService.Delete("1");
        return RedirectToAction("Index", "Home");
    }

所以我找不到我的错误,请帮忙...

So I can't find my error,a little help please...

推荐答案

在为以下删除方法构建IndexWriter时:

When you build your IndexWriter for the delete method like that:

new IndexWriter(directory, analyzar, true, IndexWriter.MaxFieldLength.LIMITED);

您要为create参数指定true,该参数会用一个空的索引覆盖现有索引,并删除所有文档.

You are specifying true for the create parameter, which overwrites the existing index with an empty one, deleting all your documents.

这篇关于如何在LuceneNet中使用indexWriter删除文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆