Unable to delete the existing document in lucene index

筅森魡賤 提交于 2020-01-15 12:47:32

问题


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);

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

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.

I tried using indexWriter.UpdateDocument(args); to no avail...


回答1:


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.

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);

And see what you get in the topDocs. I suspect you'll find that the query doesn't return any results.




回答2:


You can do it by simply:

    Query query = queryParser.parse("My Query!");
    writer.deleteDocuments(query);


来源:https://stackoverflow.com/questions/11286712/unable-to-delete-the-existing-document-in-lucene-index

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!