您好,登錄后才能下訂單哦!
這篇文章主要介紹“ElasticSearch基本操作有哪些”,在日常操作中,相信很多人在ElasticSearch基本操作有哪些問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”ElasticSearch基本操作有哪些”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!
GET /_cat/nodes: 查看所有節(jié)點(diǎn)
GET /_cat/health: 查看健康狀況
GET /_cat/master:查看主節(jié)點(diǎn)
GET /_cat/indices:查看所有索引 相當(dāng)于mysql中的showdatabases
PUT customer/external/1;在customer索引(mysql中的數(shù)據(jù)庫)下的external類型(mysql中的表)下保存1號數(shù)據(jù)(唯一標(biāo)識)為
PUT customer/external/1 { "name":"gison" }
PUT和POST都可以,
POST新增:如果不指定id,會自動生成id。指定id就會修改這個(gè)數(shù)據(jù),并新增版本號。
PUT可以新增可以修改。PUT必須指定id;由于PUT需要指定id,我們一般用來做修改操作,不指定id會報(bào)錯(cuò)。
操作結(jié)果:
{ "_index" : "customer", "_type" : "external", "_id" : "1", "_version" : 1, "result" : "created", "_shards" : { "total" : 2, "successful" : 1, "failed" : 0 }, "_seq_no" : 0, "_primary_term" : 1 }
GET customer/external/1 { "_index" : "customer", "_type" : "external", "_id" : "1", "_version" : 2,//版本號 "_seq_no" : 1,//并發(fā)控制字段,每次更新就會+1,用來做樂觀鎖 "_primary_term" : 1,//同上,主分片重新分配,如重啟,就會變化 "found" : true, "_source" : { "name" : "gison" } }
(新版本的es樂觀鎖控制用seq_no,老版本用version)
測試一下樂觀鎖
模擬兩個(gè)用戶A跟B都想改上面這條數(shù)據(jù),A用戶查出if_seq_no=1,if_primary_term=1,執(zhí)行更新
PUT customer/external/1?if_seq_no=1&if_primary_term=1 { "name":"鳴人" }
B用戶同樣也查出if_seq_no=1,if_primary_term=1,隨后執(zhí)行更新
PUT customer/external/1?if_seq_no=1&if_primary_term=1 { "name":"卡卡西" }
A用戶執(zhí)行結(jié)果
{ "_index" : "customer", "_type" : "external", "_id" : "1", "_version" : 3, "result" : "updated", "_shards" : { "total" : 2, "successful" : 1, "failed" : 0 }, "_seq_no" : 2, "_primary_term" : 1 }
B用戶執(zhí)行結(jié)果
{ "error": { "root_cause": [ { "type": "version_conflict_engine_exception", "reason": "[1]: version conflict, required seqNo [1], primary term [1]. current document has seqNo [2] and primary term [1]", "index_uuid": "XbVN6IayQTWbliz3cOOyGw", "shard": "0", "index": "customer" } ], "type": "version_conflict_engine_exception", "reason": "[1]: version conflict, required seqNo [1], primary term [1]. current document has seqNo [2] and primary term [1]", "index_uuid": "XbVN6IayQTWbliz3cOOyGw", "shard": "0", "index": "customer" }, "status": 409 }
post帶_update
帶_update更新的時(shí)候,要加上doc,對比原來數(shù)據(jù),與原來一樣就不做任何操作
POST customer/external/1/_update { "doc": { "name":"gison" } }
返回結(jié)果
{ "_index" : "customer", "_type" : "external", "_id" : "1", "_version" : 7, "result" : "noop", "_shards" : { "total" : 0, "successful" : 0, "failed" : 0 }, "_seq_no" : 6, "_primary_term" : 1 }
post和put不帶_update,都不與前內(nèi)容做對比
DELETE customer/external/1 // 刪除一條數(shù)據(jù) DELETE customer //刪除索引
ES沒有提供刪除類型的操作
POST customer/external/_bulk {"delete": {"_index": "website", "_type": "blog", "_id": "123"}} {"create": {"_index": "website", "_type": "blog", "_id": "123"}} {"title": "first blog"} {"index": {"_index": "website", "_type": "blog"}} {"title": "second blog"} {"update": {"_index": "website", "_type": "blog", "_id": "123"}} {"doc":{"title": "update blog"}}
語法格式
{action:{metadata}}\n
{request body} \n
{action:{metadata}}\n
{request body} \n
index和create區(qū)別
index時(shí)會檢查_version。如果插入時(shí)沒有指定_version,那對于已有的doc,_version會遞增,并對文檔覆蓋。插入時(shí)如果指定_version,如果與已有的文檔_version不相等,則插入失敗,如果相等則覆蓋,_version遞增。
create時(shí)也會檢查_version,但是對于已有的文檔,不會創(chuàng)建新文檔,即插入失敗。
到此,關(guān)于“ElasticSearch基本操作有哪些”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注億速云網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。