Eric Nicholson
Eric Nicholson

Reputation: 4123

How do I delete old documents from Lucene/Lucene.NET

What is the idiomatic way to delete old documents from a Lucene Index?

I have a date field (YYYYMMddhhmmss) on all of the documents, and I'd like to remove anything more than a day old (for example).

Should I perform a filtered search or enumerate through the IndexReader's documents?

I'm sure the question is the same regardless of which platform Lucene is running on.

Thanks!

Upvotes: 3

Views: 1336

Answers (2)

Shashikant Kore
Shashikant Kore

Reputation: 5052

You could try using low-level APIs of Lucene.

Get Term Enumerator from index with the term "YYYY". Iterate of the term enumerator to get terms. If the term's text doesn't with current date (or previous date), call IndexReader.deleteDocuments(term) with that term.

Since you are not using Query object, you will not get search related exception.

Upvotes: 2

synhershko
synhershko

Reputation: 4492

Searching for YYYYMMdd* should work as currently dates are stored as text strings. Once you have the results, you could use IndexReader.delete to remove the docs you're not interested in. That seems to me the best way to achieve this.

Upvotes: 3

Related Questions