我的項(xiàng)目圍繞Lucene 6.6.0展開。實(shí)際上,它處理用Java編寫的桌面搜索引擎,其中搜索部分與索引部分位于單獨(dú)的應(yīng)用程序中。我必須不時地向索引添加新字段,以滿足客戶的需求,而不必重新索引(即解析文件+索引)所有內(nèi)容。因此,當(dāng)應(yīng)用程序啟動時,我使用IndexWriter,打開與之關(guān)聯(lián)的IndexReader:IndexReader reader = DirectoryReader.open(writer, true, false);然后,對于索引中已經(jīng)存在的每個文檔:StoredField fieldVersion = new StoredField( FIELDNAME_VERSION, fixedValue // The value is the same for all the documents but may change (for all documents) when upgrading the version. );for (int i = 0; i < idMax; i++) { Document currentDoc = reader.document(i); // Checks if the field exists in the index if ( // Field does not exist yet currentDoc.get(FIELDNAME_VERSION) == null || // Field value is different from what it should be !currentDoc.get(FIELDNAME_VERSION).contentEquals(fixedValue)) { // THe field does not exist so we add it to the doc and beforehand removes the field from the currentDoc (also tried without removing first with the same result) currentDoc.removeField(FIELDNAME_VERSION); currentDoc.add(fieldVersion); // Updates the document in the index writer.updateDocuments( new Term(FIELDNAME_PATH, currentDoc.get(FIELDNAME_PATH), currentDoc); // also tried with writer.deleteDocuments(new Term(FIELDNAME_PATH, currentDoc.get(FIELDNAME_PATH))); writer.addDocument(currentDoc); }}// When all documents have been checked, write the indexwriter.commit();當(dāng)我第一次運(yùn)行該字段時,該字段將按預(yù)期方式添加到所有沒有此字段的文檔中。問題是,當(dāng)fixedValue更改時,會將新文檔添加到索引,而我希望currentDoc更新其fieldVersion,而不是為fieldVersion創(chuàng)建另一個與所有字段具有相同原始值的Document。IndexWriter處于附加模式(也可以嘗試附加或創(chuàng)建)。而且,如果我首先為單個文件建立索引,那么我在索引中得到1個文檔,然后在索引更新后得到2個文檔,然后是4個,然后8個,然后是16個,……總是引用相同的單個文件(只有fieldVersion具有不同的內(nèi)容)。這個其他的問題對我沒有幫助。當(dāng)我要求Lucene更新現(xiàn)有文檔時,為什么要添加新文檔,Lucene為什么要添加新的文檔,以及解決該問題的方法(即僅將fieldVersion的不同內(nèi)容替換為同一文檔中的現(xiàn)有文檔)?
Lucene不斷添加文檔,而使用updateDocument
慕尼黑8549860
2021-05-06 13:15:10