1
votes

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...

2
do you commit your changes and refresh IndexSearchers after you delete the documents?Jf Beaulac
@JfBeaulac yup indexWriter.Commit();John x
Does UpdateDocument cause the same duplication behavior, or does it just not do anything?itsadok
@itsadok it causes the same behavior, first time if the index is not created it creates one but the next time when it should update the index it creates another (redundant)document...John x
the ListID is stored but not indexed should i index it too? its the primary key...John x

2 Answers

4
votes

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.

0
votes

You can do it by simply:

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