You are viewing a plain text version of this content. The canonical link for it is here.
Posted to java-user@lucene.apache.org by Harini Raghavan <ha...@insideview.com> on 2006/01/08 17:08:33 UTC

Deleting a Document

Hi,
I want to update a document in the lucene index. As mentioned in the 
documentation, I tried to delete the document using IndexReader.delete 
method. But even after I delete the document, I am able to see the 
document when I perform a search. I thought this could be because, I am 
caching the IndexSearcher instance and refreshing it only when required. 
So I tried to refresh the IndexSearcher instance immediately after 
deletion. But even this did not solve the problem. Here is the code:
    public synchronized void deleteDocument(String id) {
        String indexLoc = luceneConfig.getIndexDir();
        Directory fsDir = getIndexDirectory(indexLoc, false);
        IndexReader reader = getIndexReader(fsDir);
        try {
            Term t = new Term(IndexSearchConstants.DOCUMENT_ID, id);
            reader.delete(t);

            //After deleting the document refresh the IndexSearcher instance
            manager.refreshIndexSearcher();
        } catch (IOException e) {
            logger.error("IOException occurred in deleteDocument()", e);
        } finally {
            try {
                reader.close();
            } catch(IOException e) {}
        }
    }
The document seems to be deleted only when I restart the server. Am I 
missing something while deleting the document? When does the document 
actually get deleted from the index?
Thanks,
Harini



---------------------------------------------------------------------
To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
For additional commands, e-mail: java-user-help@lucene.apache.org


Re: Deleting a Document

Posted by Yonik Seeley <ys...@gmail.com>.
Closing the reader that did the deletion causes the deletions to be
flushed to the index.
After that point, any new readers you open will see the deletions. 
Any old index readers that were opened before the deleting reader was
closed will still see the old version of the index (without the
deletions).

Hope that helps.
-Yonik

On 1/8/06, Harini Raghavan <ha...@insideview.com> wrote:
> Hi Koji,
> I am closing the reader after deleting. You can see the close being
> called in the finally block in the code.
> -Harini
>
> Koji Sekiguchi wrote:
>
> >Hi Harini,
> >
> >Close the reader after delete docs to take effect.
> >
> >Hope this helps,
> >
> >Koji
> >
> >
> >
> >>-----Original Message-----
> >>From: Harini Raghavan [mailto:harini.raghavan@insideview.com]
> >>Sent: Monday, January 09, 2006 1:09 AM
> >>To: java-user@lucene.apache.org
> >>Subject: Deleting a Document
> >>
> >>
> >>Hi,
> >>I want to update a document in the lucene index. As mentioned in the
> >>documentation, I tried to delete the document using IndexReader.delete
> >>method. But even after I delete the document, I am able to see the
> >>document when I perform a search. I thought this could be because, I am
> >>caching the IndexSearcher instance and refreshing it only when required.
> >>So I tried to refresh the IndexSearcher instance immediately after
> >>deletion. But even this did not solve the problem. Here is the code:
> >>    public synchronized void deleteDocument(String id) {
> >>        String indexLoc = luceneConfig.getIndexDir();
> >>        Directory fsDir = getIndexDirectory(indexLoc, false);
> >>        IndexReader reader = getIndexReader(fsDir);
> >>        try {
> >>            Term t = new Term(IndexSearchConstants.DOCUMENT_ID, id);
> >>            reader.delete(t);
> >>
> >>            //After deleting the document refresh the
> >>IndexSearcher instance
> >>            manager.refreshIndexSearcher();
> >>        } catch (IOException e) {
> >>            logger.error("IOException occurred in deleteDocument()", e);
> >>        } finally {
> >>            try {
> >>                reader.close();
> >>            } catch(IOException e) {}
> >>        }
> >>    }
> >>The document seems to be deleted only when I restart the server. Am I
> >>missing something while deleting the document? When does the document
> >>actually get deleted from the index?
> >>Thanks,
> >>Harini
> >>
> >>
> >>
> >>---------------------------------------------------------------------
> >>To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
> >>For additional commands, e-mail: java-user-help@lucene.apache.org
> >>
> >>
> >>
> >>
> >
> >
> >---------------------------------------------------------------------
> >To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
> >For additional commands, e-mail: java-user-help@lucene.apache.org
> >
> >
> >
> >
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
> For additional commands, e-mail: java-user-help@lucene.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
For additional commands, e-mail: java-user-help@lucene.apache.org


Re: Deleting a Document

Posted by Harini Raghavan <ha...@insideview.com>.
Hi Koji,

Thanks for the suggestion. It worked when I closed the reader before 
refreshing the IndexSearcher instance.

Harini

Koji Sekiguchi wrote:

>Hi Harini,
>
>I meant you close the reader first, then get a new searcher.
>
>regards,
>
>Koji
>
>  
>
>>-----Original Message-----
>>From: Harini Raghavan [mailto:harini.raghavan@insideview.com]
>>Sent: Monday, January 09, 2006 1:16 PM
>>To: java-user@lucene.apache.org
>>Subject: Re: Deleting a Document
>>
>>
>>Hi Koji,
>>I am closing the reader after deleting. You can see the close being 
>>called in the finally block in the code.
>>-Harini
>>
>>    
>>
>
>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
>For additional commands, e-mail: java-user-help@lucene.apache.org
>
>
>  
>

---------------------------------------------------------------------
To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
For additional commands, e-mail: java-user-help@lucene.apache.org


RE: Deleting a Document

Posted by Koji Sekiguchi <ko...@m4.dion.ne.jp>.
Hi Harini,

I meant you close the reader first, then get a new searcher.

regards,

Koji

> -----Original Message-----
> From: Harini Raghavan [mailto:harini.raghavan@insideview.com]
> Sent: Monday, January 09, 2006 1:16 PM
> To: java-user@lucene.apache.org
> Subject: Re: Deleting a Document
> 
> 
> Hi Koji,
> I am closing the reader after deleting. You can see the close being 
> called in the finally block in the code.
> -Harini
> 



---------------------------------------------------------------------
To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
For additional commands, e-mail: java-user-help@lucene.apache.org


Re: Deleting a Document

Posted by Harini Raghavan <ha...@insideview.com>.
Hi Koji,
I am closing the reader after deleting. You can see the close being 
called in the finally block in the code.
-Harini

Koji Sekiguchi wrote:

>Hi Harini,
>
>Close the reader after delete docs to take effect.
>
>Hope this helps,
>
>Koji
>
>  
>
>>-----Original Message-----
>>From: Harini Raghavan [mailto:harini.raghavan@insideview.com]
>>Sent: Monday, January 09, 2006 1:09 AM
>>To: java-user@lucene.apache.org
>>Subject: Deleting a Document
>>
>>
>>Hi,
>>I want to update a document in the lucene index. As mentioned in the 
>>documentation, I tried to delete the document using IndexReader.delete 
>>method. But even after I delete the document, I am able to see the 
>>document when I perform a search. I thought this could be because, I am 
>>caching the IndexSearcher instance and refreshing it only when required. 
>>So I tried to refresh the IndexSearcher instance immediately after 
>>deletion. But even this did not solve the problem. Here is the code:
>>    public synchronized void deleteDocument(String id) {
>>        String indexLoc = luceneConfig.getIndexDir();
>>        Directory fsDir = getIndexDirectory(indexLoc, false);
>>        IndexReader reader = getIndexReader(fsDir);
>>        try {
>>            Term t = new Term(IndexSearchConstants.DOCUMENT_ID, id);
>>            reader.delete(t);
>>
>>            //After deleting the document refresh the 
>>IndexSearcher instance
>>            manager.refreshIndexSearcher();
>>        } catch (IOException e) {
>>            logger.error("IOException occurred in deleteDocument()", e);
>>        } finally {
>>            try {
>>                reader.close();
>>            } catch(IOException e) {}
>>        }
>>    }
>>The document seems to be deleted only when I restart the server. Am I 
>>missing something while deleting the document? When does the document 
>>actually get deleted from the index?
>>Thanks,
>>Harini
>>
>>
>>
>>---------------------------------------------------------------------
>>To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
>>For additional commands, e-mail: java-user-help@lucene.apache.org
>>
>>
>>    
>>
>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
>For additional commands, e-mail: java-user-help@lucene.apache.org
>
>
>  
>

---------------------------------------------------------------------
To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
For additional commands, e-mail: java-user-help@lucene.apache.org


RE: Deleting a Document

Posted by Koji Sekiguchi <ko...@m4.dion.ne.jp>.
Hi Harini,

Close the reader after delete docs to take effect.

Hope this helps,

Koji

> -----Original Message-----
> From: Harini Raghavan [mailto:harini.raghavan@insideview.com]
> Sent: Monday, January 09, 2006 1:09 AM
> To: java-user@lucene.apache.org
> Subject: Deleting a Document
> 
> 
> Hi,
> I want to update a document in the lucene index. As mentioned in the 
> documentation, I tried to delete the document using IndexReader.delete 
> method. But even after I delete the document, I am able to see the 
> document when I perform a search. I thought this could be because, I am 
> caching the IndexSearcher instance and refreshing it only when required. 
> So I tried to refresh the IndexSearcher instance immediately after 
> deletion. But even this did not solve the problem. Here is the code:
>     public synchronized void deleteDocument(String id) {
>         String indexLoc = luceneConfig.getIndexDir();
>         Directory fsDir = getIndexDirectory(indexLoc, false);
>         IndexReader reader = getIndexReader(fsDir);
>         try {
>             Term t = new Term(IndexSearchConstants.DOCUMENT_ID, id);
>             reader.delete(t);
> 
>             //After deleting the document refresh the 
> IndexSearcher instance
>             manager.refreshIndexSearcher();
>         } catch (IOException e) {
>             logger.error("IOException occurred in deleteDocument()", e);
>         } finally {
>             try {
>                 reader.close();
>             } catch(IOException e) {}
>         }
>     }
> The document seems to be deleted only when I restart the server. Am I 
> missing something while deleting the document? When does the document 
> actually get deleted from the index?
> Thanks,
> Harini
> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
> For additional commands, e-mail: java-user-help@lucene.apache.org
> 
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
For additional commands, e-mail: java-user-help@lucene.apache.org