问题描述
我正在使用Lucene.Net(版本2.9.4.1)实现一个简单的搜索模块.我正在尝试使用以下代码删除索引中存在的文档
I am using Lucene.Net (version 2.9.4.1) to implement a simple search module. I'm trying to delete the document if it exists in the index using the following code,
var analyzer = new StandardAnalyzer(Version.LUCENE_29);
var indexWriter = new IndexWriter(
LuceneSearch._luceneDir,
analyzer,
IndexWriter.MaxFieldLength.UNLIMITED);
var searchQuery = new TermQuery(new Term("ListID", listingDoc.Get("ListID")));
indexWriter.DeleteDocuments(searchQuery);
其中listingDoc
是Document
类型,我试图删除文档(如果存在),然后将其再次添加到索引中,添加部分工作正常,但是删除部分不起作用,因为该文档是如果存在,则不删除.因此,如果我搜索一个词并匹配它,则会显示多次...请指出我在这里做错了什么
where listingDoc
is of type Document
i'm trying to delete the document if it exists and then add it again to the index, the adding part works fine but the deleting part is not working that is the document is not deleted if it exists. Therefore if i search a term and it matches it is shown multiple times... Please point out what iam doing wrong here
我正在使用ASP.Net MVC3和Entity Framework4.每次记录更新时,我都打算更新索引,但它会被复制.当我搜索它时,我会得到两次或三次的结果,这取决于我进行更新的次数.
I am using ASP.Net MVC3 and Entity Framework4. every time a record is updated i intend to update the index but instead its been duplicated. and when i search it i get the result twice or thrice depending upon the number of times i do the update.
我尝试使用indexWriter.UpdateDocument(args);
无济于事...
I tried using indexWriter.UpdateDocument(args);
to no avail...
推荐答案
在调试删除操作时,有时使用与delete命令相同的参数进行搜索可能非常有用,以查看要删除的内容.
When debugging deletions it can sometimes be useful to perform a search with the same parameters as the delete command, to see exactly what is going to get deleted.
如果您正在执行deleteDocuments(query)
,则应使用这样的IndexSearcher
:
If you're doing a deleteDocuments(query)
you should use an IndexSearcher
like this:
IndexSearcher is = new IndexSearcher(indexWriter.GetReader());
TopDocs topDocs = is.Search(query, 100);
并查看topDocs中的内容.我怀疑您会发现查询没有返回任何结果.
And see what you get in the topDocs. I suspect you'll find that the query doesn't return any results.
这篇关于无法删除Lucene索引中的现有文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!