基于 7.12.x
POST /book/_doc/1
{
"name":"从入门到精通linux",
"price": 100,
"category":"tech"
}
# response
{
"_index": "book",
"_type": "_doc",
"_id": "1",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 0,
"_primary_term": 1
}
注意,如果id已经存在,es采用覆盖的形式(有点先删除,后插入的意思, 不过 _version 字段是一直递增)
GET /book/_doc/1
# respnose
{
"_index": "book",
"_type": "_doc",
"_id": "1",
"_version": 6,
"_seq_no": 5,
"_primary_term": 1,
"found": true,
"_source": {
"name": "从入门到精通linux",
"price": 100,
"category": "tech"
}
}
两种方式
PUT /book/_doc/1
{
"name":"从入门到精通linux5",
"price": 100,
"category":"tech",
"hello":1
}
# response
{
"_index": "book",
"_type": "_doc",
"_id": "1",
"_version": 9,
"_seq_no": 8,
"_primary_term": 1,
"found": true,
"_source": {
"name": "从入门到精通linux5",
"price": 100,
"category": "tech",
"hello": 1
}
}
# 第二次 PUT
PUT /book/_doc/1
{
"name":"从入门到精通linux5",
"price": 100,
"category":"tech"
}
# response
{
"_index": "book",
"_type": "_doc",
"_id": "1",
"_version": 10,
"_seq_no": 9,
"_primary_term": 1,
"found": true,
"_source": {
"name": "从入门到精通linux5",
"price": 100,
"category": "tech"
}
}
明显发现,字段 hello 在第二次 PUT 更新的时候被删除了
POST /book/_update/1
{
"doc": {
"name": "从入门到精通linux37",
"price": 100,
"category": "tech"
}
}
# response
{
"_index": "book",
"_type": "_doc",
"_id": "1",
"_version": 13,
"_seq_no": 12,
"_primary_term": 1,
"found": true,
"_source": {
"name": "从入门到精通linux37",
"price": 100,
"category": "tech"
}
}
# 第二次 POST update
POST /book/_update/1
{
"doc": {
"name": "从入门到精通linux37",
"price": 100,
"category": "tech",
"hello":1
}
}
# response
{
"_index": "book",
"_type": "_doc",
"_id": "1",
"_version": 14,
"_seq_no": 13,
"_primary_term": 1,
"found": true,
"_source": {
"name": "从入门到精通linux37",
"price": 100,
"category": "tech",
"hello": 1
}
}
# 第三次 POST update
POST /book/_update/1
{
"doc": {
"name": "从入门到精通linux38",
"price": 100,
"category": "tech"
}
}
# response
{
"_index": "book",
"_type": "_doc",
"_id": "1",
"_version": 15,
"_seq_no": 14,
"_primary_term": 1,
"found": true,
"_source": {
"name": "从入门到精通linux38",
"price": 100,
"category": "tech",
"hello": 1
}
}
可以发现, hello 字段新增后,后面更新条件中虽然删除了, 但是结果 hello 字段一直存在
DELETE /book/_doc/1
{
"_index": "book",
"_type": "_doc",
"_id": "1",
"_version": 16,
"result": "deleted",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 15,
"_primary_term": 1
}
POST /book/_bul //注意格式紧凑
{ "index":{"_id":1} }
{"name":"从入门到精通java","price": 100,"category":"tech"}
{ "index":{"_id":2} }
{"name":"从入门到精通es","price": 100,"category":"tech"}
{ "index":{"_id":3} }
{"name":"隋唐英雄","price": 100,"category":"history"}
// response
{
"took": 41,
"errors": false,
"items": [
{
"index": {
"_index": "book",
"_type": "_doc",
"_id": "1",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 0,
"_primary_term": 1,
"status": 201
}
},
{
"index": {
"_index": "book",
"_type": "_doc",
"_id": "2",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 1,
"_primary_term": 1,
"status": 201
}
},
{
"index": {
"_index": "book",
"_type": "_doc",
"_id": "3",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 2,
"_primary_term": 1,
"status": 201
}
}
]
}