标签:get请求 doc 实现原理 src disk 应用 link shard har
1)PUT /index/type/id
创建文档&替换文档,是一样的语法。一般对应到应用程序中,每次的执行流程基本是这样的:
(1)应用程序先发起一个get请求,获取到document,展示到前台界面,供用户查看和修改
(2)用户在前台界面修改数据,发送到后台
(3)后台代码,会将用户修改的数据在内存中进行执行,然后封装好修改后的全量数据
(4)然后发送PUT请求,到es中,进行全量替换
(5)es将老的document标记为deleted,然后重新创建一个新的document
2)partial update
语法格式:
post /index/type/id/_update 
{
   "doc": {
      "要修改的少数几个field即可,不需要全量的数据"
   }
}3)图解partial update实现原理以及其优点
4)partial update操作示例
PUT /test_index/test_type/10
{
  "test_field1": "test1",
  "test_field2": "test2"
}
{
  "_index": "test_index",
  "_type": "test_type",
  "_id": "10",
  "_version": 1,
  "result": "created",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  },
  "created": true
}
POST /test_index/test_type/10/_update
{
  "doc": {
    "test_field2": "updated test2"
  }
}
{
  "_index": "test_index",
  "_type": "test_type",
  "_id": "10",
  "_version": 2,
  "result": "updated",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  }
}
GET /test_index/test_type/10
{
  "_index": "test_index",
  "_type": "test_type",
  "_id": "10",
  "_version": 2,
  "found": true,
  "_source": {
    "test_field1": "test1",
    "test_field2": "updated test2"
  }
}es是有个内置的脚本支持,可以基于groovy脚本实现各种各样的复杂操作
创建测试数据:
PUT /test_index/test_type/11
{
  "num": 0,
  "tags": []
}POST /test_index/test_type/11/_update
{
  "script": "ctx._source.num+=1"   #使用内置脚本使num加1
}
{
  "_index": "test_index",
  "_type": "test_type",
  "_id": "11",
  "_version": 2,
  "result": "updated",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  }
}
GET /test_index/test_type/11
{
  "_index": "test_index",
  "_type": "test_type",
  "_id": "11",
  "_version": 2,
  "found": true,
  "_source": {
    "num": 1,
    "tags": []
  }
}在es的安装目录下config/scripts下编写groovy脚本,名称为test-add-tags.groovy
ctx._source.tags+=new_tag
在界面操作:
POST /test_index/test_type/11/_update
{
  "script": {
    "lang": "groovy",  #缺少,报错Unable to find on disk file script [test-add-tags] using lang [painless]
    "file": "test-add-tags",
    "params": {
      "new_tag": "tag1"
    }
  }
}
GET /test_index/test_type/11
{
  "_index": "test_index",
  "_type": "test_type",
  "_id": "11",
  "_version": 3,
  "found": true,
  "_source": {
    "num": 1,
    "tags": [
      "tag1"   #新加的tag
    ]
  }
}Elasticsearch系统学习(八)-partial update
标签:get请求 doc 实现原理 src disk 应用 link shard har
原文地址:https://www.cnblogs.com/hujinzhong/p/11448090.html