前言
以下操作均在 kibana 下进行,提前需要安装 kinana。
CRUD
Index 操作
# 文档不存在,创建一个新的文档,索引名为 my_indexs ,type 默认为 doc, 文档 id = 1
put my_indexs/_doc/1
{
"name":"joyxj",
"age":2
}
# 执行结果
{
"_index" : "my_indexs",
"_type" : "_doc",
"_id" : "1",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 0,
"_primary_term" : 1
}
# 再执行一次,文档存在,删除现有的文档,再创建新的文档,版本加1
put my_indexs/_doc/1
{
"name":"joyxj.com",
"age":21
}
# 执行结果
{
"_index" : "my_indexs",
"_type" : "_doc",
"_id" : "1",
"_version" : 2,
"result" : "updated",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 1,
"_primary_term" : 1
}
# 查询文档的内容
get my_indexs/_doc/1
# 执行结果,从_source可以看出,只有一个值了。所以不是更新操作,而是新删除,后创建
{
"_index" : "my_indexs",
"_type" : "_doc",
"_id" : "1",
"_version" : 2,
"_seq_no" : 1,
"_primary_term" : 1,
"found" : true,
"_source" : {
"name" : "joyxj.com"
}
}
对于 Index 操作,如果 ID 不存在的话,则创建新的文档。否则会删除旧的文档,再创建新的文档,同时版本号加 1.
Create 操作
指定 ID 的 Create 操作,type 默认为 _doc。
# 指定ID,type 默认为 _doc,如果 ID 存在会报错
put my_indexs/_create/1
{
"name":"joyxj.com",
"age":21
}
# 执行结果
{
"_index" : "my_indexs",
"_type" : "_doc",
"_id" : "1",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 0,
"_primary_term" : 1
}
如果不指定 ID 的话,ES 会自动生成 ID
# 不指定ID , ES 自动生成 ID
POST my_indexs/_doc
{
"name":"joyxj.com",
"age":21
}
对于指定 ID 情况下的 Create 操作,如果 ID 存在,会报错。
{
"error": {
"root_cause": [
{
"type": "version_conflict_engine_exception",
"reason": "[1]: version conflict, document already exists (current version [1])",
"index_uuid": "VSRfGESYRgmsOwMj5HZPxw",
"shard": "0",
"index": "my_indexs"
}
],
"type": "version_conflict_engine_exception",
"reason": "[1]: version conflict, document already exists (current version [1])",
"index_uuid": "VSRfGESYRgmsOwMj5HZPxw",
"shard": "0",
"index": "my_indexs"
},
"status": 409
}
Read 操作
# 语法 get {index}/{type}/{id}
get my_indexs/_doc/1
# 执行结果,其包含 index 、type 、 id 、 version 等信息。 _source 下的 json 内容是我们的查询的本身内容
{
"_index" : "my_indexs",
"_type" : "_doc",
"_id" : "1",
"_version" : 1,
"_seq_no" : 0,
"_primary_term" : 1,
"found" : true,
"_source" : {
"name" : "joyxj.com",
"age" : 21
}
}
Update 操作
对于 Update 操作,文档必须存在,更新操作只会对相应的字段做增量更新,其它字段不会被修改
# 更新操作
POST my_indexs/_update/1
{
"doc" : {
"city" : "shenzhen",
"name": "blog.joyxj.com"
}
}
# 查询下结果
get my_indexs/_doc/1
{
"_index" : "my_indexs",
"_type" : "_doc",
"_id" : "1",
"_version" : 2,
"_seq_no" : 2,
"_primary_term" : 1,
"found" : true,
"_source" : {
"name" : "blog.joyxj.com",
"age" : 21,
"city" : "shenzhen"
}
}
删除操作
可以是删除文档,也可以删除 index。
# 删除文档
delete my_indexs/_doc/1
# 删除 index
delete my_indexs
Bulk API
- 支持在一个 API 中对多个 index 进行操作
- 支持以下 4 种操作
- Index
- Create
- Delete
- Update
- 操作中单条失败,不会影响到其它操作
- 返回结果包含了每一条操作的结果
执行命令
POST _bulk
{ "index" : { "_index" : "test", "_id" : "1" } }
{ "field1" : "value1" }
{ "delete" : { "_index" : "test", "_id" : "2" } }
{ "create" : { "_index" : "test", "_id" : "3" } }
{ "field1" : "value3" }
{ "update" : {"_id" : "1", "_index" : "test"} }
{ "doc" : {"field2" : "value2"} }
返回结果
{
"took" : 272,
"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" : "test",
"_type" : "_doc",
"_id" : "3",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 2,
"_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" : 3,
"_primary_term" : 1,
"status" : 200
}
}
]
}
mget 批量读取
get _mget
{
"docs":[
{
"index":"my_indexs",
"id":"1"
},
{
"index":"test",
"id":"1"
}
]
}
执行结果
{
"docs" : [
{
"_index" : "my_indexs",
"_type" : "_doc",
"_id" : "1",
"_version" : 1,
"_seq_no" : 0,
"_primary_term" : 1,
"found" : true,
"_source" : {
"name" : "joyxj.com",
"age" : 21
}
},
{
"_index" : "test",
"_type" : "_doc",
"_id" : "1",
"_version" : 2,
"_seq_no" : 3,
"_primary_term" : 1,
"found" : true,
"_source" : {
"field1" : "value1",
"field2" : "value2"
}
}
]
}
msearch 批量查询
# 下面的语句执行需要去除注释
GET twitter/_msearch
# 未指定index
{}
{"query" : {"match_all" : {}}, "from" : 0, "size" : 10}
{}
{"query" : {"match_all" : {}}}
# 指定index
{"index" : "twitter2"}
{"query" : {"match_all" : {}}}
上面的查询包含三条查询,其中前二条未指定 index, 其 index 为默认的 twitter。第三条指定了 index 为 twitter2。