Elasticsearch5.20 基本语法之修改数据
2017-02-18 19:07
375 查看
之前记录的都是最基本的入门语法,这里开始修改数据
创建和替换documents
执行第一条PUT:
在customer的external中存入一条id=1的数据
返回信息:
result=created created=true 说明是创建数据
version=1 版本号为1
执行GET命令:
获取在customer的external中id=1的数据
返回信息:
version=1 版本号为1
name=John Doe
执行第二条PUT:
返回信息:
result=updated created=false 说明是修改数据
version=2 版本号为2
再次执行GET命令:
返回信息:
version=2 版本号为2
name=Jone Doe
所以elstaticsearch执行put时是这样的:
如果指定id不存在就新增一条数据,存在时就覆盖
实际上创建document时ID是可选参数。如果未指定,Elasticsearch将生成一个随机ID。
不指定id创建时document需要使用POST命令
返回信息:
这里的id=AVpQdsOSxaunvQAUZY5w就是Elasticsearch随机生成的
注:elstaticsearch其实就是数据库,它与我们常使用的关系型数据库对应如下
Relational DB -> Databases -> Tables -> Rows -> Columns
Elasticsearch -> Indices -> Types -> Documents -> Fields
那么上面的四条命令大致相当于SQL语句如下:
更新documents
执行第一条POST
返回信息:result=updated 表示更新成功
注:实际上Elasticsearch不会在内部进行更新。每当我们进行更新时,Elasticsearch将删除旧document,然后再创建一个新的。
执行GET查看
返回信息:name=jack
再次执行第一条POST
返回信息:result=noop 表示NO OPeration,空操作。因为数据没变化
执行第二条POST
然后执行GET查看
返回信息:age=20
执行第三条POST
然后执行GET查看
返回信息:age=25
注:
删除documents
返回信息:result=deleteds 删除成功
再次删除时会返回 result=not_found
删除失败
批处理
注:批量API不会由于其中一个操作失败而失败。如果单个操作因任何原因失败,它将继续处理其后的其余操作。当批量API返回时,它将为每个操作(以相同的发送顺序)提供状态,以便检查特定操作是否失败。
所有命令总结如下:
创建和替换documents
#创建和替换documents PUT /customer/external/1?pretty { "name": "John Doe" } PUT /customer/external/1?pretty { "name": "Jane Doe" } POST /customer/external?pretty { "name": "Jane Doe" } GET /customer/external/1?pretty
执行第一条PUT:
在customer的external中存入一条id=1的数据
返回信息:
result=created created=true 说明是创建数据
version=1 版本号为1
执行GET命令:
获取在customer的external中id=1的数据
返回信息:
version=1 版本号为1
name=John Doe
执行第二条PUT:
返回信息:
result=updated created=false 说明是修改数据
version=2 版本号为2
再次执行GET命令:
返回信息:
version=2 版本号为2
name=Jone Doe
所以elstaticsearch执行put时是这样的:
如果指定id不存在就新增一条数据,存在时就覆盖
实际上创建document时ID是可选参数。如果未指定,Elasticsearch将生成一个随机ID。
不指定id创建时document需要使用POST命令
返回信息:
这里的id=AVpQdsOSxaunvQAUZY5w就是Elasticsearch随机生成的
注:elstaticsearch其实就是数据库,它与我们常使用的关系型数据库对应如下
Relational DB -> Databases -> Tables -> Rows -> Columns
Elasticsearch -> Indices -> Types -> Documents -> Fields
那么上面的四条命令大致相当于SQL语句如下:
insert into customer.external(id,name) values(1,'John Doe'); --指定id update customer.external set name = 'Jone Doe' where id = 1; insert into customer.external(name) values('John Doe'); --自动生成id select * from customer.external where id =1;
更新documents
#更新documents POST /customer/external/1/_update?pretty { "doc": { "name": "jack" } } POST /customer/external/1/_update?pretty { "doc": { "name": "Jane Doe", "age": 20 } } POST /customer/external/1/_update?pretty { "script" : "ctx._source.age += 5" } GET /customer/external/1?pretty
执行第一条POST
返回信息:result=updated 表示更新成功
注:实际上Elasticsearch不会在内部进行更新。每当我们进行更新时,Elasticsearch将删除旧document,然后再创建一个新的。
执行GET查看
返回信息:name=jack
再次执行第一条POST
返回信息:result=noop 表示NO OPeration,空操作。因为数据没变化
执行第二条POST
然后执行GET查看
返回信息:age=20
执行第三条POST
然后执行GET查看
返回信息:age=25
注:
ctx._source指的是当前document
删除documents
#删除documents DELETE /customer/external/1?pretty
返回信息:result=deleteds 删除成功
再次删除时会返回 result=not_found
删除失败
批处理
#批处理 POST /customer/external/_bulk?pretty {"index":{"_id":"1"}} {"name": "John Doe" } {"index":{"_id":"2"}} {"name": "Jane Doe" } POST /customer/external/_bulk?pretty {"update":{"_id":"1"}} {"doc": { "name": "John Doe becomes Jane Doe" } } {"delete":{"_id":"2"}}
注:批量API不会由于其中一个操作失败而失败。如果单个操作因任何原因失败,它将继续处理其后的其余操作。当批量API返回时,它将为每个操作(以相同的发送顺序)提供状态,以便检查特定操作是否失败。
所有命令总结如下:
#创建和替换documents PUT /customer/external/1?pretty { "name": "John Doe" } PUT /customer/external/1?pretty { "name": "Jane Doe" } POST /customer/external?pretty { "name": "Jane Doe" } GET /customer/external/1?pretty
#更新documents POST /customer/external/1/_update?pretty { "doc": { "name": "jack" } } POST /customer/external/1/_update?pretty { "doc": { "name": "Jane Doe", "age": 20 } } POST /customer/external/1/_update?pretty { "script" : "ctx._source.age += 5" } GET /customer/external/1?pretty
#删除documents DELETE /customer/external/1?pretty
#批处理 POST /customer/external/_bulk?pretty {"index":{"_id":"1"}} {"name": "John Doe" } {"index":{"_id":"2"}} {"name": "Jane Doe" } POST /customer/external/_bulk?pretty {"update":{"_id":"1"}} {"doc": { "name": "John Doe becomes Jane Doe" } } {"delete":{"_id":"2"}}
相关文章推荐
- Elasticsearch5.20 基本语法之入门
- Elasticsearch5.20 基本语法之查询
- php学习笔记(3):PHP基本语法和数据类型:
- JAVA数据类型及基本语法
- C++ 基本语法之变量(指针)与数据类型(占用字节大小)
- JavaScript语法入门系列(三) 基本数据类型及其对应对象类型
- java语法基本数据类型小结
- Java 基本语法——数据类型
- ruby: 基本数据类型与语法
- 动态sql语句基本语法(字段名,表名,数据库名之类作为变量时,必须用动态SQL如ALTER TABLE中使用程序传递的参数)
- 基本语法--基本的数据类型
- js复习之-基本语法及数据类型
- C# 语法练习(1): 基本数据类型
- 通过视图修改数据时所应掌握的基本准则
- oracle基本操作语法(建表空间,建用户,授权,数据导入导出)
- c++primer第二章“c++定义了几种基本类型,还提供了可用于自定义数据类型的机制,此外,我们还能修改已有的类型以形成复合类型”
- LotusScript基本语法知识……变量和数据类型
- PHP实现对文本数据库的数据显示、加入、修改、删除、查询五大基本操作的方法
- Objective-C语法之基本数据类型
- PHP的基本语法和数据类型