elasticsearch如何进行增删改查
1.新增一条记录
输入
POST /book/_doc
{
"id":"1",
"name":"桃园结义"
}
输出
{
"_index" : "book",
"_type" : "_doc",
"_id" : "5o-fDnkB8M1CImqOYcIW",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 1,
"_primary_term" : 1
}
-
其中book是索引名, _doc是固定用法。使用这条命令就可以进行相应的插入操作
什么是索引? 初次接触es的人,可能看到这个词,会和关系型数据库中的索引相混淆。
在es中,索引是存放数据的基本结构。
就好比关系型数据库中表的概念一样。
但是二者不可完全等价
那么如何将它查出来?
2.查询索引
输入
GET book/_search?pretty=true
输出
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "book",
"_type" : "_doc",
"_id" : "c4-vDnkB8M1CImqOW8RA",
"_score" : 1.0,
"_source" : {
"id" : "1",
"name" : "桃园结义"
}
}
]
}
}
- 使用查询功能,会发现,es在插入数据的时候,即使body里面有id,也会外面赋值一个id,但是与里面的id不同,一般前面加下划线
3.如何修改索引中的记录
输入
PUT /book/_doc/c4-vDnkB8M1CImqOW8RA
{
"id":"1",
"name":"三国演义之桃园结义"
}
输出
{
"took" : 801,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "book",
"_type" : "_doc",
"_id" : "c4-vDnkB8M1CImqOW8RA",
"_score" : 1.0,
"_source" : {
"id" : "1",
"name" : "三国演义之桃园结义"
}
}
]
}
}
4.如何删除索引
输入
DELETE book
输出
{
"acknowledged" : true
}
之后我们将再聊一聊es的进阶用法
二.致谢
https://blog.csdn.net/weixin_44993178/article/details/92400425
https://www.cnblogs.com/Star-Haitian/articles/10082150.html