Elasticsearch 基础操作
文章目录
1.文档的基本 CRUD
操作 | 示例 |
---|---|
Index | PUT my_index/_doc/1{“user”:“mike”,“comment”:“hello,world”} |
Create | PUT my_index/_create/1{“user”:“mike”,“comment”:“hello,world”} |
Create | POST my_index/_doc{“user”:“mike”,“comment”:“hello,world”} |
Read | GET my_index/_doc/1 |
Update | POST my_index/_update/1{“user”:“mike”,“comment”:“hello,world”} |
Delete | DELETE my_index/_doc/1 |
- Type 名,约定都用 _doc
- Create,如果 ID 不存在,创建新的文档;如果 ID 已经存在,会失败
- Index,如果 ID 不存在,创建新的文档;否则删除现有文档,再创建新的文档,版本会增加
- Update,文档必须已经存在,更新只会对相应字段做增量修改
1.1 Create
支持自动生成文档 Id 和指定文档 Id 两种方式
- 通过调用 POST /users/_doc,系统会自动生成 document Id
POST users/_doc
{
"user" : "mike",
"post_date" : "2020-11-15T14:12:12",
"message" : "trying out Elasticsearch"
}
返回
{
"_index" : "users",
"_type" : "_doc",
"_id" : "I9go9XUBP8pITtn8SP5J",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 6,
"_primary_term" : 2
}
- 使用 HTTP PUT user/_create/1 创建时,URI 中显示指定 _create,此时如果该 Id 的文档以及存在,操作失败
PUT users/_create/1
{
"user" : "Jack",
"post_date" : "2019-05-15T14:12:12",
"message" : "trying out Elasticsearch"
}
返回结果
{
"_index" : "users",
"_type" : "_doc",
"_id" : "1",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 0,
"_primary_term" : 1
}
重复创建,报错 document already exists
{
"error" : {
"root_cause" : [
{
"type" : "version_conflict_engine_exception",
"reason" : "[1]: version conflict, document already exists (current version [1])",
"index_uuid" : "hABQ1rzMSOSQS1nKhQ2t4A",
"shard" : "0",
"index" : "users"
}
],
"type" : "version_conflict_engine_exception",
"reason" : "[1]: version conflict, document already exists (current version [1])",
"index_uuid" : "hABQ1rzMSOSQS1nKhQ2t4A",
"shard" : "0",
"index" : "users"
},
"status" : 409
}
1.2 Get
根据 id 获取一个文档
#Get the document by ID
GET users/_doc/1
返回结果
{
"_index" : "users",
"_type" : "_doc",
"_id" : "1",
"_version" : 1,
"_seq_no" : 0,
"_primary_term" : 1,
"found" : true,
"_source" : {
"user" : "Jack",
"post_date" : "2019-05-15T14:12:12",
"message" : "trying out Elasticsearch"
}
}
- 找到文档,返回 HTTP 200
- 文档元信息
- _index/_type
- 版本信息,同一个 Id 的文档,即使被删除,Version 号也会不断增加
- _source 中默认包含了文档的所有原始信息
- 文档元信息
- 找不到文档,返回 HTTP 404
1.3 Index
索引在 Elasticsearch 中的意思类似于创建
- Index 和 Create 不同的地方:如果文档不存在,就创建新的文档。否则删除现有文档,再创建新的文档,版本信息 +1。
PUT users/_doc/1
{
"user" : "Mike"
}
返回
{
"_index" : "users",
"_type" : "_doc",
"_id" : "1",
"_version" : 3,
"result" : "updated",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 4,
"_primary_term" : 1
}
GET users/_doc/1
查看之前的文档,内容已经被修改
{
"_index" : "users",
"_type" : "_doc",
"_id" : "1",
"_version" : 3,
"_seq_no" : 4,
"_primary_term" : 1,
"found" : true,
"_source" : {
"user" : "Mike"
}
}
1.4 Update
-
Update 方法不会删除原来的文档,而是更新数据
-
Post 方法/Payload 需要包含在 doc 中
POST users/_update/1
{
"doc":{
"post_date" : "2019-05-15T14:12:12",
"message" : "trying out Elasticsearch"
}
}
GET users/_doc/1
查看之前的文档,内容已经被修改,增加了两个字段
{
"_index" : "users",
"_type" : "_doc",
"_id" : "1",
"_version" : 4,
"_seq_no" : 5,
"_primary_term" : 1,
"found" : true,
"_source" : {
"user" : "Mike",
"post_date" : "2019-05-15T14:12:12",
"message" : "trying out Elasticsearch"
}
}
1.5 Delete
# delete document by Id
DELETE users/_doc/1
2.批量操作
2.1 批量操作 bulk
- 支持在一次 API 调用中,对不同的索引进行操作
- 支持四种类型操作
- Index
- Create
- Update
- Delete
- 可以在 URI 中指定 Index,也可以在请求的 Payload 中进行
- 单条操作失败,并不会影响其他操作
- 返回结果包括了每一条操作执行的结果
测试命令:
POST _bulk
{ "index" : { "_index" : "test", "_id" : "1" } }
{ "field1" : "value1" }
{ "delete" : { "_index" : "test", "_id" : "2" } }
{ "create" : { "_index" : "test2", "_id" : "3" } }
{ "field1" : "value3" }
{ "update" : {"_id" : "1", "_index" : "test"} }
{ "doc" : {"field2" : "value2"} }
执行结果:
{
"took" : 75,
"errors" : true,
"items" : [
{
"index" : {
"_index" : "test",
"_type" : "_doc",
"_id" : "1",
"_version" : 5,
"result" : "updated",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 6,
"_primary_term" : 3,
"status" : 200
}
},
{
"delete" : {
"_index" : "test",
"_type" : "_doc",
"_id" : "2",
"_version" : 1,
"result" : "not_found",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 7,
"_primary_term" : 3,
"status" : 404
}
},
{
"create" : {
"_index" : "test2",
"_type" : "_doc",
"_id" : "3",
"status" : 409,
"error" : {
"type" : "version_conflict_engine_exception",
"reason" : "[3]: version conflict, document already exists (current version [1])",
"index_uuid" : "UPztWsW_QuOiZzZcmeylng",
"shard" : "0",
"index" : "test2"
}
}
},
{
"update" : {
"_index" : "test",
"_type" : "_doc",
"_id" : "1",
"_version" : 6,
"result" : "updated",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 8,
"_primary_term" : 3,
"status" : 200
}
}
]
}
2.2 批量读取 mget
批量操作,可以减少网络连接产生的开销,提高性能
GET /_mget
{
"docs" : [
{
"_index" : "test",
"_id" : "1"
},
{
"_index" : "test",
"_id" : "2"
}
]
}
执行结果:
{
"docs" : [
{
"_index" : "test",
"_type" : "_doc",
"_id" : "1",
"_version" : 6,
"_seq_no" : 8,
"_primary_term" : 3,
"found" : true,
"_source" : {
"field1" : "value1",
"field2" : "value2"
}
},
{
"_index" : "test",
"_type" : "_doc",
"_id" : "2",
"found" : false
}
]
}
2.3 批量查询 msearch
批量操作,可以减少网络连接产生的开销,提高性能
POST kibana_sample_data_ecommerce/_msearch
{}
{"query" : {"match_all" : {}},"size":1}
{"index" : "kibana_sample_data_flights"}
{"query" : {"match_all" : {}},"size":2}
执行结果如下,同时返回两个索引的查询结果
3.常见错误返回
问题 | 原因 |
---|---|
无法连接 | 网络故障或者集群挂了 |
连接无法关闭 | 网络故障或者节点出错 |
429 | 集群过于繁忙 |
4xx | 请求体格式有错 |
500 | 集群内部错误 |
4.参考
- 《Elasticsearch核心技术与实战》- 阮一鸣