在上一篇文章中我们介绍了elasticsearch的基本概念以及索引和索引映射的restful接口和它们之间的关系https://blog.csdn.net/weixin_42214548/article/details/107094416,今天再来给大家介绍一下elasticsearch中的文档以及它的restful接口的使用
创建文档
POST http://127.0.0.1:9200/index_test/_doc/id
URL结尾可以指定文档的id,如果不指定文档的id则会自动生成一个id
请求参数
{
"id":1001,
"sex": 2,
"realname": "大帅比",
"username":"有退休金的老李"
}
响应
{
"_index": "index_test",
"_type": "_doc",
"_id": "zv0HEHMBnqHViiCdQnfE",
"_version": 2,
"result": "updated",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 1,
"_primary_term": 1
}
elasticsearch的结果
可以看到最后成功的响应了,并且大家可以注意返回的参数中有一个_type和_version,_type表示elasticsearch中索引的类型,但是我们创建索引的时候没有指定类型,在新版本中类型的概念已经被弱化默认就是文档,_version则是每次操作了索引之后的一个版本号,每次都会加1
删除文档
DELETE http://127.0.0.1:9200/index_test/_doc/zv0HEHMBnqHViiCdQnfE
{
"_index": "index_test",
"_type": "_doc",
"_id": "zv0HEHMBnqHViiCdQnfE",
"_version": 3,
"result": "deleted",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 2,
"_primary_term": 1
}
修改文档
修改文档可以分为局部修改和全量修改,下面为大家显示一下
全量修改
PUT http://127.0.0.1:9200/index_test/_doc/zv0HEHMBnqHViiCdQnfE
{
"id":1001,
"sex": 2,
"realname": "大帅比2222222",
"username":"有退休金的老李"
}
{
"_index": "index_test",
"_type": "_doc",
"_id": "zv0HEHMBnqHViiCdQnfE",
"_version": 2,
"result": "updated",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 4,
"_primary_term": 1
}
局部修改
POST http://127.0.0.1:9200/index_test/_doc/zv0HEHMBnqHViiCdQnfE/_update
{
"doc":{
"username":"有退休金的老李11111"
}
}
{
"_index": "index_test",
"_type": "_doc",
"_id": "zv0HEHMBnqHViiCdQnfE",
"_version": 3,
"result": "updated",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 5,
"_primary_term": 1
}