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.
source share