ElasticSearch ID 自动生成和手动指定

ElasticSearch ID 自动生成和手动指定

Posted by lijian on April 22, 2018

手动指定

  • 从其他系统中导入到es 会采取手动指定的方式,采用系统已有的唯一标示作为es document的 _id
PUT /test_index/test_type/2
{
  "test_content":"this is content"
}
GET /test_index/test_type/2
{
  "_index" : "test_index",
  "_type" : "test_type",
  "_id" : "2",  # 这里的_id 是我们指定的
  "_version" : 2,
  "_seq_no" : 1,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "test_content" : "this is content"
  }
}

自动生成

  • es 就是数据的唯一存储介质
  • 自动生成ID程度为20个字符
  • url 安全的,可以放在url 中
  • guid , 分布式系统并行生成也不会发生冲突
POST /test_index/test_type
{
  "test_content":"this is NEW content"
}

{
  "_index" : "test_index",
  "_type" : "test_type",
  "_id" : "jREORWoBnWNfF8XNxH71", # 注意这里_id 为我们自动生成的ID
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 0,
  "_primary_term" : 1
}