Elasticsearch提供了近實時的數據操做和搜索能力。默認狀況下,從你開始索引/更新/刪除你的數據到出現搜索結果的時間會有一秒的延時(刷新間隔)。這個與其它的SQL數據庫平臺在一個事務完成後當即獲取到數據這一點上有很大的優點。html
咱們以前已經看過如何將一個文檔放入索引中,讓咱們再次回憶一下那個命令:數據庫
Http請求:json
PUT /customer/doc/1?pretty { "name": "John Doe" }
curl命令:網絡
curl -XPUT 'localhost:9200/customer/doc/1?pretty&pretty' -H 'Content-Type: application/json' -d' { "name": "John Doe" } '
Kibana Console:app
http://localhost:5601/app/kibana#/dev_tools/console?load_from=https://www.elastic.co/guide/en/elasticsearch/reference/current/snippets/_modifying_your_data/1.json
再次補充說明一下,上面的請求將會將一個ID爲1的文檔加入customer索引。若是咱們再次執行上面的請求,以相同的文檔內容或者是不一樣的,Elasticsearch將會用這個新文檔替換以前的文檔(就是以相同的ID從新
加入索引)。curl
Http請求內容:elasticsearch
PUT /customer/doc/1?pretty { "name": "Jane Doe" }
curl命令:ide
curl -XPUT 'localhost:9200/customer/doc/1?pretty&pretty' -H 'Content-Type: application/json' -d' { "name": "Jane Doe" } '
Kibana Console:ui
http://localhost:5601/app/kibana#/dev_tools/console?load_from=https://www.elastic.co/guide/en/elasticsearch/reference/current/snippets/_modifying_your_data/2.json
上述操做將ID爲1的文檔的name屬性從「John Doe」改爲了「Jane Doe」。設想另外一種場景,咱們使用一個不一樣的ID,這樣的話將會建立一個新的文檔,而以前的文檔仍是保持原樣。url
Http請求:
PUT /customer/doc/2?pretty { "name": "Jane Doe" }
curl命令:
curl -XPUT 'localhost:9200/customer/doc/2?pretty&pretty' -H 'Content-Type: application/json' -d' { "name": "Jane Doe" } '
Kibana Console:
http://localhost:5601/app/kibana#/dev_tools/console?load_from=https://www.elastic.co/guide/en/elasticsearch/reference/current/snippets/_modifying_your_data/3.json
上述操做將一個ID爲2的文檔加入索引。
當將文檔加入索引時,ID部分並非必須的。若是沒有指定,Elasticsearch將會生產一個隨機的ID,而後使用它去索引文檔。實際Elasticsearch生成的ID(或者是咱們明確指定的)將會在API調用成功後返回。
以下這個例子演示如何使用隱式的ID將一個文檔加入索引:
Http請求:
POST /customer/doc?pretty { "name": "Jane Doe" }
curl命令:
curl -XPOST 'localhost:9200/customer/doc?pretty&pretty' -H 'Content-Type: application/json' -d' { "name": "Jane Doe" } '
Kibana Console:
http://localhost:5601/app/kibana#/dev_tools/console?load_from=https://www.elastic.co/guide/en/elasticsearch/reference/current/snippets/_modifying_your_data/4.json
注意在上面的例子中,當咱們沒有明確指定ID的時候,咱們須要使用POST
方法代替PUT
來發送請求。
除了可以新增和替換文檔,咱們也能夠更新文檔。注意雖然Elasticsearch在底層並無真正更新文檔,而是當咱們更新文檔時,Elasticsearch首先去刪除舊的文檔,而後加入新的文檔。
以下的例子演示如何去更新咱們的以前ID爲1的文檔,在這裏將name屬性改成「Jane Doe」:
Http請求內容:
POST /customer/doc/1/_update?pretty { "doc": { "name": "Jane Doe" } }
curl命令:
curl -XPOST 'localhost:9200/customer/doc/1/_update?pretty&pretty' -H 'Content-Type: application/json' -d' { "doc": { "name": "Jane Doe" } } '
Kibana Console:
http://localhost:5601/app/kibana#/dev_tools/console?load_from=https://www.elastic.co/guide/en/elasticsearch/reference/current/snippets/_updating_documents/1.json
以下示例演示如何更新咱們以前ID爲1的文檔,修改name屬性爲「Jane Doe」,並同時添加新的age屬性:
Http請求內容:
POST /customer/doc/1/_update?pretty { "doc": { "name": "Jane Doe", "age": 20 } }
curl命令:
curl -XPOST 'localhost:9200/customer/doc/1/_update?pretty&pretty' -H 'Content-Type: application/json' -d' { "doc": { "name": "Jane Doe", "age": 20 } } '
Kibana Console:
http://localhost:5601/app/kibana#/dev_tools/console?load_from=https://www.elastic.co/guide/en/elasticsearch/reference/current/snippets/_updating_documents/2.json
更新操做也可使用簡單的腳原本執行。以下的示例使用一個腳本將age增長了5:
Http請求:
POST /customer/doc/1/_update?pretty { "script" : "ctx._source.age += 5" }
curl命令:
curl -XPOST 'localhost:9200/customer/doc/1/_update?pretty&pretty' -H 'Content-Type: application/json' -d' { "script" : "ctx._source.age += 5" } '
Kibana Console:
http://localhost:5601/app/kibana#/dev_tools/console?load_from=https://www.elastic.co/guide/en/elasticsearch/reference/current/snippets/_updating_documents/3.json
在上面的示例中,ctx._source
指代的是當前須要被更新的source文檔。
Elasticsearch提供了一次更新多個文檔的功能,經過使用查詢條件(好比SQL的UPDATE-WHERE語句)。詳情查看docs-update-by-query API
刪除一個文檔操做至關的直截了當。以下的示例演示瞭如何刪除咱們以前ID爲2的文檔:
Http請求內容:
DELETE /customer/doc/2?pretty
curl命令:
curl -XDELETE 'localhost:9200/customer/doc/2?pretty&pretty'
Kibana Console:
http://localhost:5601/app/kibana#/dev_tools/console?load_from=https://www.elastic.co/guide/en/elasticsearch/reference/current/snippets/_deleting_documents/1.json
查看_delete_by_query API去刪除匹配特定條件的全部的文檔。有一個值得注意的的地方是,直接刪除整個索引比經過Query API刪除索引中的全部文檔更高效。
除了在單個文檔上執行索引,更新和刪除操做外,Elasticsearch還提供了批操做的功能,經過使用 _bulk
API完成。這個功能很是重要,由於它提供了一種很是高效的機制去經過更少的網絡切換儘量快的執行多個操做。
做爲一個快速入門示例,以下請求在一個批操做中建立了兩個文檔:
Http請求內容:
POST /customer/doc/_bulk?pretty {"index":{"_id":"1"}} {"name": "John Doe" } {"index":{"_id":"2"}} {"name": "Jane Doe" }
curl命令:
curl -XPOST 'localhost:9200/customer/doc/_bulk?pretty&pretty' -H 'Content-Type: application/json' -d' {"index":{"_id":"1"}} {"name": "John Doe" } {"index":{"_id":"2"}} {"name": "Jane Doe" } '
Kibana Console:
http://localhost:5601/app/kibana#/dev_tools/console?load_from=https://www.elastic.co/guide/en/elasticsearch/reference/current/snippets/_batch_processing/1.json
以下的示例在一個批操做中首先更新ID爲1的文檔,而後刪除ID爲2的文檔:
Http請求內容:
POST /customer/doc/_bulk?pretty {"update":{"_id":"1"}} {"doc": { "name": "John Doe becomes Jane Doe" } } {"delete":{"_id":"2"}}
curl命令:
curl -XPOST 'localhost:9200/customer/doc/_bulk?pretty&pretty' -H 'Content-Type: application/json' -d' {"update":{"_id":"1"}} {"doc": { "name": "John Doe becomes Jane Doe" } } {"delete":{"_id":"2"}} '
Kibana Console:
http://localhost:5601/app/kibana#/dev_tools/console?load_from=https://www.elastic.co/guide/en/elasticsearch/reference/current/snippets/_batch_processing/2.json
注意上面的刪除操做,刪除時只須要指定被刪除的文檔的ID便可,不須要指定對應的source內容。
批處理API不會由於單條指令失敗而所有失敗。若是裏面有單條指令由於一些緣由失敗了,那麼整個批處理還會繼續執行它後面剩餘的指令。當批處理API返回時,它會提供每條指令的執行狀態(以發送時的順序),以便你能夠檢查一個特定的指令是否失敗。