我有这个方法,我在我的控制器中调用,必须删除一个特定的文档.我在一些文章中读到删除文档的最佳方法是使用IndexWriter.但我不能让它发挥作用.这是我的代码 我的索引: var article1 =
我的索引:
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的方法:
public ActionResult Delete() { _carService.Delete("1"); return RedirectToAction("Index", "Home"); }
所以我找不到我的错误,请帮助…
当您为删除方法构建IndexWriter时,如下所示:new IndexWriter(directory, analyzar, true, IndexWriter.MaxFieldLength.LIMITED);
您为create参数指定true,该参数将使用空索引覆盖现有索引,并删除所有文档.