简体   繁体   English

Lucene 6.0.0从索引中删除文档

[英]Lucene 6.0.0 deleting document from index

I'm trying to delete a document from a Lucene index I created. 我正在尝试从创建的Lucene索引中删除文档。 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. 我尝试使用和不使用forceMergeDeletes来执行上述代码,并且都显示hasDeletions为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. 我还尝试在新程序中打开IndexReader进行搜索,以检查删除文件是否有任何延迟,但是我可以在已删除的文件中进行搜索。 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. 当我尝试使用查询删除文档时,整个索引将被删除(即使在APPEND模式下),并且我确信索引中每个文档的文件名都不同。 I am new to Lucene if someone can help me it would be great :) 如果有人可以帮助我,我是Lucene的新手,那太好了:)

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. 请记住,运行查询时, deleteDocuments会删除所有搜索结果,无论得分如何,而不仅仅是最佳结果或第一结果。

Let's say you are trying to delete a file at path: "/rootdirectory/testfile.txt" 假设您要删除路径为“ /rootdirectory/testfile.txt”的文件

The analyzed query will look like: filename:rootdirectory filename:testfile.txt 分析后的查询将类似于: filename:rootdirectory filename:testfile.txt

So if all your documents filenames are somewhere in "rootdirectory", then yes, they will all be deleted. 因此,如果您所有文档的文件名都在“ rootdirectory”中,则可以,它们都将被删除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM