[TOC]
## 数据管理
ES提供近乎实时的数据操纵和搜索能力。默认情况下,从索引/更新/删除数据到在搜索结果中出现数据之前,可以预期延迟一秒钟(刷新间隔)。这是与其他SQL数据库的重要区别,SQL数据库中的数据在事务完成后立即可用。
### **添加Document: `PUT ///`**
现在我们往"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 ///`**
查询索引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 ///`**
添加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 _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 ///`**
删除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 //_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
}
}
]
}
```
通过上面的运行结果可知,更新与删除都成功了。