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

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

问题描述

我正在使用弹性搜索作为文档数据库,我创建的每个记录都有一个系统用于记录ID的guid id。商务人士希望提供一个功能,让用户根据日期和自己的自动文件名约定,到目前为止这个日期/月份创建了多少条记录。



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

解决方案

你需要使用应该是唯一的字段作为文档的id 。默认情况下,具有现有ID的新文档将覆盖具有相同ID的现有文档,但是您可以切换到 op_type = create ,以便如果具有相同的文档id已经存在。



尽管如此,没有办法与任意字段具有相同的行为,只有 _id 字段以这种方式工作。我可能会考虑在应用程序层中处理这个逻辑,而不是在弹性搜索中。


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