Create
自动生成文档id
通过 POST index_name/_doc 系统会自动生成document id
PUT index_name/_doc{"xxxx": "xxxx",.....}
指定文档id
通过 PUT index_name/_create/doc_id ,例如doc_id为1,则可以指定id创建。如果该id对应的文档已经存在,则操作失败
PUT custome_users/_create/1{"username": "xiaoming"}## 返回{"_index" : "custome_users","_type" : "_doc","_id" : "1","_version" : 1,"result" : "created","_shards" : {"total" : 2,"successful" : 1,"failed" : 0},"_seq_no" : 0,"_primary_term" : 1}## 再次执行PUT custome_users/_create/1{"username": "xiaoming"}## 结果{"error": {"root_cause": [{"type": "version_conflict_engine_exception","reason": "[1]: version conflict, document already exists (current version [1])","index_uuid": "yT6HwezMQuiW2F_Rv_sw6Q","shard": "0","index": "custome_users"}],"type": "version_conflict_engine_exception","reason": "[1]: version conflict, document already exists (current version [1])","index_uuid": "yT6HwezMQuiW2F_Rv_sw6Q","shard": "0","index": "custome_users"},"status": 409}
Get
找到文档,HTTP 200
结果
GET custome_users/_doc/1{"_index" : "custome_users","_type" : "_doc","_id" : "1","_version" : 1,"_seq_no" : 0,"_primary_term" : 1,"found" : true,"_source" : {"username" : "xiaoming"}}
找不到文档,HTTP 404
GET custome_users/_doc/2{"_index" : "custome_users","_type" : "_doc","_id" : "2","found" : false}
Index
Index 和 Create不同的地方:如果文档不存在,则索引新的文档,否则删除现有文档,新的文档被索引,版本+1
PUT custome_users/_doc/1{"fullname": "xiaoming_new"}# 可以看到"_version"的值为2{"_index" : "custome_users","_type" : "_doc","_id" : "1","_version" : 2,"result" : "updated","_shards" : {"total" : 2,"successful" : 1,"failed" : 0},"_seq_no" : 1,"_primary_term" : 1}# GET custome_users/_doc/1, 可以看到原来的username字段没有了,说明原文档被删除,增加了新的温昂{"_index" : "custome_users","_type" : "_doc","_id" : "1","_version" : 2,"_seq_no" : 3,"_primary_term" : 1,"found" : true,"_source" : {"fullname" : "xiaoming"}}
Update
- Update方法不会删除原来的文档,是真正的更新数据
- POST方法 / Payload需要包含在
doc中 - 可以用来新增字段
POST custome_users/_update/1
{
"doc": {
"username": "xiaoming_new_v3"
}
}
# "_version"字段又增加了1
{
"_index" : "custome_users",
"_type" : "_doc",
"_id" : "1",
"_version" : 3,
"result" : "updated",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 2,
"_primary_term" : 1
}
# GET custome_users/_doc/1 可以看到,fullname字段没有丢失,说明是纯更新,没有删掉原文档
{
"_index" : "custome_users",
"_type" : "_doc",
"_id" : "1",
"_version" : 3,
"_seq_no" : 4,
"_primary_term" : 1,
"found" : true,
"_source" : {
"fullname" : "xiaoming",
"username" : "xiaoming_new_v3"
}
}
Bulk API - 批量操作
mget - 批量读取
官方文档
批量操作,减少网络连接所产生的开销,提高性能
GET _mget
{
"docs": [
{
"_index": "custome_users",
"_id": 1
},
{
"_index": "custome_users",
"_id": 2
}
]
}
