How to set a field so that the string is unique in lucene?

My application generates a unique identifier for each row for indexing in lucene and storing in the database. One day, if any, and the row has the same identifier, I want to update it, and not insert a new row and index.

How to do it?

+4
source share
1 answer

This is precisely the purpose of the IndexWrite # updateDocument method . The first argument is a term that must be unique in your index.

For instance,

String id = "42"; Document doc = new Document(); Field field = new Field("id", id, Store.YES, Index.NOT_ANALYZED); doc.add(field); indexWriter.updateDocument(new Term("id", id), doc); 

ensures that doc is the only document with id 42 in your index.

+8
source

Source: https://habr.com/ru/post/1400133/


All Articles