Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does the elasticsearch ID have to be unique to a type or to the index?

Elasticsearch allows you to store a _type along with the _index. I was wondering if I were to provide my own _id should it be unique across the index?

like image 533
Archimedes Trajano Avatar asked Jul 31 '15 13:07

Archimedes Trajano


People also ask

Is ID unique in Elasticsearch?

IDs are not unique across indices. If you want to refer to a document you need to know both the index name and the ID.

What is ID in Elasticsearch?

_id fieldedit 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.

What is the difference between index and type in Elasticsearch?

In RDBMS terms, index is a database and type can be a table which contains many rows( document in elasticsearch).


1 Answers

It should be unique together

PUT so
PUT /so/t1/1
{}
PUT /so/t2/1
{}
GET /so/_search

{
   "took": 1,
   "timed_out": false,
   "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
   },
   "hits": {
      "total": 2,
      "max_score": 1,
      "hits": [
         {
            "_index": "so",
            "_type": "t2",
            "_id": "1",
            "_score": 1,
            "_source": {}
         },
         {
            "_index": "so",
            "_type": "t1",
            "_id": "1",
            "_score": 1,
            "_source": {}
         }
      ]
   }
}

And the reason for that: you'd never get documents by index w/o knowing doctype, and querying ES with index-wide query will return documents including their types and indexes.

like image 145
Slam Avatar answered Oct 01 '22 14:10

Slam