Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete Documents from a Lucene Index using Term or QueryParser

I am trying to delete documents from Lucene Index. I want to delete only the specified file from lucene index .

My following program is deleting the index which can be searched using keyword analyzer but my required filename can be searched only using StandardAnalyzer . So is it any way to set standard analyzer in my term or instead of term how can i user QueryParser to delete the Documents from lucene index.

 try{
    File INDEX_DIR= new File("D:\\merge lucene\\abc\\");

    Directory directory = FSDirectory.open(INDEX_DIR);

     IndexReader indexReader = IndexReader.open(directory,false);
     Term term= new Term("path","fileindex23005.htm");
    int l=   indexReader.deleteDocuments(term);
                      indexReader.close();

    System.out.println("documents deleted");
  }
  catch(Exception x){x.printStackTrace();}
like image 207
adesh singh Avatar asked Dec 16 '22 06:12

adesh singh


1 Answers

I assume you are using Lucene 3.6 or before, otherwise IndexReader.deleteDocuments no longer exists. You should, however, be using IndexWriter instead, anyway.

If you can only find the document using query parser, then just run a normal query, then iterate through the documents returned, and delete them by docnum, along the lines of:

Query query = queryParser.parse("My Query!");
ScoreDoc[] docs = searcher.search(query, 100).scoreDocs;
For (ScoreDoc doc : docs) {
    indexReader.deleteDocument(doc.doc);
}

Or better yet (simpler, uses non-defunct, non-deprecated functionality), just use an IndexWriter, and pass it the query directly:

Query query = queryParser.parse("My Query!");
writer.deleteDocuments(query);
like image 199
femtoRgon Avatar answered May 18 '23 21:05

femtoRgon