创建
-
通过
PUT my_index/_create/1
方式创建文档 -
支持自动生成文档Id和指定文档Id两种方式
-
通过调用
POST /users/_doc
-
系统自动生成document id
-
-
使用
PUT user/_create/1
创建时, URI中显式指定_create, 此时如果该id已经存在,则操作失败
-
-
通过指定doc id方式创建
PUT users/_doc/1?op_type=create
{
"user": "Test",
"post_date": "2021-04-18 21:13:21",
"message" : "set doc id by user "
}
{
"_index" : "users",
"_type" : "_doc",
"_id" : "1",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 1,
"_primary_term" : 1
}
{
"error" : {
"root_cause" : [
{
"type" : "version_conflict_engine_exception",
"reason" : "[1]: version conflict, document already exists (current version [1])",
"index_uuid" : "zKAAPOM8RTOq1JLJieQ9aw",
"shard" : "0",
"index" : "users"
}
],
"type" : "version_conflict_engine_exception",
"reason" : "[1]: version conflict, document already exists (current version [1])",
"index_uuid" : "zKAAPOM8RTOq1JLJieQ9aw",
"shard" : "0",
"index" : "users"
},
"status" : 409
}
-
通过系统自动生成doc id
POST users/_doc
{
"user": "Test",
"post_date": "2021-04-18 21:13:21",
"message" : "Generate doc id by ES "
}
{
"_index" : "users",
"_type" : "_doc",
"_id" : "iPwk5XgB2vvUbggENU8T",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 2,
"_primary_term" : 1
}
获取文档
GET users/_doc/1
获取文档信息
-
如果找不到文档,返回404
-
当返回200, 表示找到文档
-
文档原信息
-
_index、/ _type
-
版本信息,如果中存在相同被删除的文档,也能够获取。对应文档版本号信息不断增加
-
_source 中默认包含了文档的所有原始信息
-
-
-
Get文档
GET users/_doc/1
{
"_index" : "users",
"_type" : "_doc",
"_id" : "1",
"_version" : 2,
"_seq_no" : 3,
"_primary_term" : 1,
"found" : true,
"_source" : {
"user" : "Test"
}
}
通过对以上的操作,发现_source下的属性字段,已经减少
Index 文档
-
Index文档时,如果文档不存在,就索引新的文档。否则,现在有文档会被删除,新的文档被索引
-
通过
PUT users/_doc/1
的方式
-
Index文档
PUT users/_doc/1
{
"user": "Test"
}
{
"_index" : "users",
"_type" : "_doc",
"_id" : "1",
"_version" : 2,
"result" : "updated",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 3,
"_primary_term" : 1
}
通过index操作之后,可以发现_version版本已经递增,说明对文档执行了删除操作。
Update文档
-
Update方法不会删除原来的文档,而是实现真正的数据更新
-
Post 方式 / Payload 需要包在
doc
中 -
通过
POST users/_update/1
方式执行更新
-
更新文档
POST users/_update/1
{
"doc": {
"post_date": "2021-04-18 21:13:21",
"message" : "update doc"
}
}
{
"_index" : "users",
"_type" : "_doc",
"_id" : "1",
"_version" : 3,
"result" : "noop",
"_shards" : {
"total" : 0,
"successful" : 0,
"failed" : 0
},
"_seq_no" : 4,
"_primary_term" : 1
}
通过更新的方式,能够保证文档中已经存在的索引能够保留下来,并以增量的方式保存,这是我们通过
GET users/_doc/1
的方式,获取文档详情,就能够看到_source下的属性已经增量增加了
批量操作(Bulk API)
-
支持在一次API调用中,对不同的索引进行操作
-
支持四种类型操作
-
Index
-
Create
-
Update
-
Delete
-
-
可以在URI中指定Index, 也可以在请求的Payload中进行
-
操作中如果单项操作失败,不会影响其他操作
-
返回结果包含了每条操作的执行结果
-
批量操作
POST _bulk
{"index" : {"_index" : "test", "_id" : 1}}
{"field1": "value1"}
{"delete": {"_index": "test", "_id": "2"}}
{"create": {"_index": "test3", "_id": "3"}}
{"field1": "value3"}
{"update": {"_index": "test", "_id": "1"}}
{"doc": {"field2": "value2"}}
{
"took" : 574,
"errors" : false,
"items" : [
{
"index" : {
"_index" : "test",
"_type" : "_doc",
"_id" : "1",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 0,
"_primary_term" : 1,
"status" : 201
}
},
{
"delete" : {
"_index" : "test",
"_type" : "_doc",
"_id" : "2",
"_version" : 1,
"result" : "not_found",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 1,
"_primary_term" : 1,
"status" : 404
}
},
{
"create" : {
"_index" : "test3",
"_type" : "_doc",
"_id" : "3",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 0,
"_primary_term" : 1,
"status" : 201
}
},
{
"update" : {
"_index" : "test",
"_type" : "_doc",
"_id" : "1",
"_version" : 2,
"result" : "updated",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 2,
"_primary_term" : 1,
"status" : 200
}
}
]
}
批量读取 (mget)
通过该命令,能够批量获取返回结果
GET /_mget
{
"docs": [
{
"_index": "test",
"_id" : 1
},{
"_index": "test",
"_id" : 2
}
]
}
{
"docs" : [
{
"_index" : "test",
"_type" : "_doc",
"_id" : "1",
"_version" : 2,
"_seq_no" : 2,
"_primary_term" : 1,
"found" : true,
"_source" : {
"field1" : "value1",
"field2" : "value2"
}
},
{
"_index" : "test",
"_type" : "_doc",
"_id" : "2",
"found" : false
}
]
}
批量查询 (msearch)
批量查询可以执行多个查询指令,返回多个结果
POST kibana_sample_data_ecommerce/_msearch
{}
{"query": {"match_all": {}}, "size": 1}
{"index": "kibana_sample_data_flights"}
{"query": {"match_all": {}}, "size": 2}
在请求API的时候,不建议发送过多的数据,否则会导致集群压力上升等问题