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

爷,独闯天下 提交于 2019-11-29 20:12:42

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/9595057/how-to-set-a-field-to-keep-a-row-unique-in-lucene

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!