Search code examples
springelasticsearchspring-dataspring-data-elasticsearch

Unique constraint on field in spring data elasticsearch


It is possible to create unique constraint in Spring Data Elasticsearch which is equal to @Column(unique = true) in RDBMS?

Unfortunately, @Field annotation does not have Unique param. Perhaps I could use @Id but what if there are other fields like username, email, ...?


Solution

  • Their is no unique constraint in Elasticsearch. You can use your field as an _id like explained in this post : How to create unique constraint in Elasticsearch database?

    Or you'll have to create a validator by yourself to test if the value exist with some term query.

    Addition: Although the op_type parameter sent to Elasticsearch would provide this feature, there is currently no support for this flag in Spring Data Elasticsearch. There is an issue in Jira which you could upvote.