增
POST {{es-url}}/indexName/_doc(/id)(如果不指定id,会自动生成 id,并插入一条数据。指定 id 就会修改这个数据,并新增版本号;入参可以包括模板中不存在的key,ES会自动根据value确定其field的类型)
入参
{
"id":1,
"name":"xiazihao with id",
"interesting":"novel movie ",
"birthday_time":"2021-01-01 00:00:00",
"birthday":"2021-01-01"
}
出参
{
"_index": "test-xizihao", // 索引名称
"_type": "_doc", // type 类型
"_id": "1", // id 传类id 就会使用传的id,没传会自动生成
"_version": 1, // 版本 更新的时候会加1
"result": "created", // 结果 create是新增 ,如果是updated就是更新
"_shards": { // 分片
"total": 2, //搜索了多少分片
"successful": 1, // 成功数量
"failed": 0 // 失败数量
},
"_seq_no": 0, // 并发控制字段 每次更新都会加1 用于做乐观锁
"_primary_term": 1 // 主分片 重新分片 如重启会变化
}
删
DELETE {{es-url}}/indexName/_doc/id
出参
{
"_index": "test-xizihao",
"_type": "_doc",
"_id": "1",
"_version": 2,
"result": "deleted",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 1,
"_primary_term": 1
}
改
更新
-
POST {{es-url}}/indexName/_doc/id入参
{ "id":1, "name":"xiazihao with id update", "interesting":"novel movie ", "birthday_time":"2021-01-01 00:00:00", "birthday":"2021-01-01" }出参
{ "_index": "test-xizihao", "_type": "_doc", "_id": "1", "_version": 2, "result": "updated", // 结果是 updated 更新 "_shards": { "total": 2, "successful": 1, "failed": 0 }, "_seq_no": 1, "_primary_term": 1 }
乐观锁更新
POST {{es-url}}/indexName/_doc/id?if_seq_no=num&if_primary_term=num
入参 (如果seq_no 或者 _primary_term 不对则报错)
{
"id":1,
"name":"xiazihao with id update optimism",
"interesting":"novel movie ",
"birthday_time":"2021-01-01 00:00:00",
"birthday":"2021-01-01"
}
出参 (报错的情形)
{
"error": {
"root_cause": [
{
"type": "version_conflict_engine_exception",
"reason": "[1]: version conflict, required seqNo [0], primary term [1]. current document has seqNo [1] and primary term [1]", // 需要seqNo 0,primary term 1.实际入参seqNo 1,primary term 1
"index_uuid": "q07WuUE-SMikbT1-73lIng",
"shard": "2",
"index": "test-xizihao"
}
],
"type": "version_conflict_engine_exception",
"reason": "[1]: version conflict, required seqNo [0], primary term [1]. current document has seqNo [1] and primary term [1]",
"index_uuid": "q07WuUE-SMikbT1-73lIng",
"shard": "2",
"index": "test-xizihao"
},
"status": 409
}
出参 (正确的情形)
{
"_index": "test-xizihao",
"_type": "_doc",
"_id": "1",
"_version": 3,
"result": "updated",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 2, // 加1了
"_primary_term": 1
}
查
通过id查
GET {{es-url}}/indexName/_doc/id
出参
{
"_index": "test-xizihao",
"_type": "_doc",
"_id": "1",
"_version": 2,
"_seq_no": 1,
"_primary_term": 1,
"found": true, // 搜索结果 true 成功
"_source": { // 数据
"id": 1,
"name": "xiazihao with id update",
"interesting": "novel movie ",
"birthday_time": "2021-01-01 00:00:00",
"birthday": "2021-01-01"
}
}
- GET {{es-url}}/indexName/_search
出参
{
"took": 6, // Elasticsearch执行搜索的时间(毫秒)
"timed_out": false, // 搜索是否超时
"_shards": { // 多少个分片被搜索了,以及统计了成功/失败的搜索分片
"total": 3,
"successful": 3,
"skipped": 0,
"failed": 0
},
"hits": { // 搜索结果
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 1,
"hits": [ // 实际的搜索结果数组(默认为前10的文档)
{
"_index": "test-xizihao",
"_type": "_doc",
"_id": "1",
"_score": 1, // 相关性得分
"_source": {
"id": 1,
"name": "xiazihao with id",
"interesting": "novel movie ",
"birthday_time": "2021-01-01 00:00:00",
"birthday": "2021-01-01",
"age": 2
}
}
]
}
}