tags:

views:

339

answers:

1

Hi,

I'm using Lucene.NET 2.3.1 with a MultiSearcher.

For testing purposes, I'm indexing a database with about 10000 rows. I have two indexes and I select randomly which to insert each row in. This works correctly, but as Lucene doesn't have update capabilities, I have to test if the row exists (I have an Id field) and then delete it.

I have a List and a List, and each is created with this code:

IndexModifier mod = new IndexModifier(path, new StandardAnalyzer(), create);
m_Modifiers.Add(mod);
m_Readers.Add(IndexReader.Open(path));
m_Searchers.Add(new IndexSearcher(path));

Now the delete code:

Hits results = m_Searcher.Search(new TermQuery(t));

for (int i = 0; i < results.Length(); i++)
{
    DocId = results .Id(i);
    Index = m_Searcher.SubSearcher(DocId);
    DocId = m_Searcher.SubDoc(DocId);

    m_Modifiers[Index].DeleteDocument(DocId);
}

The search is correct and I'm getting results when the row exists. SubSearcher returns always 0 or 1, if Index is 0, SubDoc returns the same ID passed, and if it's 1, then it returns around the number passed minus 5000 times the number of times I have indexed the DB. It seems as if it wasn't deleting anything.

Each time I index the database, I optimize and close the indices, and Luke says it has no pending deletions.

What could be the problem?

A: 

I am not sure what's the end goal of this activity, so pardon if the following solution doesn't meet your requirements.

First, if you want to delete documents, you can use IndexReader, which you have already created. IndexModifier is not required.

Second, you need not find the subsearcher ID and document ID in that subsearcher. You can as well use the top-level MultiReader. I would write the equivalent java code as follows.

IndexReader[] readers = new IndexReader[size];
// Initialize readers
MultiReader multiReader = new MultiReader(readers);

IndexSearcher searcher = new IndexSearcher(multiReader);
Hits results = searcher.search(new TermQuery(t));
for (int i = 0; i < results.length(); i++) {
    int docID = results.id(i);
    multiReader.deleteDocument(docID);
}
multiReader.commit(); // Check if this throws an exception.
multiReader.close();
searcher.close();
Shashikant Kore
Thanks, at last I solved it myself. My problem was really a field which should be UN_TOKENIZED and was TOKENIZED.If I remember correctly, in order to delete with the multiReader, I had to close my Modifier, and as most deletes will be done just as part of a document update, I prefer not to close it, and therefore delete it directly with the Modifier. Am I right, or is there a better solution?