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

查看:28
本文介绍了如何设置一个字段以保持行在 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.

怎么做?

推荐答案

这正是IndexWrite#updateDocument 方法.第一个参数是索引中必须唯一的词.

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天全站免登陆