Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Lucene updateDocument删除该文档,但计数一直在增加_Java_Lucene - Fatal编程技术网

Java Lucene updateDocument删除该文档,但计数一直在增加

Java Lucene updateDocument删除该文档,但计数一直在增加,java,lucene,Java,Lucene,我正在使用updateDocument()方法更新lucene索引中的文档。下面是我是怎么做的 writer.updateDocument(new Term(Constants.DOC_ID_FIELD, doc.get(Constants.DOC_ID_FIELD)), doc); 我和Luke检查了我的索引数据,发现在第二次运行索引时,Luke告诉我,已删除的文档-不可用。因此,基本上,文档被标记为已删除,但它仍然驻留在索引中 我不想保留这些标记为已删除的文档。我做错了吗 另外,我的理解是

我正在使用
updateDocument()
方法更新lucene索引中的文档。下面是我是怎么做的

writer.updateDocument(new Term(Constants.DOC_ID_FIELD, doc.get(Constants.DOC_ID_FIELD)), doc);
我和Luke检查了我的索引数据,发现在第二次运行索引时,Luke告诉我,
已删除的文档-不可用
。因此,基本上,文档被标记为已删除,但它仍然驻留在索引中

我不想保留这些标记为已删除的文档。我做错了吗


另外,我的理解是,当我更新文档时,它会删除旧文档,然后添加新文档。不是这样吗?

以下内容应将文档从索引中删除:

public static void deleteDocumentsFromIndexUsingTerm(Term term) throws IOException, ParseException {

        System.out.println("Deleting documents with field '" + term.field() + "' with text '" + term.text() + "'");
        Directory directory = FSDirectory.getDirectory(INDEX_DIRECTORY);
        IndexReader indexReader = IndexReader.open(directory);
        indexReader.deleteDocuments(term);
        indexReader.close();

    }

参考资料:

从邮件列表中获得了答案

IndexWriter.updateDocument()
删除然后添加。因此,您的索引将删除文档。你为什么在乎?它们最终会随着片段的合并而消失

如果您真的在意,请参阅
IndexWriter,forceMergeDeletes()
。另见 javadoc:这通常是一个非常昂贵的操作;很少有理由这样做


我能做到。我已经在找了。我的理解是,当我更新文档时,它会删除旧文档,然后添加新文档。不是这样吗?另外,您的建议要求打开IndexReader和索引编写器。我正在以批处理模式进行索引。因此,在阅读索引和更新索引时,这可能不是一种优雅的方式。