Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set a field to keep a row unique in lucene?

My app generates unique id for each row to index in lucene and save to database. One sutation is if there is and row have the same id,I want to update it,not insert an new row and index.

How to do that?

like image 265
Shisoft Avatar asked Mar 07 '12 02:03

Shisoft


1 Answers

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

For example,

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);

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

like image 182
jpountz Avatar answered Sep 30 '22 13:09

jpountz