1、document的全量替換
(1)語法與創(chuàng)建文檔是一樣的,如果document id不存在,那么就是創(chuàng)建;如果document id已經(jīng)存在,那么就是全量替換操作,替換document的json串內(nèi)容
(2)document是不可變的,如果要修改document的內(nèi)容,第一種方式就是全量替換,直接對(duì)document重新建立索引,替換里面所有的內(nèi)容
(3)es會(huì)將老的document標(biāo)記為deleted,然后新增我們給定的一個(gè)document,當(dāng)我們創(chuàng)建越來越多的document的時(shí)候,es會(huì)在適當(dāng)?shù)臅r(shí)機(jī)在后臺(tái)自動(dòng)刪除標(biāo)記為deleted的document
#添加
PUT /test_insex/test_type/4
{
"test_field":"test test"
}
#全量替換
PUT /test_insex/test_type/4
{
"test_field2":"test test test test2"
}
#查詢
GET /test_insex/test_type/4
#返回
{
"_index": "test_insex",
"_type": "test_type",
"_id": "4",
"_version": 2,
"found": true,
"_source": {
"test_field2": "test test test test2"
}
}

全量替換過程說明,也可以當(dāng)做document delete的原理
2、document的強(qiáng)制創(chuàng)建
創(chuàng)建文檔與全量替換的語法是一樣的,有時(shí)我們只是想新建文檔,不想替換文檔,如果強(qiáng)制進(jìn)行創(chuàng)建呢?
#語法
PUT /index/type/id?op_type=create 或者 PUT
/index/type/id/_create
#一般采用后者的方式居多
#操作
PUT /test_insex/test_type/4/_create
{
"test_field":"test"
}
#如果是已存在的情況下去創(chuàng)建則會(huì)報(bào)錯(cuò)
3、document的刪除
不會(huì)直接物理刪除,只會(huì)將其標(biāo)記為deleted,但是你已經(jīng)查詢不到的。當(dāng)數(shù)據(jù)越來越多的時(shí)候,在后臺(tái)自動(dòng)刪除。
#語法
DELETE /index/type/id
#操作
DELETE /test_insex/test_type/4
#返回
{
"found": true,
"_index": "test_insex",
"_type": "test_type",
"_id": "4",
"_version": 4,
"result": "deleted",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
}
}