elasticsearch 增删改查
增加
增加一条数据
PUT my_index/_doc/1
{
"title":"ES基本操作",
"content":"增删改查"
}
PUT请求增加数据,指定索引/类型/ID,在ES中,一开始将索引类比为数据库,类型比作数据表(数据表可以有多个,索引同一个索引下,类型也可以有多个),在ES6.0后官方认为这种类比是有问题的,并将一个索引对应多个类型这种做法取消了,并在7.x后强制要求只能有一个类型,就是我们建立的 _doc
title,content为指定的字段
也可以单独创建一个索引,而不创建数据
PUT my_index_1
删除
删除索引
DELETE my_index_!
删除单条数据
DETELE my_index/_doc/1
删除单条数据需要指定ID
查询
GET my_index/_doc/1
{
"_index" : "my_index",
"_type" : "_doc",
"_id" : "1",
"_version" : 2,
"_seq_no" : 1,
"_primary_term" : 1,
"found" : true,
"_source" : {
"title" : "ES基本操作",
"content" : "增删改查"
}
}
查询的数据,返回内容,主要参数
_index:数据所在索引
_type:所在类型
_id: 数据ID
_version: 数据版本
found:是否找到
_source:查询到的数据
更新数据
PUT my_index/_doc/1
{
"title":"ES基本操作",
"content":"增删改查-基本操作"
}
GET my_index/_doc/1
{
"_index" : "my_index",
"_type" : "_doc",
"_id" : "1",
"_version" : 2,
"_seq_no" : 1,
"_primary_term" : 1,
"found" : true,
"_source" : {
"title" : "ES基本操作",
"content" : "增删改查-基本操作"
}
}
使用PUT对数据进行更新,是直接覆盖原有的数据,
PUT my_index/_doc/1
{
"content":"增删改查-基本操作"
}
{
"_index" : "my_index",
"_type" : "_doc",
"_id" : "1",
"_version" : 3,
"_seq_no" : 2,
"_primary_term" : 1,
"found" : true,
"_source" : {
"content" : "增删改查-基本操作"
}
}
如果只想修改一个字段,又不想将所有字段数据全部传过去
使用POST请求,先恢复数据
PUT my_index/_doc/1
{
"title":"ES基本操作",
"content":"增删改查"
}
POST my_index/_doc/1
{
"content":"增删改查-基本操作"
}
{
"_index" : "my_index",
"_type" : "_doc",
"_id" : "1",
"_version" : 7,
"_seq_no" : 6,
"_primary_term" : 1,
"found" : true,
"_source" : {
"content" : "增删改查-基本操作"
}
}
只简单的使用POST请求是不能做到只更新字段的,必须_update
POST my_index/_update/1
{
"doc":{
"content":"增删改查-基本操作"
}
}
{
"_index" : "my_index",
"_type" : "_doc",
"_id" : "1",
"_version" : 9,
"_seq_no" : 8,
"_primary_term" : 1,
"found" : true,
"_source" : {
"title" : "ES基本操作",
"content" : "增删改查-基本操作"
}
}
这里只是对单个数据进行了查询,下一篇将会讲ES真正强大之处,全文搜索
关注公众号,和更多人一起学习