Indexing/Replacing Documents
插入或替換document认然。
ElastiSearch提供幾乎實(shí)時(shí)的數(shù)據(jù)操作與搜索功能徙垫。它的數(shù)據(jù)在事務(wù)完成之后立即可用腾它。
PUT /customer/_doc/1?pretty
{
"name": "John Doe"
}
如果兩次插入的_doc的值是一樣的乒裆,那么ElasticSearch會覆蓋這個(gè)文檔的值璃俗,_version的值會+1.
POST /customer/_doc?pretty
{
"name": "Jane Doe"
}
索引時(shí)奴璃,ID部分是可選的,如果沒有指定ID城豁,那么ElasticSearch就會生成一個(gè)隨機(jī)的ID苟穆。
!!沒有ID的時(shí)候要使用POST雳旅!不是PUT
Updating Documents
無論什么時(shí)候跟磨,ElasticSearch的更新都不是真正的更新,而是刪了舊文檔攒盈,然后將更新后的內(nèi)容應(yīng)用到新文檔之中抵拘。
更新例子:
注意是POST請求。
POST /customer/_doc/1/_update?pretty
{
"doc": { "name": "Jane Doe" }
}
POST /customer/_doc/1/_update?pretty
{
"doc": { "name": "Jane Doe", "age": 20 }
}
這個(gè)實(shí)例在更新名字的同時(shí)添加了一個(gè)名字為“age”的filed型豁。
updates也可以使用一些類似的腳本言語僵蛛。
POST /customer/_doc/1/_update?pretty
{
"script" : "ctx._source.age += 5"
}
ctx._source代表current source document(當(dāng)前文檔)的source。
還有一些SQL UPDATE-WHERE用法迎变,參見docs_update_by_query API
https://www.elastic.co/guide/en/elasticsearch/reference/6.2/docs-update-by-query.html
Deleting Document
DELETE /customer/_doc/2?pretty
_delete_by_query API用于刪除特定的查詢匹配文檔充尉。
詳情參見:
https://www.elastic.co/guide/en/elasticsearch/reference/6.2/docs-delete-by-query.html
Batch Processing批量處理
ElasticSearch提供_bulk API 用于批量處理。
POST /customer/_doc/_bulk?pretty
{"index":{"_id":"1"}}
{"name": "John Doe" }
{"index":{"_id":"2"}}
{"name": "Jane Doe" }
這個(gè)例子index了兩個(gè)customer
POST /customer/_doc/_bulk?pretty
{"update":{"_id":"1"}}
{"doc": { "name": "John Doe becomes Jane Doe" } }
{"delete":{"_id":"2"}}
批量的更新操作和刪除操作衣形。
批量操作不會因?yàn)槠渲械囊粋€(gè)操作失敗而整個(gè)失敗驼侠。如果它其中有一個(gè)失敗,則繼續(xù)處理其它的操作谆吴,它會返回每個(gè)操作的狀態(tài)倒源,可以用來檢測某個(gè)特定的操作是否失敗。