如何设置一个字段以使行在Lucene中保持唯一? [英] How to set a field to keep a row unique in lucene?

查看:63
本文介绍了如何设置一个字段以使行在Lucene中保持唯一?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序为每一行生成唯一的ID,以在Lucene中建立索引并将其保存到数据库. 一种说法是,如果存在和行具有相同的ID,我想更新它,而不是插入新的行和索引.

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.

该怎么做?

推荐答案

这正是

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

例如,

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

将确保doc是索引中唯一ID为42的文档.

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

这篇关于如何设置一个字段以使行在Lucene中保持唯一?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆