文档
create一个文档
- 支持自动生成文档Id和指定文档ld两种方式
- 通过调用“post /users/_ _doc’系统会自动生成document Id
- 使用HTTP PUT user/_ create/1 创建时,URI中显示指定_ _create, 此时如果该id的文档已经存在,操作失败
PUT users/ _create /1
{
"firstName": "Jack",
"lastName": "Johnson",
"tags":["guitar","skateboard"]
}
get一个文档
- 找到文档, 返回HTTP 200
- 同一个ld的文档,即使被删除,Version号也会不断增加
_souece 中默认包含了文档的所有原始信息
- 同一个ld的文档,即使被删除,Version号也会不断增加
GET users/_doc/1
index文档
Index和Create不一样的地方:如果文档不存在,就索引新的文档。否则现有文档会被删除,新的文档被索引。版本信息(version)+1
PUT users/_doc/1
{
"tags":["guitar", "skateboard","reading"]
}
update文档
- Update方法不会删除原来的文档,而是实现真正的数据更新
- Post方法/ Payload需要包含在“doc”中
POST users/ _update/1
{
"doc": {
"albums": ["Album1" , "ALumb2"]
}
}
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" : "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"
}
]
}
批量查询_msearch
//### msearch操作
POST kibana_ sample_ data_ ecommerce/_ msearch
{}
{"query" : {"match_ all" : {}},"size":1}
{"index" : "kibana_ _sample_ data_ flights"}
{"query" : {"match_ all" : {}},"size":2}
常见错误返回
问题 | 原因 |
---|---|
无法连接 | 网络故障或集群挂了 |
连接无法关闭 | 网络故障或节点出错 |
429 | 集群过于繁忙 |
4xx | 请求体格式有错 |
500 | 集群内部错误 |