1、document的全量替换
(1)语法与创建文档是一样的,如果document id不存在,那么就是创建;如果document id已经存在,那么就是全量替换操作,替换document的json串内容
(2)document是不可变的,如果要修改document的内容,第一种方式就是全量替换,直接对document重新建立索引,替换里面所有的内容 第二种方式是更新操作
(3)es会将老的document标记为deleted,然后新增我们给定的一个document,当我们创建越来越多的document的时候,es会在适当的时机在后台自动删除标记为deleted的document,但是并不是立刻做物理删除
(4) 具体操作
向索引库中加入一个不存在的doucment,则创建
PUT /ecommerce/product/4
{
"name" : "test",
"desc" : "test document",
"price" : 40,
"producer" : "test producer",
"tags": [ "test" ]
}
返回结果,注意此时的版本_version是1
{
"_index": "ecommerce",
"_type": "product",
"_id": "4",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"created": true
}
继续进行操作,修改name 为test2
PUT /ecommerce/product/4
{
"name" : "test2",
"desc" : "test document",
"price" : 40,
"producer" : "test producer",
"tags": [ "test" ]
}
返回结果:此时版本_version是2
{
"_index": "ecommerce",
"_type": "product",
"_id": "4",
"_version": 2,
"result": "updated",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"created": false
}
此后 es会对版本1的文档标记为delete,等到合适的时机在后台进行删除.
2、document的强制创建
(1)强制创建的意思其实是如果文档已经存在则报错,而不会像创建文档那样document的全量替换.
(2)操作原型: PUT /index/type/id?op_type=create
,PUT /index/type/id/_create
(3) 具体操作
PUT /ecommerce/product/4/_create
{
"name" : "test2",
"desc" : "test document",
"price" : 40,
"producer" : "test producer",
"tags": [ "test" ]
}
返回结果
{
"error": {
"root_cause": [
{
"type": "version_conflict_engine_exception",
"reason": "[product][4]: version conflict, document already exists (current version [2])",
"index_uuid": "wCSA0NSsTrKIeHIh2Qoryw",
"shard": "2",
"index": "ecommerce"
}
],
"type": "version_conflict_engine_exception",
"reason": "[product][4]: version conflict, document already exists (current version [2])",
"index_uuid": "wCSA0NSsTrKIeHIh2Qoryw",
"shard": "2",
"index": "ecommerce"
},
"status": 409
}
3、document的删除
执行操作 DELETE /index/type/id ,
不会立即物理删除,只会将其标记为deleted,当数据越来越多的时候,适合的时机在后台自动删除