前面ElasticSerach的索引(一、ElsaticSerach-索引操作)已经建好了,下面我们来新建文档,并添加数据
1、创建文档
- 创建
post /my_index05/_doc/ //不指定ID,生产随机ID
{
"first_name" : "Jane",
"last_name" : "Smith",
"age" : 32,
"about" : "I like to collect rock albums",
"interests": [ "music" ]
}
post /my_index05/_doc/1 //指定ID为1
{
"first_name" : "Jane1",
"last_name" : "Smith",
"age" : 32,
"about" : "I like to collect rock albums",
"interests": [ "music" ]
}
- 创建结果:
{
"_index" : "my_index05",
"_type" : "_doc",
"_id" : "srBWR4AB7YaxECYb3G1U", //可以指定id,不指定id的时候,生产随机的字符串
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 0,
"_primary_term" : 1
}
-------------key解释-------------
{
"_index"【索引】: "my_index05",
"_type"【类型-文档】: "_doc",
"_id"【唯一标识】: "srBWR4AB7YaxECYb3G1U", #
"_version"【版本】: 1,
"result"【结果】: "created", #这里的 create 表示创建成功,ID相同的时候是update
"_shards"【分片】: {
"total"【分片 - 总数】: 2,
"failed"【分片 - 失败】: 0
},
"_seq_no": 0,
"_primary_term": 1
}
注意:
- 上面的数据创建后,由于没有指定数据唯一性标识(ID),默认情况下,ES 服务器会随机生成一个,如果指定ID,则ID就是指定的ID
- 此处需要注意:如果增加数据时明确数据主键,那么请求方式也可以为 PUT,如果没有指定明确的数据主键,那么请求方式只能使用POST
- 如果指定ID的时候POST数据,ID已经存在,那么数据就会被更新 "result" : "updated",
2、查询数据
查询索引名称为my_index05,type为_doc,ID为1的数据
- 查询
get /my_index05/_doc/1
- 结果
//查询结果
{
"_index" : "my_index05",
"_type" : "_doc",
"_id" : "1",
"_version" : 5,
"_seq_no" : 5,
"_primary_term" : 1,
"found" : true,
"_source" : {
"first_name" : "tom",
"last_name" : "Smith",
"age" : 35,
"about" : "I like to collect rock albums",
"interests" : [
"music"
]
}
}
//结果中键值对介绍
{
"_index" 【索引】: "my_index05",
"_type"【文档类型】 : "_doc",
"_id" : "1",
"_version" : 5,
"_seq_no" : 5,
"_primary_term" : 1,
"found" : true,#【查询结果】: true, # true 表示查找到,false 表示未查找到
"_source" 【文档源信息】 : {
"first_name" : "tom",
"last_name" : "Smith",
"age" : 35,
"about" : "I like to collect rock albums",
"interests" : [
"music"
]
}
}
3、修改文档
- 查询ID为1的数据
{
"_index" : "my_index05",
"_type" : "_doc",
"_id" : "1",
"_version" : 5,
"_seq_no" : 5,
"_primary_term" : 1,
"found" : true,
"_source" : {
"first_name" : "tom",
"last_name" : "Smith",
"age" : 35,
"about" : "I like to collect rock albums",
"interests" : [
"music"
]
}
}
- 更新ID等于1的数据
post /my_index05/_doc/1 //指定ID为1
{
"first_name" : "Jane",
"last_name" : "Smith",
"age" : 32,
"about" : "I like to collect rock albums",
"interests": [ "music" ]
}
//es响应数据
{
"_index" : "my_index05",
"_type" : "_doc",
"_id" : "1",
"_version" : 6, //版本,更新一次版本会更新
"result" : "updated", //结果为更新
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 6,
"_primary_term" : 1
}
- 查看更新之后的数据
{
"_index" : "my_index05",
"_type" : "_doc",
"_id" : "1",
"_version" : 6, //版本,更新一次版本会更新
"_seq_no" : 6,
"_primary_term" : 1,
"found" : true,
"_source" : {
"first_name" : "Jane", //名称已经修改
"last_name" : "Smith",
"age" : 32,
"about" : "I like to collect rock albums",
"interests" : [
"music"
]
}
}
注意:上面更新操作使用的是新增的语句,当es中存在id为1的数据的时候,其实触发的是更新操作。
4、修改指定字段数据
上面说了修改文档的整条数据,ES还提供了修改文档中单个指定字段的数据。
post /my_index05/_update【更新】/1
{
"doc【文档名】":{
"first_name"【修改的字段名】:"Jane123"
}
}
#es响应数据
{
"_index" : "my_index05",
"_type" : "_doc",
"_id" : "1",
"_version" : 8, #版本使用该方法也回更新
"result" : "updated",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 8,
"_primary_term" : 1
}
- 查看修改后的数据
{
"_index" : "my_index05",
"_type" : "_doc",
"_id" : "1",
"_version" : 9,
"_seq_no" : 9,
"_primary_term" : 1,
"found" : true,
"_source" : {
"first_name" : "Jane123", //first_name名称修改
"last_name" : "Smith",
"age" : 32,
"about" : "I like to collect rock albums",
"interests" : [
"music"
]
}
}
5、删除文档数据
DELETE /my_index05/_doc/1
#响应数据
{
"_index" : "my_index05",
"_type" : "_doc",
"_id" : "1",
"_version" : 10, #版本
"result" : "deleted", # deleted 表示数据被标记为删除
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 10,
"_primary_term" : 1
}
- 再次查询
{
"_index" : "my_index05",
"_type" : "_doc",
"_id" : "1",
"found" : false
}
注意:这里删除是软删(逻辑删除),标记为删除