Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create unique constraint in Elasticsearch database?

Tags:

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?

like image 388
Aaron Fischer Avatar asked Jan 30 '14 15:01

Aaron Fischer


People also ask

How do I create a unique index in Elasticsearch?

One solution will be to use uniqueId field value for specifying document ID and use op_type=create while storing the documents in ES. With this you can make sure your uniqueId field will have unique value and will not be overridden by another same valued document.

How do I find unique constraints in PostgreSQL?

To find the name of a constraint in PostgreSQL, use the view pg_constraint in the pg_catalog schema. Join the view pg_catalog. pg_constraint with the view pg_class ( JOIN pg_class t ON t. oid = c.

Is Elasticsearch good for relational data?

Because Elasticsearch is not a relational database, joins do not exist as a native functionality like in an SQL database. It focuses more on search efficiency as opposed to storage efficiency. The stored data is practically flattened out or denormalized to drive fast search use cases.

How many records can Elasticsearch handle?

Every Elasticsearch index made up of one or more shards which are internally Lucene index which has a hard limit of ~2 billion(precisely equal to INT_MAX value-128) on maximum no of the document as explained in this link and this link.


1 Answers

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.

like image 86
javanna Avatar answered Sep 20 '22 11:09

javanna