Elasticsearch系统学习(八)-partial update

一、partial update介绍

1.1、什么是partial update?

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"
  }
}

二、基于groovy脚本实现partial update

es是有个内置的脚本支持,可以基于groovy脚本实现各种各样的复杂操作

创建测试数据:

PUT /test_index/test_type/11
{
  "num": 0,
  "tags": []
}

2.1、使用内置脚本

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": []
  }
}

2.2、使用外部脚本

在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
    ]
  }
}

原文地址:https://www.cnblogs.com/hujinzhong/p/11448090.html

时间: 2024-08-30 01:50:24

Elasticsearch系统学习(八)-partial update的相关文章

Elasticsearch系统学习(一)-elasticsearch简单介绍和核心概念

一.ES简单介绍 1.1.es功能 (1)分布式的搜索引擎和数据分析引擎 搜索:百度,网站的站内搜索,IT系统的检索 数据分析:电商网站,最近7天牙膏这种商品销量排名前10的商家有哪些:新闻网站,最近1个月访问量排名前3的新闻版块是哪些 分布式,搜索,数据分析 (2)全文检索,结构化检索,数据分析 全文检索:我想搜索商品名称包含牙膏的商品,select * from products where product_name like "%牙膏%" 结构化检索:我想搜索商品分类为日化用品的

Elasticsearch系统学习(三)-基本操作

一.document数据格式 (1)应用系统的数据结构都是面向对象的,复杂的 (2)对象数据存储到数据库中,只能拆解开来,变为扁平的多张表,每次查询的时候还得还原回对象格式,相当麻烦 (3)ES是面向文档的,文档中存储的数据结构,与面向对象的数据结构是一样的,基于这种文档数据结构,es可以提供复杂的索引,全文检索,分析聚合等功能 (4)es的document用json数据格式来表达 { "email": "[email protected]", "firs

Elasticsearch系统学习(七)-ES并发控制

一.ES并发控制原理 1.1.ES并发冲突问题 1.2.悲观锁与乐观锁并发控制图解 ES内部基于_version进行乐观锁并发控制: 二.并发控制实践 2.1.基于_version进行乐观锁并发控制 1)构建数据 PUT /test_index/test_type/7 { "test_field": "test test" } { "_index": "test_index", "_type": "

Elasticsearch系统学习(十一)-mapping

一.知识铺垫 1.1.搜索结果各项含义 GET /_search { "took": 6, #整个搜索请求花费了多少毫秒 "timed_out": false, #是否超时,可以手动指定超时时间 "_shards": { #默认一个搜索请求,会打到index的所有primary shard上去,每个primary shard都可能会有一个或多个replic shard,所以请求也可以到primary shard的其中一个replica shard

Elasticsearch学习笔记(九)partial update

一.什么是partial update? PUT /index/type/id,创建文档&替换文档,就是一样的语法 一般对应到应用程序中,每次的执行流程基本是这样的: (1)应用程序先发起一个get请求,获取到document,展示到前台界面,供用户查看和修改 (2)用户在前台界面修改数据,发送到后台 (3)后台代码,会将用户修改的数据在内存中进行执行,然后封装好修改后的全量数据 (4)然后发送PUT请求,到es中,进行全量替换 (5)es将老的document标记为deleted,然后重新创建

Elasticsearch技术解析与实战(七)Elasticsearch partial update

普通的partial update 1.插入测试数据 PUT /test_index/test_type/10 { "test_field1": "test1", "test_field2": "test2" } 2.更新 POST /test_index/test_type/10/_update { "doc": { "test_field2": "updated test2

ElasticSearch partial update+mget+bulk

一.partial update 1.什么是partial update? PUT /index/type/id,创建文档&替换文档,就是一样的语法 一般对应到应用程序中,每次的执行流程基本是这样的: (1)应用程序先发起一个get请求,获取到document,展示到前台界面,供用户查看和修改(2)用户在前台界面修改数据,发送到后台(3)后台代码,会将用户修改的数据在内存中进行执行,然后封装好修改后的全量数据(4)然后发送PUT请求,到es中,进行全量替换(5)es将老的document标记为d

Elasticsearch 顶尖高手(19)—基于groovy脚本执行partial update

es,其实是有内置脚本支持的, 可以基于groovy脚本实现各种各样的复杂操作 基于groovy脚本,如何执行partial update 创建数据 PUT /test_index/test_type/11 {   "num":0,   "tags":[] } 1.内置脚本 POST /test_index/test_type/11/_update { "script":"cts._source.num+=1" } 2.外部脚

Oracle学习(八):处理数据

1.知识点:能够对比以下的录屏进行阅读 SQL> --SQL语句 SQL> --1. DML语句(Data Manipulation Language 数据操作语言): insert update delete select SQL> --2. DDL语句(Data Definition Language 数据定义语言): create/alter/drop/truncate table SQL> -- create/drop view,create/drop index(sequ