初步檢索
1、_cat
get方式查詢es的一些信息/_cat/allocation
/_cat/shards /_cat/shards/{index} /_cat/master查看主節點 /_cat/nodes查看所有節點 /_cat/tasks /_cat/indices查看所有索引 /_cat/indices/{index} /_cat/segments /_cat/segments/{index} /_cat/count /_cat/count/{index} /_cat/recovery /_cat/recovery/{index} /_cat/health查看健康狀況 /_cat/pending_tasks /_cat/aliases /_cat/aliases/{alias} /_cat/thread_pool /_cat/thread_pool/{thread_pools} /_cat/plugins /_cat/fielddata /_cat/fielddata/{fields} /_cat/nodeattrs /_cat/repositories /_cat/snapshots/{repository} /_cat/templates
2,索引一個文檔(保存)
保存一個數據,保存在哪個索引的哪個類型下,指定用哪個唯一標識
PUT customer/external/1
{ "name":"John Doe" }
在customer索引下的external類型下保存1號數據,postman下請求
返回數據如下:帶_的統稱爲元數據,反應了一些基本信息
{ "_index": "customer",表明該數據在哪個索引(數據庫)下; "_type": "external",哪些類型下(表) "_id": "1",保存數據的id是幾 "_version": 1,保存數據的版本 "result": "created",保存的結果,created爲新建,同樣的請求發送多次,後面的均爲更新updated,看下圖 "_shards": {分片信息 "total": 2, "successful": 1, "failed": 0 }, "_seq_no": 0, "_primary_term": 1 }
上面的請求爲PUT,保存新增二合一,必須指定id,這個id下的數據沒有就新增,有就更新
也可以post請求,post具有PUT的功能,put不指定id會保存,但post可以不指定id,這樣post的數據永遠都是新增(返回它自己生成的id),若指定id則和put功能一樣
3,查詢文檔
es的api是restful的,rest api可移步阮一峯RESTful API 設計指南
因此查詢,就是把上面put示例的PUT改爲GET即可
{ "_index": "customer", "_type": "external", "_id": "1", "_version": 2, "_seq_no": 1,_seq_no和_primary_term樂觀鎖操作相關, "_primary_term": 1, "found": true,found爲true代表找到了數據 "_source": {找到的數據的內容 "name": "John Doe" } }
樂觀鎖示例:
put或post請求後掛參:
if_seq_no=1&if_primary_term=1
執行後,_seq_no變成了4,如果比改變_seq_no再次執行上述請求
則會報錯,提示current document has seqNo [4] and primary term [1] ,現在seqNo 已經變成4了。這也就控制了一部分併發
"reason": "[1]: version conflict, required seqNo [1], primary term [1]. current document has seqNo [4] and primary term [1]",
4,更新文檔
前面post數據基礎上,加上/_update,同事請求體用{"doc":}包裝起來,同樣可實現更新操作。ps:put不行,405not allowed
加不加_update有什麼區別呢?
加了_update,除了請求體需要用{"doc":}包起來,在更新內容不變的情況下,_version和_seq_no是不變的
{ "_index": "customer", "_type": "external", "_id": "1", "_version": 3, "result": "noop",no+operation,數據沒有變化的情況下,執行_update更新時沒有操作的 "_shards": { "total": 0, "successful": 0, "failed": 0 }, "_seq_no": 4, "_primary_term": 1 }
5,刪除文檔&索引
刪除文檔
DELETE /customer/external/1
{ "_index": "customer", "_type": "external", "_id": "1", "_version": 4, "result": "deleted", "_shards": { "total": 2, "successful": 1, "failed": 0 }, "_seq_no": 5, "_primary_term": 1 }
刪除索引
DELETE customer即可
類型相當於mysql中的表,無法直接刪除
6.bulk批量API
POST customer/external/_bulk
{"index":{"_id":"1"}}//index索引一條數據,id爲1 {"name":"Jone Doe"}//index索引數據的內容 {"index":{"_id":"2"}} {"name":"Jane Doe"}
必須是post,兩行爲一個整體,語法格式
{action:{metadata}}\n
{request body }\n
{action:{metadata}}\n
{request body }\n
postman不支持換行類型的數據,移步Kibana
#! Deprecation: [types removal] Specifying types in bulk requests is deprecated. { "took" : 112,花費了112毫秒 "errors" : false,沒有發生任何錯誤 "items" : [items有兩條數據,每條數據獨立統計它自己的數據,任何一條記錄的失敗,都不會影響任何一條其他數據 { "index" : {保存的原信息體 "_index" : "customer", "_type" : "external", "_id" : "1", "_version" : 1, "result" : "created", "_shards" : { "total" : 2, "successful" : 1, "failed" : 0 }, "_seq_no" : 0, "_primary_term" : 1, "status" : 201 } }, { "index" : { "_index" : "customer", "_type" : "external", "_id" : "2", "_version" : 1, "result" : "created", "_shards" : { "total" : 2, "successful" : 1, "failed" : 0 }, "_seq_no" : 1, "_primary_term" : 1, "status" : 201 } } ] }
複雜實例POST /_bulk
POST /_bulk 沒有指定任何索引的任何類型,說明是要對整個es批量操作 {"delete":{"_index":"website","_type":"blog","_id":"123"}}刪除哪個索引哪個類型下id爲哪個的數據,刪除沒有請求體,請求信息都在元數據中 {"create":{"_index":"website","_type":"blog","_id":"123"}}創建操作 {"title":"My first blog post"}創建操作的內容 {"index":{"_index":"website","_type":"blog"}}保存記錄 {"title":"my second blog post"}保存的內容 {"update":{"_index":"website","_type":"blog","_id":"123"}}更新記錄 {"doc":{"title":"My updated blog post"}}更新的內容
運行結果:
{ "took" : 190, "errors" : false, "items" : [ { "delete" : { "_index" : "website", "_type" : "blog", "_id" : "123", "_version" : 1, "result" : "not_found", "_shards" : { "total" : 2, "successful" : 1, "failed" : 0 }, "_seq_no" : 0, "_primary_term" : 1, "status" : 404說明沒找到這條記錄 } }, { "create" : { "_index" : "website", "_type" : "blog", "_id" : "123", "_version" : 2, "result" : "created", "_shards" : { "total" : 2, "successful" : 1, "failed" : 0 }, "_seq_no" : 1, "_primary_term" : 1, "status" : 201創建成功 } }, { "index" : { "_index" : "website", "_type" : "blog", "_id" : "H62vk3oB2xZASEa9yqEY", "_version" : 1, "result" : "created", "_shards" : { "total" : 2, "successful" : 1, "failed" : 0 }, "_seq_no" : 2, "_primary_term" : 1, "status" : 201 } }, { "update" : { "_index" : "website", "_type" : "blog", "_id" : "123", "_version" : 3, "result" : "updated", "_shards" : { "total" : 2, "successful" : 1, "failed" : 0 }, "_seq_no" : 3, "_primary_term" : 1, "status" : 200 } } ] }
index和create有何區別?
create對於已有的文檔,不會插入,會報文檔已存在異常
index對於已有的文檔,會檢查_version,一致會覆蓋並_version遞增,不一致會失敗
準備了一份顧客銀行賬戶信息的虛構的JSON文檔樣本。每個文檔都有下列的schema(模式)。
我用的時候原地址以及404了,感謝評論區給出的地址
https://gitee.com/xlh_blog/common_content/blob/master/es測試數據.json,不太方便複製,也不太方便插入到代碼,我給大家放到文件裏了
導入測試數據
POST bank/account/_bulk