GET /_cat/indices?v
返回內容以下:html
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size yellow open .kibana XYZPR5XGQGWj8YlyZ1et_w 1 1 1 0 3.1kb 3.1kb
能夠看到在集羣中有一個索引git
如今讓咱們建立一個名叫 customer 的索引,而後再次列出全部的索引github
PUT /customer?pretty
GET /_cat/indices?v
執行第一行返回如下內容,這裏咱們使用PUT謂詞建立了一個名叫 customer 的索引,在後面跟上 pretty 表示若是有數據返回的話,用格式化後的JSON返回數據api
{ "acknowledged": true, "shards_acknowledged": true }
執行第二行返回如下內容,結果告訴咱們,已經建立了一個名叫 customer 的索引,它有5個主分片和1個複製分片(默認狀況下是1個),在這個索引中尚未文檔。網絡
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size yellow open .kibana XYZPR5XGQGWj8YlyZ1et_w 1 1 1 0 3.1kb 3.1kb yellow open customer M8i1ZxhsQJqk7HomOA7c_Q 5 1 0 0 650b 650b
可能你已經注意到 customer 索引的健康值被標記爲 yellow ,回顧咱們前面討論的內容, yellow 表示該索引的複製分片(副本)尚未被分配。該索引出現這種狀況的緣由是, Elasticsearch 默認會爲該索引建立1個副本,因爲此時咱們只有1個節點,那麼這副本就無法被分配(爲了高可用),直到之後爲該集羣加入了另外一個節點。一旦該副本分配到了另外一個節點,該索引的健康狀態就會變成 green 。iphone
接下來咱們放一些東西到 customer 索引中。以前提過的,爲了索引某個文檔,咱們必須告訴 Elasticsearch ,該文檔應該屬於該索引的哪一個類型,下面咱們索引一個簡單的文檔到 customer 索引,類型名稱爲 external , 而且ID爲1elasticsearch
PUT /customer/external/1?pretty { "name": "John Doe" }
返回內容以下:ide
{ "_index": "customer", "_type": "external", "_id": "1", "_version": 1, "result": "created", "_shards": { "total": 2, "successful": 1, "failed": 0 }, "created": true }
從以上能夠看出,一個新的客戶文檔成功被索引到 customer索引的 extenal 類型中,而且咱們在索引的時候指定文檔的內部id值爲1。學習
值得注意的是, Elasticsearch 不須要在你索引文檔到某個索引以前,明確的建立一個索引。好比上一個例子,若是 customer索引不存在, Elasticsearch將自動建立該索引。ui
再來看下咱們剛剛索引的文檔
GET /customer/external/1?pretty
返回內容以下:
{ "_index": "customer", "_type": "external", "_id": "1", "_version": 1, "found": true, "_source": { "name": "John Doe" } }
這裏比較特殊的是found字段,它說明咱們查到了一個id爲1的文檔,另外一特殊的字段_source,保存了在上一個步驟索引的的文檔。
如今讓咱們刪除剛剛已經建立的索引,並再次查看全部索引。
DELETE /customer?pretty
GET /_cat/indices?v
第一行返回內容如下:
{ "acknowledged": true }
第二行返回內容以下:
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size yellow open .kibana XYZPR5XGQGWj8YlyZ1et_w 1 1 1 0 3.1kb 3.1kb
從以上內容能夠看到咱們的 customer索引已經被刪除了。
在繼續學習以前,讓咱們快速回顧一下,本節學的API命令
PUT /customer PUT /customer/external/1 { "name": "John Doe" } GET /customer/external/1 DELETE /customer
若是仔細學習了以上命令,應該會發現 elasticsearch 訪問數據所使用的模式,歸納以下:
<REST Verb> /<Index>/<Type>/<ID>
使用REST 訪問模式,在全部的API命令中是十分廣泛的,若是你能夠簡單記住它,對於掌握 Elasticsearch ,那麼已經開了一個好頭。
Elasticsearch 具備近實時的操做和查詢數據的能力,默認狀況下,從你索引,更新或者刪除你的數據到用戶能夠搜索到新的結果這個過程大概須要1秒(基於refresh 頻率)。它們和相似SQL這樣的平臺不同,SQL的數據在事務完成後就立刻就生效,不會有延遲。
以前已經演示了怎麼索引單個文檔,再來回顧一下:
PUT /customer/external/1?pretty { "name": "John Doe" }
上面的命令將會索引指定文檔到 customer 索引的 external 類型,文檔的id值是1。若是咱們用不一樣的文檔內容(或者相同)再次執行上面的命令,elasticsearch將會用一個新的文檔取代舊的文檔(即重建索引)。
PUT /customer/external/1?pretty { "name": "Jane Doe" }
上面的操做把id爲1的文檔的name字段由"john doe"改爲"jane doe"。另外一方面,若是咱們使用不一樣的id執行上述命令,將會建立一個新的文檔,舊的文檔會保持原樣。
PUT /customer/external/2?pretty { "name": "Jane Doe" }
以上操做索引了一個新的id爲2文檔。
索引新文檔的時候,id值是可選的。若是沒有指定, elasticsearch 將會爲文檔生成一個隨機的id。實際生成的id將會保存在調用api接口的返回結果中。
下面的例子展現不指定文檔id的時候是如何索引文檔的:
POST /customer/external?pretty { "name": "Jane Doe" }
返回內容以下:
{ "_index": "customer", "_type": "external", "_id": "AVyc9L6dtgHksqXKpTlM", "_version": 1, "result": "created", "_shards": { "total": 2, "successful": 1, "failed": 0 }, "created": true }
注意,在上面的例子中,由於沒有指定id,咱們須要使用POST謂詞取代以前的PUT謂詞。
除了能夠索引和替換文檔以外,咱們還能夠更新文檔。注意, elasticsearch 並無在原來的文檔基礎上進行更新,每當進行更新時, Elasticsearch 將刪除舊的文檔,而後索引新的文檔。如下例子演示瞭如何更新文檔,把以前ID爲1的name字段改成"Jane Doe":
POST /customer/external/1/_update?pretty { "doc": { "name": "Jane Doe" } }
如下例子演示瞭如何更新先前ID爲1的文檔,改變name字段爲"Jane Doe" 的同時添加age字段
POST /customer/external/1/_update?pretty { "doc": { "name": "Jane Doe", "age": 20 } }
也可使用簡單的腳原本執行更新。如下示例使用腳本將年齡增長5:
POST /customer/external/1/_update?pretty { "script" : "ctx._source.age += 5" }
在以上例子中, ctx._source 指當前即將被更新的源文檔。請注意,在撰寫本文時,只能一次更新單個文檔。未來, Elasticsearch 可能會提供經過查詢條件(如SQL UPDATE-WHERE
語句)更新多個文檔的功能。
刪除文檔很是簡單,如下例子演示了怎麼刪除 customer 索引下ID爲2的文檔,查閱Delete By Query API 刪除與特定查詢匹配的全部文檔。值得注意的是,直接刪除整個索引比經過query api 刪除全部文檔更高效。
DELETE /customer/external/2?pretty
除了可以索引,更新和刪除單個文檔以外, Elasticsearch 也提供了使用 _bulk API 批量執行上述任何操做的功能。這個功能是很是重要的,由於它提供了一個很是有效的機制來儘量快地進行多個操做,而且儘量減小網絡的往返行程。簡單舉個例子,下面會在一個 bulk操做中索引兩個文檔:
POST /customer/external/_bulk?pretty {"index":{"_id":"1"}} {"name": "John Doe" } {"index":{"_id":"2"}} {"name": "Jane Doe" }
返回內容以下:
{ "took": 27, "errors": false, "items": [ { "index": { "_index": "customer", "_type": "external", "_id": "1", "_version": 1, "result": "created", "_shards": { "total": 2, "successful": 1, "failed": 0 }, "created": true, "status": 201 } }, { "index": { "_index": "customer", "_type": "external", "_id": "2", "_version": 1, "result": "created", "_shards": { "total": 2, "successful": 1, "failed": 0 }, "created": true, "status": 201 } } ] }
下面的例子會在一個操做內更新第一個文檔同時刪除第二個文檔:
POST /customer/external/_bulk?pretty {"update":{"_id":"1"}} {"doc": { "name": "John Doe becomes Jane Doe" } } {"delete":{"_id":"2"}}
返回內容以下:
{ "took": 25, "errors": false, "items": [ { "update": { "_index": "customer", "_type": "external", "_id": "1", "_version": 2, "result": "updated", "_shards": { "total": 2, "successful": 1, "failed": 0 }, "status": 200 } }, { "delete": { "found": true, "_index": "customer", "_type": "external", "_id": "2", "_version": 2, "result": "deleted", "_shards": { "total": 2, "successful": 1, "failed": 0 }, "status": 200 } } ] }
注意以上的刪除操做,在它以後並無相應的源文檔,由於只須要文檔的ID就能刪除。
若是某個操做因某些緣由執行失敗,不會影響後面的操做,它會繼續執行剩下的操做。api返回結果時,每個操做都會提供狀態(和接收到的順序一致),你能夠經過這個狀態檢查操做是否執行成功。
一、查看集羣中的索引, GET /_cat/indices?v
二、建立索引 PUT /product?pretty 。(es會自動創建index和type,不須要提早建立,並且es默認會對document每一個field都創建倒排索引,讓其能夠被搜索)
三、刪除索引, DELETE /test_index?pretty
一、新增商品
PUT /product/goods/1 { "goods_id": "10", "goods_name": "索愛C702c", "createTime": "2016-12-21", "goods_type": [ "華爲", "樂視", "小米" ] }
二、查詢商品, GET /product/goods/1
三、修改商品
方式1:替換文檔(和建立同樣,全部字段必須寫全)
PUT /product/goods/4 { "goods_id": "40", "goods_name": "聯想筆記本", "createTime": "2017-05-21", "goods_type": [ "電腦" ] }
字段不寫全的狀況
方式2:更新文檔
POST /product/goods/1/_update { "doc":{ "goods_name":"iphone手機" } }
比較建立,更新,替換文檔返回結果:
四、刪除商品, DELETE /product/goods/4
官方文檔
https://www.elastic.co/guide/en/elasticsearch/reference/current/_exploring_your_cluster.html
https://www.elastic.co/guide/en/elasticsearch/reference/current/_modifying_your_data.html
參考文檔
https://github.com/13428282016/elasticsearch-CN/wiki/es-gettting-started