文档基本操作¶
创建 _doc、_create¶
创建的关键字为 _create
或 _doc
。
创建的时候,指定索引名,后面带上 _doc
, 然后在请求体中写入对应的字段和值即可。
ES 会自动生成对应的文档 ID,文档 ID 为 _id
字段。
Example
HTTP/1.1 201 Created
Location: /product/_doc/gbxXC4IBc5NJpEpg9EmE
content-type: application/json; charset=UTF-8
content-encoding: gzip
content-length: 163
{
"_index": "product",
"_type": "_doc",
"_id": "gbxXC4IBc5NJpEpg9EmE", // 自动生成的 ID
"_version": 1,
"result": "created", // 表示创建成功,如果已有记录,则是 updated 更新成功
"_shards": {
"total": 1,
"successful": 1,
"failed": 0
},
"_seq_no": 3,
"_primary_term": 1
}
也可以创建的时候指定 _id
:
Example
HTTP/1.1 201 Created
Location: /product/_doc/1
content-type: application/json; charset=UTF-8
content-encoding: gzip
content-length: 140
{
"_index": "product",
"_type": "_doc",
"_id": "1",
"_version": 1,
"result": "created",
"_shards": {
"total": 1,
"successful": 1,
"failed": 0
},
"_seq_no": 4,
"_primary_term": 1
}
更新 _doc、_update¶
更新有两种方式,一种是「全量更新」,也叫「覆盖更新」;一种是「增量更新」,只更新指定的字段。
更新有两个关键字:_update
和 _doc
。
全量更新使用 _doc
,增量更新使用 _update
。
下面这种方式是全量更新,是先删除原始文档,再将新的数据插入。
全量更新
我们先插入一条数据:
HTTP/1.1 201 Created
Location: /product/_doc/1
content-type: application/json; charset=UTF-8
content-encoding: gzip
content-length: 139
{
"_index": "product",
"_type": "_doc",
"_id": "1",
"_version": 1,
"result": "created",
"_shards": {
"total": 1,
"successful": 1,
"failed": 0
},
"_seq_no": 8,
"_primary_term": 1
}
我们查看一下:
...
{
...
"_source": {
"title": "IPhone 11",
"price": 7999.99,
"description": "balabala11",
"create_at": "2022-07-19"
}
}
然后进行修改:
然后再次查看:
可以看到其他字段都没了。这种就是覆盖的方式进行更新。
下面这种方式是增量更新:
增量更新
我们先插入一条数据:
HTTP/1.1 201 Created
Location: /product/_doc/2
content-type: application/json; charset=UTF-8
content-encoding: gzip
content-length: 142
{
"_index": "product",
"_type": "_doc",
"_id": "2",
"_version": 1,
"result": "created",
"_shards": {
"total": 1,
"successful": 1,
"failed": 0
},
"_seq_no": 18,
"_primary_term": 1
}
我们查看一下:
...
{
...
"_source": {
"title": "IPhone 11",
"price": 7999.99,
"description": "balabala11",
"create_at": "2022-07-19"
}
}
然后进行修改:
然后再次查看:
...
{
...
"_source": {
"title": "IPhone 20",
"price": 7999.99,
"description": "balabala11",
"create_at": "2022-07-19"
}
}
可以看到其他字段都在,这种就是增量的方式进行更新。
删除¶
Example
查看¶
基于 ID 查询
example1
HTTP/1.1 200 OK
content-type: application/json; charset=UTF-8
content-encoding: gzip
content-length: 192
{
"_index": "product",
"_type": "_doc",
"_id": "1",
"_version": 1,
"_seq_no": 4,
"_primary_term": 1,
"found": true,
"_source": {
"title": "IPhone 11",
"price": 7999.99,
"description": "balabala11",
"create_at": "2022-07-19"
}
}
example2
HTTP/1.1 200 OK
content-type: application/json; charset=UTF-8
content-encoding: gzip
content-length: 208
{
"_index": "product",
"_type": "_doc",
"_id": "gbxXC4IBc5NJpEpg9EmE",
"_version": 1,
"_seq_no": 3,
"_primary_term": 1,
"found": true,
"_source": {
"title": "IPhone 12",
"price": 8999.99,
"description": "balabala",
"create_at": "2022-07-17"
}
}
批量操作 _bulk¶
批量操作的关键字为 _bulk
。
批量有更新、创建、删除的操作。通过多个 JSON 来表示。要注意的是 JSON 内部不可以换行,JSON之间必须换行。
-
创建:
-
更新:
-
删除:
-
可以混在同一个请求中:
POST /<INDEX_NAME>/_bulk { "create": { "_index": <_index>, "_id": <_id> } } { "<FIELD>": "<VALUE>", ...} { "index": { "_index": <_index>, "_id": <_id> } } { "<FIELD>": "<VALUE>", ...} { "update": { "_id": <_id>, "_index": <_index> } } { "doc": { "<FIELD>": "<VALUE>", ... } } { "delete": { "_id": <_id>, "_index": <_index> } }
Example
HTTP/1.1 200 OK
content-type: application/json; charset=UTF-8
content-encoding: gzip
content-length: 189
{
"took": 11,
"errors": false,
"items": [
{
"index": {
"_index": "product",
"_type": "_doc",
"_id": "1",
"_version": 1,
"result": "created",
"_shards": {
"total": 1,
"successful": 1,
"failed": 0
},
"_seq_no": 1,
"_primary_term": 1,
"status": 201
}
},
{
"create": {
"_index": "product",
"_type": "_doc",
"_id": "2",
"_version": 1,
"result": "created",
"_shards": {
"total": 1,
"successful": 1,
"failed": 0
},
"_seq_no": 2,
"_primary_term": 1,
"status": 201
}
}
]
}
Warning
批量操作不会因为一个失败而全部失败,而是继续执行后续操作,最后返回所有处理结果。