版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_27868061/article/details/84944081
更新API允许基于提供的脚本更新文档。 该操作从索引获取文档(与分片并置),运行脚本(使用可选的脚本语言和参数),并对结果进行索引(也允许删除或忽略操作)。 它使用版本控制来确保在“get”和“reindex”期间没有发生更新。
请注意,此操作仍然意味着文档的完全重新索引,它只是删除了一些网络往返,并减少了get和索引之间版本冲突的可能性。 需要启用_source字段才能使此功能正常工作。
之前使用PUT方法创建文档时,如果文档已存在,则指定更新操作,这里主要使用脚本进行更新
示例:
POST /spring-elasticsearch-demo/person/2/_update HTTP/1.1
Host: localhost:9200
Content-Type: application/json
cache-control: no-cache
Postman-Token: f8f033b6-ceb3-4a7f-8bd5-636188bba148
{
"script" : {
"source": "ctx._source.name += params.name",
"lang": "painless",
"params" : {
"name" : "泡泡糖"
}
}
}
{
"_index": "spring-elasticsearch-demo",
"_type": "person",
"_id": "2",
"_version": 2,
"result": "updated",
"_shards": {
"total": 2,
"successful": 2,
"failed": 0
},
"_seq_no": 2,
"_primary_term": 1
}
GET /spring-elasticsearch-demo/person/2 HTTP/1.1
Host: localhost:9200
cache-control: no-cache
Postman-Token: e69cbc38-6296-493f-8636-63053495525b
{
"_index": "spring-elasticsearch-demo",
"_type": "person",
"_id": "2",
"_version": 2,
"found": true,
"_source": {
"id": "2",
"name": "大大泡泡糖",
"description": "天天向上",
"age": 1,
"create": 1535760000000
}
}
可以看到脚本为字符串形式,ctx._source可以引用到源文档,params可以引用到参数,可以使用一些操作符,如+=
1.添加新字段
POST /spring-elasticsearch-demo/person/2/_update HTTP/1.1
Host: localhost:9200
Content-Type: application/json
cache-control: no-cache
Postman-Token: 24d28f25-a9ad-4483-822d-55a8c191ee47
{
"script" : {
"source": "ctx._source.colors = ['red']",
"lang": "painless"
}
}
{
"_index": "spring-elasticsearch-demo",
"_type": "person",
"_id": "2",
"_version": 3,
"result": "updated",
"_shards": {
"total": 2,
"successful": 2,
"failed": 0
},
"_seq_no": 3,
"_primary_term": 1
}
2.为数组添加新的元素
POST /spring-elasticsearch-demo/person/2/_update HTTP/1.1
Host: localhost:9200
Content-Type: application/json
cache-control: no-cache
Postman-Token: be56dc9d-69c5-4fec-a6fb-d6739c16fcce
{
"script" : {
"source": "ctx._source.colors.add(params.color)",
"lang": "painless",
"params":{
"color":"green"
}
}
}
{
"_index": "spring-elasticsearch-demo",
"_type": "person",
"_id": "2",
"_version": 4,
"result": "updated",
"_shards": {
"total": 2,
"successful": 2,
"failed": 0
},
"_seq_no": 4,
"_primary_term": 1
}
3.部分文档更新
更新API还支持传递部分文档,该部分文档将合并到现有文档中(简单的递归合并,对象的内部合并,替换核心“键/值”和数组)。 要完全替换现有文档,应使用索引API。 以下部分更新会向现有文档添加新字段:
POST /spring-elasticsearch-demo/person/2/_update HTTP/1.1
Host: localhost:9200
Content-Type: application/json
cache-control: no-cache
Postman-Token: 2fe50386-4224-4969-8c95-d15e01460138
{
"doc":{
"name":"大宝SODMI"
}
}
{
"_index": "spring-elasticsearch-demo",
"_type": "person",
"_id": "2",
"_version": 5,
"result": "updated",
"_shards": {
"total": 2,
"successful": 2,
"failed": 0
},
"_seq_no": 5,
"_primary_term": 1
}
此时要求,将需要递归合并更新的部分文档,设置为doc字段,此时如果部分文档与之前相同,则不需要更新,执行noop操作
POST /spring-elasticsearch-demo/person/2/_update HTTP/1.1
Host: localhost:9200
Content-Type: application/json
cache-control: no-cache
Postman-Token: b746fddd-6bce-4f1e-896d-42253b2b74cb
{
"doc":{
"name":"大宝SODMI"
}
}
{
"_index": "spring-elasticsearch-demo",
"_type": "person",
"_id": "2",
"_version": 5,
"result": "noop",
"_shards": {
"total": 0,
"successful": 0,
"failed": 0
}
}
可以通过如下设置,禁用此行为:
{
"doc":{
"name":"大宝SODMI"
},
"detect_noop": false
}
4.upserts
upserts字段可以当要更新文档不存在时,作为新文档插入
POST /spring-elasticsearch-demo/person/3/_update HTTP/1.1
Host: localhost:9200
Content-Type: application/json
cache-control: no-cache
Postman-Token: 3e8df4b9-4982-414f-a2ff-7000e4d0cb1d
{
"script" : {
"source": "ctx._source.colors.add(params.color)",
"lang": "painless",
"params":{
"color":"green"
}
},
"upsert":{
"name":"不存在就是我",
"description":"我是描述"
}
}
{
"_index": "spring-elasticsearch-demo",
"_type": "person",
"_id": "3",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 2,
"failed": 0
},
"_seq_no": 0,
"_primary_term": 1
}