首先先来了解一下什么是文档,es里边的文档相当于mysql数据库里边的一张表,如果你用过mongodb的话,那么肯定对es这种数据格式一定很了解了。接下来我们来看一下如何创建一个文档。
PUT /sdb/user/1
{
"username":"张三",
"age":16,
"gender":"男",
"about":"this is my info",
"addrs":["甘肃","陕西","兰州"]
}
执行成功后结果如下:
{
"_index" : "sdb",
"_type" : "user",
"_id" : "1",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 4,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 0,
"_primary_term" : 1
}
这时我们已经给sdb索引添加了一个文档,我们来查看一下命令
GET /sdb/user/1
查看user文档ID为1
GET /sdb/user/q
{
"_index" : "sdb",
"_type" : "user",
"_id" : "1",
"_version" : 1,
"_seq_no" : 0,
"_primary_term" : 1,
"found" : true,
"_source" : {
"username" : "张三",
"age" : 16,
"gender" : "男",
"about" : "this is my info",
"addrs" : [
"甘肃",
"陕西",
"兰州"
]
}
}
执行成功,说明已经检索出来了,
创建我们会了,下面我们来说说如何修改一个已经存在的文档,命令如下:
//根据ID来修改,把之前的张三修改为张三名
POST /sdb/user/1
{
"username":"张三名",
"age":16,
"gender":"男",
"about":"this is my info",
"addrs":["甘肃","陕西","兰州"]
}
执行成功后输出入下
{
"_index" : "sdb",
"_type" : "user",
"_id" : "1",
"_version" : 4,
"result" : "updated",
"_shards" : {
"total" : 4,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 3,
"_primary_term" : 1
}
到这里创建和修改我们都会了,下面我们来看看如何删除一个文档呢,命令如下
DELETE /sdb/user/1
1代表删除文档ID为1的,以上代码执行结果如下:
{
"_index" : "sdb",
"_type" : "user",
"_id" : "1",
"_version" : 5,
"result" : "deleted",
"_shards" : {
"total" : 4,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 4,
"_primary_term" : 1
}
到此已经删除成功了,我们在用GET查看,发下结果如下:
{
"_index" : "sdb",
"_type" : "user",
"_id" : "1",
"found" : false
}
以上为es中文档的创建修改和删除,有问题可以在下面评论,想讨论技术的可以私聊我。