SH_V95
SH_V95

Reputation: 161

Lucene 6.0.0 deleting document from index

I'm trying to delete a document from a Lucene index I created. And my delete code looks something like this :

public void delete(String fname, String index_path)
   {
        try {
            Analyzer analyzer = new StandardAnalyzer();
            QueryParser parser = new QueryParser(LuceneConstants.FILE_NAME, analyzer);
            Directory indexDirectory = FSDirectory.open(Paths.get(index_path));
            IndexWriterConfig iwg = new IndexWriterConfig(analyzer);
            iwg.setMaxBufferedDeleteTerms(1);
            iwg.setOpenMode(IndexWriterConfig.OpenMode.APPEND);
            IndexWriter writer = new IndexWriter(indexDirectory,iwg);
            writer.deleteDocuments(new Term(LuceneConstants.FILE_NAME,fname));
            writer.forceMergeDeletes();
            writer.commit();
            writer.flush();
            System.out.println(writer.hasDeletions());
            writer.close();
            System.out.println("Deleted File :"+fname);
        } catch (IOException ex) {
            Logger.getLogger(IndexHandler.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ParseException ex) {
            Logger.getLogger(IndexHandler.class.getName()).log(Level.SEVERE, null, ex);
        }
   }

I have tried executing the above code with and without forceMergeDeletes and both show me hasDeletions as false. Is there something wrong with my code ? I also tried opening the IndexReader in a new program to search to check if there are any delays to delete the file but I am able to search in the deleted file. When i try to delete documents using query, the whole index is deleted (even in APPEND mode) and I am sure the filenames are different for each document in the index. I am new to Lucene if someone can help me it would be great :)

Upvotes: 0

Views: 834

Answers (1)

femtoRgon
femtoRgon

Reputation: 33351

You should be careful using an analyzed query to delete. Remember that deleteDocuments will delete all search results when running the query, regardless of score, not just the best or first result.

Let's say you are trying to delete a file at path: "/rootdirectory/testfile.txt"

The analyzed query will look like: filename:rootdirectory filename:testfile.txt

So if all your documents filenames are somewhere in "rootdirectory", then yes, they will all be deleted.

Upvotes: 1

Related Questions