如何在 Elasticsearch 数据库中创建唯一约束? [英] How to create unique constraint in Elasticsearch database?

查看:23
本文介绍了如何在 Elasticsearch 数据库中创建唯一约束?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 elasticsearch 作为文档数据库,我创建的每条记录都有一个系统用于记录 ID 的 guid id.业务人员希望提供一项功能,让用户可以根据日期和迄今为止这一天/每月创建的记录数量,拥有自己的自动文件命名约定.

我需要的是防止重复的用户文件名.有没有办法将索引字段设置为唯一?像 sql 唯一约束?

解决方案

您需要使用应该唯一的字段作为文档的 id.默认情况下,具有现有 id 的新文档将覆盖具有相同 id 的现有文档,但您可以切换到 op_type=create 以便在具有相同 id 的文档已存在时返回错误.>

虽然没有办法对任意字段有相同的行为,只有 _id 字段就是这样工作的.我可能会考虑在应用层而不是在 elasticsearch 中处理这个逻辑.

I am using elasticsearch as a document database and each record I create has a guid id that the system uses for the record id. Business people want to offer a feature to let the user have their own auto file name convention based on date and how many records were created so far this day/month.

What I need is to prevent duplicate user file names. Is there a way to setup an indexed field to be unique? Like a sql unique constraint?

解决方案

You'd need to use the field that is supposed to be unique as id for your documents. By default a new document with existing id would override the existing document with same id, but you can switch to op_type=create in order to get back an error if a document with same id already exists.

There's no way to have the same behaviour with arbitrary fields though, only the _id field works that way. I would probably consider handling this logic in the application layer instead of within elasticsearch.

这篇关于如何在 Elasticsearch 数据库中创建唯一约束?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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