之前记录的都是最基本的入门语法,这里开始修改数据
创建和替换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不存在就新增一条数据,存在时就覆盖
实际上创建document时ID是可选参数。如果未指定,Elasticsearch将生成一个随机ID。
不指定id创建时document需要使用POST命令
返回信息:
这里的id=AVpQdsOSxaunvQAUZY5w就是Elasticsearch随机生成的
注:elstaticsearch其实就是数据库,它与我们常使用的关系型数据库对应如下
Relational DB -> Databases -> Tables -> Rows -> Columns
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"}}