文档的基本操作
添加文档与自动映射
发送创建文档的请求
POST http://192.168.95.130:9200/index_for_doc/_doc/1
如果不指定文档id,则ES会自动生成一个随机字符串作为id,这里的id值得是restful api后面的url
// 请求体
{
"id": 1001,
"name": "tttt-1",
"desc": "tttt is very good, 雪中悍刀行非常牛!",
"create_date": "2019-12-24"
}
// 响应体
{
"_index": "index_for_doc", // 位于哪个索引
"_type": "_doc", // 类型
"_id": "1", // 新添加数据的id
"_version": 1, // 版本号
"result": "created", // 结果
"_shards": {
"total": 1, // 该数据总共需要写入几个shard
"successful": 1, // 该数据总共写入了几个shard
"failed": 0 // 失败了几个
},
"_seq_no": 0,
"_primary_term": 1
}
这个index
(index_for_doc)和document
(id为1)的mapping是我实现没有定义的,但是当我添加数据之后,它根据数据自动生成了:
// 自动生成的mapping
{
"mappings":{
"_doc":{
"properties":{
"name":{
"type":"text", // 由于name和desc是普通的字符串
"fields":{
"keyword":{
"ignore_above":256,
"type":"keyword"
}
}
},
"id":{
"type":"long" // 由于id插入的数据是一个数值
},
"create_date":{
"type":"date" // 由于date插入的数据是一个日期字符串
},
"desc":{
"type":"text",
"fields":{
"keyword":{
"ignore_above":256,
"type":"keyword"
}
}
}
}
}
}
}
可以通过es-head的UI界面直观的查看
删除文档
发送删除文档的请求
DELETE http://192.168.95.130:9200/index_for_doc/_doc/1
这种删除并不是物理上的删除,而是逻辑上的删除,文档中的数据依然位于磁盘中,只有当数据量达到一定程度之后ES才会被动的去真正删除数据。
// 响应体
{
"_index": "index_for_doc",
"_type": "_doc",
"_id": "1",
"_version": 2, // 版本号发生了变化,不管是否删除成功,只要发送了操作请求,就会导致version自增
"result": "deleted",
"_shards": {
"total": 1,
"successful": 1,
"failed": 0
},
"_seq_no": 1,
"_primary_term": 1
}
修改文档
局部修改
发送修改文档的请求
POST http://192.168.95.130:9200/index_for_doc/_doc/2/_update
局部修改不需要写上文档中全部的属性
// 请求体
{
"doc": {
"name": "乌托邦"
}
}
// 响应体
{
"_index": "index_for_doc",
"_type": "_doc",
"_id": "2",
"_version": 2,
"result": "updated",
"_shards": {
"total": 1,
"successful": 1,
"failed": 0
},
"_seq_no": 1,
"_primary_term": 1
}
全量修改
发送修改文档的请求
PUT http://192.168.95.130:9200/index_for_doc/_doc/2
全量修改必须带上所有的字段,否则漏掉的字段在文档中就会被删除!
// 请求体
{
"id": 1111,
"name": "乌托邦",
"desc": "tttt is very good, 美丽新世界非常牛!",
"create_date": "2019-12-25"
}
查询文档
根据id查找
发送查询文档的请求
GET http://192.168.95.130:9200/index_for_doc/_doc/2
// 响应体
{
"_index": "index_for_doc",
"_type": "_doc",
"_id": "2",
"_version": 5,
"_seq_no": 4,
"_primary_term": 1,
"found": true,
"_source": {
"id": 1111,
"name": "乌托邦",
"desc": "tttt is very good, 美丽新世界非常牛!",
"create_date": "2019-12-25"
}
}
查找所有
发送查询文档的请求
GET http://192.168.95.130:9200/index_for_doc/_doc/_search
// 响应体
{
"took": 2, // 检索时间,单位是毫秒
"timed_out": false, // 是否超时
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": { // 查询结果信息
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 1.0, // 最高匹配分数
"hits": [
{
"_index": "index_for_doc",
"_type": "_doc",
"_id": "2",
"_score": 1.0, // 相关度得分
"_source": {
"id": 1111,
"name": "乌托邦",
"desc": "tttt is very good, 美丽新世界非常牛!",
"create_date": "2019-12-25"
}
}
]
}
}
选择需要查询的字段
发送查询文档的请求
GET http://192.168.95.130:9200/index_for_doc/_doc/2?_source=id,name,desc
or
GET http://192.168.95.130:9200/index_for_doc/_doc/_search?_source=id,name,desc
只展示id
,name
,desc
{
"_index": "index_for_doc",
"_type": "_doc",
"_id": "2",
"_version": 6,
"_seq_no": 5,
"_primary_term": 1,
"found": true,
"_source": {
"name": "乌托邦",
"id": 1111,
"desc": "tttt is very good, 美丽新世界非常牛!"
}
}
查询该文档是否存在
发送验证是否存在的请求
HEAD http://192.168.95.130:9200/index_for_doc/_doc/2
判断index_for_doc下是否有id为2的文档
// HEAD请求没有响应体
// 如果响应报文状态码是200,则说明存在;404说明不存在。
由于HEAD请求没有响应体,所以可以更少的占用网络带宽。
文档乐观锁控制
发送查询请求
GET http://192.168.95.130:9200/index_for_doc/_doc/2
{
"_index": "index_for_doc",
"_type": "_doc",
"_id": "2",
"_version": 6,
"_seq_no": 5, // 序号
"_primary_term": 1, // 位置
"found": true,
"_source": {
"id": 1111,
"name": "乌托邦",
"desc": "tttt is very good, 美丽新世界非常牛!",
"create_date": "2019-12-25"
}
}
基于乐观锁控制去修改文档
发送基于乐观锁控制的修改文档请求
POST http://192.168.95.130:9200/index_for_doc/_doc/2?if_seq_no=5&if_primary_term=1
如果if_seq_no != 5或者if_primary_term != 1,则无法完成修改
如果是ES7.x之前的版本,是通过?version=n
来做控制的,但是这样没有提供定位,所以性能会低一些。
// 请求体
{
"doc": {
"name": "飘"
}
}
// 响应体
{
"_index": "index_for_doc",
"_type": "_doc",
"_id": "2",
"_version": 7,
"result": "updated",
"_shards": {
"total": 1,
"successful": 1,
"failed": 0
},
"_seq_no": 6, // 此时修改成功之后_seq_no更新为6
"_primary_term": 1
}
如果版本号发生冲突,会返回版本冲突异常的提示,并且版本号不会自增:
{
"error": {
"root_cause": [
{
"type": "version_conflict_engine_exception",
"reason": "[2]: version conflict, required seqNo [5], primary term [1]. current document has seqNo [6] and primary term [1]",
"index_uuid": "umqptD3XS5iDMHGzVR0aNQ",
"shard": "3",
"index": "index_for_doc"
}
],
"type": "version_conflict_engine_exception",
"reason": "[2]: version conflict, required seqNo [5], primary term [1]. current document has seqNo [6] and primary term [1]",
"index_uuid": "umqptD3XS5iDMHGzVR0aNQ",
"shard": "3",
"index": "index_for_doc"
},
"status": 409
}