Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get auto increment id for elasticsearch

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?

like image 784
K DAO Avatar asked Mar 18 '14 05:03

K DAO


People also ask

What is Elasticsearch document ID?

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.

Should I use auto increment ID?

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.

What is seq no in Elasticsearch?

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.


2 Answers

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

like image 87
jrwren Avatar answered Oct 03 '22 23:10

jrwren


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:

like image 32
jukart Avatar answered Oct 04 '22 00:10

jukart