文档的crud
文档的crud- type名,约定都用_doc
- create - 如果id已经存在,会失败
- index - 如果id不存在,则创建新的文档,否则,先删除现有的文档,再创建新的文档,版本会增加
- update - 文档必须已经存在,更新只会对相应的字段做增量修改
create一个文档
create- 支持自动生成文档id和指定文档id的两种方式
- 通过调用“post/users/_doc”
- 系统会自动生成document id
- 使用http put user/_create/1创建时,uri中显示指定_create,此时如果该id的文档已经存在,则操作失败
get一个文档
get- 找到文档,返回http 200
- 文档原信息
- _index/_type
- 版本信息,同一个id的文档,即使被删除,version号也会不断增加
- _source中默认包含了文档的所有原始信息
- 文档原信息
- 找不到文档,返回http 404
index文档
-
index和create不一样的地方:如果文档不存在,就索引新的文档,否则现有的文档会被删除,新的文档被索引,版本信息+1
index
update文档
- update方法不会删除原来的文档,而是实现真正的数据更新
-
post方法/payload需要包含在“doc”中
update
实际操作
############Create Document############
#create document. 自动生成 _id
POST users/_doc
{
"user" : "Mike",
"post_date" : "2019-04-15T14:12:12",
"message" : "trying out Kibana"
}
#create document. 指定Id。如果id已经存在,报错
PUT users/_doc/1?op_type=create
{
"user" : "Jack",
"post_date" : "2019-05-15T14:12:12",
"message" : "trying out Elasticsearch"
}
#create document. 指定 ID 如果已经存在,就报错
PUT users/_create/1
{
"user" : "Jack",
"post_date" : "2019-05-15T14:12:12",
"message" : "trying out Elasticsearch"
}
### Get Document by ID
#Get the document by ID
GET users/_doc/1
### Index & Update
#Update 指定 ID (先删除,在写入)
GET users/_doc/1
PUT users/_doc/1
{
"user" : "Mike"
}
#GET users/_doc/1
#在原文档上增加字段
POST users/_update/1/
{
"doc":{
"post_date" : "2019-05-15T14:12:12",
"message" : "trying out Elasticsearch"
}
}
### Delete by Id
# 删除文档
DELETE users/_doc/1
### Bulk 操作
#执行两次,查看每次的结果
#执行第1次
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"} }
#执行第2次
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"} }
### mget 操作
GET /_mget
{
"docs" : [
{
"_index" : "test",
"_id" : "1"
},
{
"_index" : "test",
"_id" : "2"
}
]
}
#URI中指定index
GET /test/_mget
{
"docs" : [
{
"_id" : "1"
},
{
"_id" : "2"
}
]
}
GET /_mget
{
"docs" : [
{
"_index" : "test",
"_id" : "1",
"_source" : false
},
{
"_index" : "test",
"_id" : "2",
"_source" : ["field3", "field4"]
},
{
"_index" : "test",
"_id" : "3",
"_source" : {
"include": ["user"],
"exclude": ["user.location"]
}
}
]
}
### msearch 操作
POST kibana_sample_data_ecommerce/_msearch
{}
{"query" : {"match_all" : {}},"size":1}
{"index" : "kibana_sample_data_flights"}
{"query" : {"match_all" : {}},"size":2}
### 清除测试数据
#清除数据
DELETE users
DELETE test
DELETE test2
bulk api
bulk- 支持在一次api调用中,对
不同
的索引进行操作(批量操作) - 支持四种类型的操作
- index
- create
- update
- delete
- 可以在uri中指定index,也可以在请求的payload中进行
- 操作中单条操作失败,并不会影响其他操作
- 返回结果包括了每一条操作执行的结果
批量读取 - mget
批量操作,可以减少网络连接所产生的开销,提高性能
mget
mget-result
批量查询 - msearch
msearch常见错误返回
问题 | 原因 |
---|---|
无法连接 | 网络故障或者集群挂了 |
连接无法关闭 | 网络故障或者节点出错 |
429 | 集群过于繁忙(增加节点/提高吞吐量) |
4xx | 请求体格式有错 |
500 | 集群内部错误 |
在线文档
https://www.elastic.co/guide/en/elasticsearch/reference/7.6/index.html
网友评论