溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Elasticsearch document id 生成方式是什么

發(fā)布時(shí)間:2021-12-16 17:33:36 來源:億速云 閱讀:228 作者:柒染 欄目:云計(jì)算

本篇文章為大家展示了Elasticsearch document id 生成方式是什么,內(nèi)容簡明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過這篇文章的詳細(xì)介紹希望你能有所收獲。

Elasticsearch document id 生成方式是什么

手動(dòng)指定

根據(jù)應(yīng)用情況來說,是否滿足手動(dòng)指定 document id 的前提:

一般來說,是從某些其他的系統(tǒng)中,導(dǎo)入一些數(shù)據(jù)到es時(shí),會(huì)采取這種方式,就是使用系統(tǒng)中已有數(shù)據(jù)的唯一標(biāo)識(shí),作為es中document的id。舉個(gè)例子,比如說,我們現(xiàn)在在開發(fā)一個(gè)電商網(wǎng)站,做搜索功能,或者是OA系統(tǒng),做員工檢索功能。這個(gè)時(shí)候,數(shù)據(jù)首先會(huì)在網(wǎng)站系統(tǒng)或者IT系統(tǒng)內(nèi)部的數(shù)據(jù)庫中,會(huì)先有一份,此時(shí)就肯定會(huì)有一個(gè)數(shù)據(jù)庫的primary key(自增長,UUID,或者是業(yè)務(wù)編號(hào))。如果將數(shù)據(jù)導(dǎo)入到 Elasticsearch 中,此時(shí)就比較適合采用數(shù)據(jù)在數(shù)據(jù)庫中已有的primary key。

如果說,我們是在做一個(gè)系統(tǒng),這個(gè)系統(tǒng)主要的數(shù)據(jù)存儲(chǔ)就是 Elasticsearch 一種,也就是說,數(shù)據(jù)產(chǎn)生出來以后,可能就沒有id,直接就放es一個(gè)存儲(chǔ),那么這個(gè)時(shí)候,可能就不太適合說手動(dòng)指定document id的形式了,因?yàn)槟阋膊恢纈d應(yīng)該是什么,此時(shí)可以采取下面要講解的讓 Elasticsearch 自動(dòng)生成id的方式。

# put /index/type/id

PUT /test_index/test_type/2
{
  "test_content": "my test"
}

{
  "_index" : "test_index",
  "_type" : "test_type",
  "_id" : "2",
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 2,
    "failed" : 0
  },
  "_seq_no" : 0,
  "_primary_term" : 1
}

自動(dòng)生成

# post /index/type

PUT test_index/test_type
{
  "test_content": "my test automated document id"
}

{
  "error" : "Incorrect HTTP method for uri [/test_index/test_type?pretty=true] and method [PUT], allowed: [POST]",
  "status" : 405
}

POST /test_index/test_type
{
  "test_content": "my test"
}

{
  "_index" : "test_index",
  "_type" : "test_type",
  "_id" : "A7Ma5XYB_s8SuYmy2Xg0",
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 2,
    "failed" : 0
  },
  "_seq_no" : 1,
  "_primary_term" : 1
}
# post /index/type

PUT test_index/test_type
{
  "test_content": "my test automated document id"
}

{
  "error" : "Incorrect HTTP method for uri [/test_index/test_type?pretty=true] and method [PUT], allowed: [POST]",
  "status" : 405
}

POST /test_index/test_type
{
  "test_content": "my test"
}

{
  "_index" : "test_index",
  "_type" : "test_type",
  "_id" : "A7Ma5XYB_s8SuYmy2Xg0",
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 2,
    "failed" : 0
  },
  "_seq_no" : 1,
  "_primary_term" : 1
}

有可能兩個(gè)創(chuàng)建 Document 的請(qǐng)求是完全在同一時(shí)間執(zhí)行的(小概率事件),只不過在不同的 Elastic 節(jié)點(diǎn)上,那么,如果 _id 自動(dòng)生成的算法不夠好的話,有沒有可能出現(xiàn)兩個(gè)節(jié)點(diǎn),給兩個(gè)不同的 Document 創(chuàng)建了相同的 _id ?

當(dāng)然是不可能的。

GUID 算法可以保證在分布式的環(huán)境下,不同節(jié)點(diǎn)同一時(shí)間創(chuàng)建的 _id 一定是不沖突的(即使是同一個(gè)節(jié)點(diǎn),也不會(huì)有任何的問題)。

Elasticsearch 自動(dòng)生成 _id 的機(jī)制,可以保證不會(huì)出現(xiàn)兩個(gè)不同的 Document 的 _id 是一樣的。

注意,自動(dòng)生成 ID 的時(shí)候,使用的是 POST 而不是 PUT;手動(dòng)生成 ID 的時(shí)候使用 PUT 或者 POST 都可以。

另外,這一節(jié)的實(shí)際操作,我是在 cloud.elastic.co 提供的虛擬機(jī)上進(jìn)行的。其實(shí)在準(zhǔn)備認(rèn)證期間,我覺得可以考慮購買兩個(gè)月左右的服務(wù);也可以考慮在阿里云上購買。

自動(dòng)生成的id,長度為20個(gè)字符,URL安全,base64編碼,GUID,分布式系統(tǒng)并行生成時(shí)不可能會(huì)發(fā)生沖突。

上述內(nèi)容就是Elasticsearch document id 生成方式是什么,你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI