简体   繁体   English

Lucene.NET - 无法使用IndexWriter删除文档

[英]Lucene.NET - Can't delete docs using IndexWriter

I'm taking over a project so I'm still learning this. 我正在接管一个项目,所以我还在学习这个。 The project uses Lucence.NET. 该项目使用Lucence.NET。 I also have no idea if this piece of functionality is correct or not. 我也不知道这个功能是否正确。 Anyway, I am instantiating: 无论如何,我实例化:

var writer = new IndexWriter(directory, analyzer, false);

For specific documents, I'm calling: 对于具体文件,我打电话给:

writer.DeleteDocuments(new Term(...));

In the end, I'm calling the usual writer.Optimize(), writer.Commit(), and writer.Close(). 最后,我正在调用通常的writer.Optimize(),writer.Commit()和writer.Close()。

The field in the Term object is a Guid, converted to a string (.ToString("D")), and is stored in the document, using Field.Store.YES, and Field.Index.NO. Term对象中的字段是Guid,转换为字符串(.ToString(“D”)),并使用Field.Store.YES和Field.Index.NO存储在文档中。

However, with these settings, I cannot seem to delete these documents. 但是,通过这些设置,我似乎无法删除这些文档。 The goal is to delete, then add the updated versions, so I'm getting duplicates of the same document. 目标是删除,然后添加更新的版本,所以我得到相同文档的重复。 I can provide more code/explanation if needed. 如果需要,我可以提供更多代码/解释。 Any ideas? 有任何想法吗? Thanks. 谢谢。

The field must be indexed. 该字段必须编入索引。 If a field is not indexed, its terms will not show up in enumeration. 如果某个字段未编入索引,则其字词不会显示在枚举中。

I don't think there is anything wrong with how you are handling the writer. 我认为你处理作家的方式没有任何问题。

It sounds as if the term you are passing to DeleteDocuments is not returning any documents. 听起来好像传递给DeleteDocuments的术语没有返回任何文档。 Have you tried to do a query using the same term to see if it returns any results? 您是否尝试使用相同的术语进行查询以查看是否返回任何结果?

Also, if your goal is to simple recreate the document, you can call UpdateDocument: 此外,如果您的目标是简单地重新创建文档,则可以调用UpdateDocument:

//     Updates a document by first deleting the document(s) containing term and
//     then adding the new document. The delete and then add are atomic as seen
//     by a reader on the same index (flush may happen only after the add).  NOTE:
//     if this method hits an OutOfMemoryError you should immediately close the
//     writer. See above for details.

You may also want to check out SimpleLucene (http://simplelucene.codeplex.com) - it makes it a bit easier to do basic Lucene tasks. 您可能还想查看SimpleLucene(http://simplelucene.codeplex.com) - 它使基本的Lucene任务变得容易一些。

[Update] Not sure how I missed it but @Shashikant Kore is correct, you need to make sure the field is indexed otherwise your term query will not return anything. [更新]不确定我是如何错过它但@Shashikant Kore是正确的,您需要确保该字段已编入索引,否则您的术语查询将不会返回任何内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM