Search code examples
elasticsearchspring-data-elasticsearch

How to enforce uniqueness on a field in Elasticsearch to avoid duplicates


@Repository
public interface FooRepository extends ElasticsearchRepository<Foo, String> {
    Foo findByName(String name);
}

How to enforce uniqueness on a field in Elasticsearch to avoid duplicates, I want name to be unique in Foo. Is there a way to enforce this using FieldType metadata


Solution

  • The uniqueness of a field in Elasticsearch is guaranteed only for id fields and if you want the error if you try to update the same id value, you can use the op_type=create in your index-API.

    There are several posts on Elastic's discuss and stackoverflow confirming the same behavior.

    few good posts which you can follow for further understanding like this and this and this