ar099968
ar099968

Reputation: 7547

Elasticsearch: auto increment integer field across two index

I need a auto increment integer field across two index.

Can Elasticsearch do it automatically like MySQL "auto increment" field in a table?

Eg. when puts some documents in two different index:

POST /my_index_1/blogpost/
{
  "title": "Foo Bar"
}

POST /my_index_2/blogpost/
{
  "title": "Baz quux"
}

On retrieve it, i want:

GET /my_index_*/blogpost/
{
  "uid" : 1,
  "title": "Foo Bar"
},
{
  "uid" : 2,
  "title": "Baz quux"
}

Upvotes: 1

Views: 5057

Answers (1)

Jilles van Gurp
Jilles van Gurp

Reputation: 8294

No, ES does not have any auto increment feature since it is a distributed system, figuring out the correct value for the counter is non trivial. Especially since (bulk) indexing tends to be heavily concurrent. You can typically max out CPUs on all nodes if you throw enough documents at it.

So, your best option is to do this outside of ES before you send the documents to ES. Or even better, don't do this. If you need some kind of order of insertion, a better option is to simply use a timestamp. They are actually stored as a number internally. You still might get duplicates of course if two documents get indexed the same millisecond. A trick we've used to work around that is to offset documents indexed at the same time by 1 ms. to ensure we keep the insertion order.

Upvotes: 5

Related Questions