[TOC]
## 數據管理
ES提供近乎實時的數據操縱和搜索能力。默認情況下,從索引/更新/刪除數據到在搜索結果中出現數據之前,可以預期延遲一秒鐘(刷新間隔)。這是與其他SQL數據庫的重要區別,SQL數據庫中的數據在事務完成后立即可用。
### **添加Document: `PUT /<index>/<type>/<ID>`**
現在我們往"customer"中創建一個ID為1 的document:
```
curl -X PUT "localhost:9200/customer/_doc/1?pretty" -H 'Content-Type: application/json' -d'
{
"name": "John Doe"
}
'
```
運行結果如下:
```cmd
{
"_index" : "customer",
"_type" : "_doc",
"_id" : "1",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 0,
"_primary_term" : 1
}
```
通過上面的命令,我們已經成功的添加了一個customer document在customer index中
>[info] 值得注意的是,Elasticsearch不需要在顯式地創建一個索引之前,我們就可以創建索引文檔。在前面的示例中,如果沒有事先已經存在索引,Elasticsearch將自動創建索引。
其中,ID為可選項,假如我們沒有指定ID,ES則會自動生成一個唯一的ID,如下:
```
curl -X POST "localhost:9200/customer/_doc?pretty" -H 'Content-Type: application/json' -d'
{
"name": "Cherish"
}
'
```
運行結果如下:
```cmd
{
"_index" : "customer",
"_type" : "_doc",
"_id" : "EJLLSWcBMnaFSnQdDr7_",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 0,
"_primary_term" : 3
}
```
注意:我們這里使用了`POST`而不是`PUT`,添加后,生成的ID為`EJLLSWcBMnaFSnQdDr7_`
### **查詢Document: `GET /<index>/<type>/<ID>`**
查詢索引Customer中ID為1的Document命令:
```
curl -X GET "localhost:9200/customer/_doc/1?pretty"
```
查詢結果如下:
```cmd
{
"_index" : "customer",
"_type" : "_doc",
"_id" : "1",
"_version" : 1,
"found" : true,
"_source" : {
"name" : "John Doe"
}
}
```
根據查詢結果,ID為1的document中,name=“John Doe”
### **替換Document:`PUT /<index>/<type>/<ID>`**
添加Document時,假設ID已存在,如前面添加的ID為1,這將會覆蓋原來的記錄,如下:
```
curl -X PUT "localhost:9200/customer/_doc/1?pretty" -H 'Content-Type: application/json' -d'
{
"name": "Milton"
}
'
```
重新查詢時 `curl -X GET "localhost:9200/customer/_doc/1?pretty"`
查詢結果如下:
```cmd
{
"_index" : "customer",
"_type" : "_doc",
"_id" : "1",
"_version" : 2,
"found" : true,
"_source" : {
"name" : "Milton"
}
}
```
通過上面結果可以得知,ID為1的Document已經被替換。注意“_version”已自增
### **更新Document: `POST /<index>/<type>/<ID>/_update`**
- 更新customer中ID為1的Document中的name為“Apple”
```
curl -X POST "localhost:9200/customer/_doc/1/_update?pretty" -H 'Content-Type: application/json' -d'
{
"doc": { "name": "Apple" }
}
'
```
- 更新customer中ID為1的Document中的name為“Tom”,并且添加新的字段age
```
curl -X POST "localhost:9200/customer/_doc/1/_update?pretty" -H 'Content-Type: application/json' -d'
{
"doc": { "name": "Tom", "age": 20 }
}
'
```
- 使用Scripts腳本更新Document中的age+5
```
curl -X POST "localhost:9200/customer/_doc/1/_update?pretty" -H 'Content-Type: application/json' -d'
{
"script" : "ctx._source.age += 5"
}
'
```
上面例子中`ctx._source`代表被更新的對象本身。
### **刪除Document: `DELETE /<index>/<type>/<ID>`**
刪除customer中ID為2的Document
```
curl -X DELETE "localhost:9200/customer/_doc/2?pretty"
```
假設文檔不存在,則返回如下
```
{
"_index" : "customer",
"_type" : "_doc",
"_id" : "2",
"_version" : 1,
"result" : "not_found",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 0,
"_primary_term" : 1
}
```
### **批量處理:`POST <index>/<type>/_bulk`**
在ES中,除了上面針對單個Document增、刪、改、查之外,ES還提供了一個強大的API`_bulk`,它具備了批量操作的能力。
1. 批量添加兩個Document
```
curl -X POST "localhost:9200/customer/_doc/_bulk?pretty" -H 'Content-Type: application/json' -d'
{"index":{"_id":"11"}}
{"name": "Milton" }
{"index":{"_id":"22"}}
{"name": "Cherish" }
'
```
運行結果如下:
```cmd
{
"took" : 272,
"errors" : false,
"items" : [
{
"index" : {
"_index" : "customer",
"_type" : "_doc",
"_id" : "11",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 0,
"_primary_term" : 3,
"status" : 201
}
},
{
"index" : {
"_index" : "customer",
"_type" : "_doc",
"_id" : "22",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 1,
"_primary_term" : 3,
"status" : 201
}
}
]
}
```
通過上面可知,已經新增的兩條Document
2. 批量更新和刪除Document
```
curl -X POST "localhost:9200/customer/_doc/_bulk?pretty" -H 'Content-Type: application/json' -d'
{"update":{"_id":"11"}}
{"doc": { "name": "Milton Love Cherish" } }
{"delete":{"_id":"22"}}
'
```
運行結果如下:
```cmd
{
"took" : 269,
"errors" : false,
"items" : [
{
"update" : {
"_index" : "customer",
"_type" : "_doc",
"_id" : "11",
"_version" : 2,
"result" : "updated",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 1,
"_primary_term" : 3,
"status" : 200
}
},
{
"delete" : {
"_index" : "customer",
"_type" : "_doc",
"_id" : "22",
"_version" : 2,
"result" : "deleted",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 2,
"_primary_term" : 3,
"status" : 200
}
}
]
}
```
通過上面的運行結果可知,更新與刪除都成功了。