1.全量替换
如果document的id不存在,那么就是创建,如果以及存在,那么就是全量创建
PUT /test_index/test_type/1
{
"test_field":"xxx"
}
执行结果
{
"_index": "test_index",
"_type": "test_type",
"_id": "1",
"_version": 1,
"found": true,
"_source": {
"test_field": "xxx"
}
}
PUT /test_index/test_type/1
{
"test_field":"xxx",
"test_field2":"xxx2"
}
执行结果:
{
"_index": "test_index",
"_type": "test_type",
"_id": "2",
"_version": 1,
"found": true,
"_source": {
"test_field": "xxx",
"test_field2": "xxx2"
}
}
2.强制创建
如果你不想做替换操作,而是希望做创建操作,那么可以指定命令为创建(即强制创建操作)
语法:两种语法
1)PUT /index/type/id?op_type=create
2)PUT /index/type/id?_create
普通创建:
PUT /test_index/test_type/2
{
"test_field":"xxx1",
"test_field2":"xxx3"
}
强制创建:
PUT /test_index/test_type/2?op_type=create
{
"test_field":"xxx1",
"test_field2":"xxx3"
}
结果是id冲突:
{
"error": {
"root_cause": [
{
"type": "version_conflict_engine_exception",
"reason": "[test_type][2]: version conflict, document already exists (current version [2])",
"index_uuid": "ZnZjGP7GQsqotz19lqhxJg",
"shard": "2",
"index": "test_index"
}
],
"type": "version_conflict_engine_exception",
"reason": "[test_type][2]: version conflict, document already exists (current version [2])",
"index_uuid": "ZnZjGP7GQsqotz19lqhxJg",
"shard": "2",
"index": "test_index"
},
"status": 409
}
另一种语法:
PUT /test_index/test_type/2/_create
{
"test_field":"xxx0",
"test_field2":"xxx02"
}
执行结果:id冲突
{
"error": {
"root_cause": [
{
"type": "version_conflict_engine_exception",
"reason": "[test_type][2]: version conflict, document already exists (current version [2])",
"index_uuid": "ZnZjGP7GQsqotz19lqhxJg",
"shard": "2",
"index": "test_index"
}
],
"type": "version_conflict_engine_exception",
"reason": "[test_type][2]: version conflict, document already exists (current version [2])",
"index_uuid": "ZnZjGP7GQsqotz19lqhxJg",
"shard": "2",
"index": "test_index"
},
"status": 409
}
3.delete删除的原理
命令:DELETE /test_index/test_type/2
此时如果执行get操作就查找不到数据了。但是实际上,delete不会进行物理删除,只会将其标记为deleted,当数据越来越多的时候,在后台自动删除。