简体   繁体   中英

Lucene update field from document from index without loose information

i like to update a field from my document in my index. An Example, my document:

id: 123 (For term)
xNum: 4568-456843
content (Field.Store.NO): my text from the file

Now i like to update only the xNum field (without new content), without loose the indexed words from this document. The content come from files, not from a database. If is possible to do this? somehow?

I use Lucene Core V.8.3

Actually there is no update for Lucene indexed field. Even when you run an update function it deletes the indexed record then add new one. When you not load your previous records then you will be only adding xNum: 4568-456843 value to the that record.

int id = 123 ;
String idSearch = "Id";

Query searchQuery = new TermQuery(new Term(idSearch , String.valueOf(id)))

TopDocs result = searcher.search(searchQuery ,1);

for (ScoreDoc docTemp : result.scoreDocs) {
   Document _doc = searcher.doc(docTemp.doc);
   YourObject obj = fillFields(doc);
}
obj.xNum = 4568-456843;

Document updateDoc = createDoc(obj);


indexWriter.updateDocument(new Term(idSearch, String.valueOf(id)), updateDoc);

You could try to cast your results fields then update which property you want and update it as in the example

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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