笑问苍天
笑问苍天

Reputation: 485

Lucene.NET Delete.Documents not working as expected for BooleanQuery

When I try to delete the documents via boolean query with Occur.Should, nothing got deleted

var query = new BooleanQuery();
query.Add(new TermQuery(new Term("Field", "A")), Occur.Should);
query.Add(new TermQuery(new Term("Field", "B")), Occur.Should);
writer.DeleteDocuments(query);
query.Clauses.Clear();
writer.Commit();

I expected the documents with "Field" value "A" or "B" will be deleted from index, but when I fetch the documents from index, still got "A" and "B" there, did I use it in the wrong way?

Upvotes: 1

Views: 144

Answers (1)

笑问苍天
笑问苍天

Reputation: 485

Turns out, I should not call Clauses.Clear() before commit. That process will clean all boolean clauses. Delete the clear part, everything works fine.

var query = new BooleanQuery();
query.Add(new TermQuery(new Term("Field", "A")), Occur.Should);
query.Add(new TermQuery(new Term("Field", "B")), Occur.Should);
writer.DeleteDocuments(query);
writer.Commit();

Upvotes: 1

Related Questions