I need to store the unique auto increment id together with the rest of the fields in my document in ElasticSearch. Is there anyway in ElasticSearch to obtain them.
I found this as a potential solution: http://blogs.perl.org/users/clinton_gormley/2011/10/elasticsearchsequence---a-blazing-fast-ticket-server.html
But I just wonder is there any better way?
Each document has an _id that uniquely identifies it, which is indexed so that documents can be looked up either with the GET API or the ids query. The _id can either be assigned at indexing time, or a unique _id can be generated by Elasticsearch.
Auto-increment should be used as a unique key when no unique key already exists about the items you are modelling. So for Elements you could use the Atomic Number or Books the ISBN number.
the _seq_no and _primary_term as parameter needed to implement the optimistic locking. Elasticsearch keeps tracks of the sequence number and primary term of the last operation to have changed each of the documents it stores.
The 1.x reference document API says that you can leave off an id and it will be automatically generated. Use POST instead of put and the op_type will automatically be set to create.
https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-index_.html#_automatic_id_generation
Here's another way to use elasticsearch to create iids
:
The main advantage is that the iid
can be backed up with simple dumps where an implementation using the _version
of elasticsearch is not able to back up the version.
It allows to request a bulk of iids to minimize the number of requests needed.
A request to get a bulk of 10 iids
would look like this:
curl -XPOST "http://localhost:9200/sequence/sequence/1/_updatefields=iid&retry_on_conflict=5" -d '{ "script": "ctx._source.iid += bulk_size", "params": { "bulk_size": 10 }, "lang": "groovy", "upsert": { "iid": 0 } }'
It needs this (optimised) mapping for the index:
curl -XPOST http://localhost:9200/sequence/_mapping -d '{ "settings": { "number_of_shards": 1, "auto_expand_replicas": "0-all" }, "mappings": { "sequence": { "_all": { "enabled": 0 }, "_type": { "index": "no" }, "dynamic": "strict", "properties": { "iid": { "type": "string", "index": "no" } } } } }'
A more detailed description can be found here:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With