查询全部文档
GET users/_search
不存在结果:
{
"error" : {
"root_cause" : [
{
"type" : "index_not_found_exception",
"reason" : "no such index [users]",
"resource.type" : "index_or_alias",
"resource.id" : "users",
"index_uuid" : "_na_",
"index" : "users"
}
],
"type" : "index_not_found_exception",
"reason" : "no such index [users]",
"resource.type" : "index_or_alias",
"resource.id" : "users",
"index_uuid" : "_na_",
"index" : "users"
},
"status" : 404
}
创建文档--create
如果存在,则报错
PUT users/_create/1
{
"user" : "nike",
"firstname": "jack",
"tags":["guitar", "skateboard"]
}
结果:
{
"_index" : "users",
"_type" : "_doc",
"_id" : "1",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 0,
"_primary_term" : 1
}
存在,报错:
{
"error" : {
"root_cause" : [
{
"type" : "version_conflict_engine_exception",
"reason" : "[1]: version conflict, document already exists (current version [1])",
"index_uuid" : "B_oYNKB4Rpq4_eepVEq4QA",
"shard" : "0",
"index" : "users"
}
],
"type" : "version_conflict_engine_exception",
"reason" : "[1]: version conflict, document already exists (current version [1])",
"index_uuid" : "B_oYNKB4Rpq4_eepVEq4QA",
"shard" : "0",
"index" : "users"
},
"status" : 409
}
索引文档--index
存在则覆盖掉原先文档内容,并文档版本+1
PUT users/_doc/1
{
"user" : "nike"
}
结果:
{
"_index" : "users",
"_type" : "_doc",
"_id" : "1",
"_version" : 2,
"result" : "updated",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 1,
"_primary_term" : 1
}
查询文档--GET
GET users/_doc/1
结果:
{
"_index" : "users",
"_type" : "_doc",
"_id" : "1",
"_version" : 2,
"_seq_no" : 1,
"_primary_term" : 1,
"found" : true,
"_source" : {
"user" : "nike"
}
}
更新文档--update
更新文档数据,不会覆盖文档数据,版本号+1
更新文档数据,需求将更新数据放到doc里面
POST users/_update/1
{
"doc": {
"user" : "Mike",
"post_date": "2019-04-15 00:00:00",
"message": "trying out kibana"
}
}
结果:
{
"_index" : "users",
"_type" : "_doc",
"_id" : "1",
"_version" : 3,
"result" : "updated",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 2,
"_primary_term" : 1
}
删除--DELETE
DELETE users
结果:
{
"acknowledged" : true
}
批量操作--Bulk API
- 支持在一次AP操作中,对不同索引操作
- 支持4中类型操作
- index
- create
- update
- delete
3.可以url中指定index, 也可以在playload中进行
4.操作单条失败,并不影响其他操作
5.返回结果包括每一条操作执行对结果
POST _bulk
{"index": {"_index" : "test", "_id":"1"}}
{"field1": "value1"}
{"delete": {"_index" : "test", "_id":"2"}}
{"create": {"_index" : "test2", "_id":"3"}}
{"field1": "value2"}
{"update": {"_id" : "1", "_index":"test"}}
{"doc" : {"field2":"value2"}}
结果:
{
"took" : 394,
"errors" : false,
"items" : [
{
"index" : {
"_index" : "test",
"_type" : "_doc",
"_id" : "1",
"_version" : 3,
"result" : "updated",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 2,
"_primary_term" : 1,
"status" : 200
}
},
{
"delete" : {
"_index" : "test",
"_type" : "_doc",
"_id" : "2",
"_version" : 1,
"result" : "not_found",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 3,
"_primary_term" : 1,
"status" : 404
}
},
{
"create" : {
"_index" : "test2",
"_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" : 4,
"result" : "updated",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 4,
"_primary_term" : 1,
"status" : 200
}
}
]
}
批量查询--MGET
需要将多个查询文档index及_id 以单个子项 放到 以 docs 键名的数组中
GET _mget
{
"docs" : [
{
"_index" : "users",
"_id": 1
},
{
"_index" : "test",
"_id": 1
}
]
}
结果:
{
"docs" : [
{
"_index" : "users",
"_type" : "_doc",
"_id" : "1",
"_version" : 3,
"_seq_no" : 2,
"_primary_term" : 1,
"found" : true,
"_source" : {
"user" : "Mike",
"post_date" : "2019-04-15 00:00:00",
"message" : "trying out kibana"
}
},
{
"_index" : "test",
"_type" : "_doc",
"_id" : "1",
"_version" : 4,
"_seq_no" : 4,
"_primary_term" : 1,
"found" : true,
"_source" : {
"field1" : "value1",
"field2" : "value2"
}
}
]
}