user34537
user34537

Reputation:

How do i delete/update a doc with lucene?

I am creating a tagging system for my site

I got the basics of adding a document to lucene but i can seem to figure out how to delete a document or update one when the user changes the tags of something. I found pages that say use the document index and i need to optimize before effect but how do i get the document index? Also i seen another that said to use IndexWriter to delete but i couldnt figure out how to do it with that either.

I am using C# asp.net and i dont have java install on that machine

Upvotes: 7

Views: 12655

Answers (2)

Mikos
Mikos

Reputation: 8563

What version of Lucene are you using? The IndexWriter class has an update method that lets you update (BTW an update under the hood is really a delete followed by an add). You will need to have some identifier (such as as document id) which lets you update. When you index the document, add a unique document identifier such as a URL, a counter etc. Then the "Term" will be the ID of the document you wish to update. For example using URL you can update thus:

IndexWriter writer = ...
writer.update(new Term("id","http://somedomain.org/somedoc.htm"), doc); 

Upvotes: 12

Dan Head
Dan Head

Reputation: 2752

You need an IndexReader to delete a document, I'm not sure about the .net version but the Java and C++ versions of the Lucene API have an IndexModifier class that hides the differences between IndexReader and IndexWriter classes and just uses the appropriate one as you call addDocument() and removeDocument().

Also, there is no concept of updating a document in Lucene you have to remove it an them re-add it again. In order to do this you will need to make sure that every document has a unique stored id in the index.

Upvotes: 3

Related Questions