笔记四:文档的基本 CRUD 与批量操作
文档的CRUD操作
— | — | — | – |
---|---|---|---|
Index | PUT | my_index/_doc/1(ID不存在会创建新的,否则会替换现有文档,版本增加) | {“user”:”mike,”comment”:”You know ,for search “} |
Create | PUT | my_index/_create/1(如果ID已存在会失败) | {“user”:”mike,”comment”:”You know ,for search “} |
Create | POST | my_index/_doc/1(不指定ID,自动生成) | {“user”:”mike,”comment”:”You know ,for search “} |
Read | GET | my_index/_doc/1 | {“user”:”mike,”comment”:”You know ,for search “} |
Update | POST | my_index/_update/1(文档必须存在,更新只会对相应字段做增量修改) | { “doc”: {“user”:”mike,”comment”:”You know Elasticserch “} } |
Delete | DELETE | my_index/_doc/1 | {“user”:”mike,”comment”:”You know ,for search “} |
Create
支持自动生成文档Id 和指定文档Id 两种方式
通过调用 “POST /users/_doc”
- 会自动成功document Id
使用 HTTP PUT /user/_create/1 创建时,URI中显示指定 _create ,此时如果id存在,操作失败
POST users/_doc { "fristName": "sunke", "lastName" : "Lee" , "tags" : ["guitar","ball"] } //执行结果 { "_index" : "users", "_type" : "_doc", "_id" : "zX7CxG0BUbmjDJcPW5Fs", "_version" : 1, "result" : "created", "_shards" : { "total" : 2, "successful" : 2, "failed" : 0 }, "_seq_no" : 1, "_primary_term" : 1 } PUT users/_create/1 { "fristName": "sunke", "lastName" : "Lee" , "tags" : ["guitar","ball"] } //第一次执行结果 { "_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": "vgfH8pmURlmodwq8Zp0elw", "shard": "0", "index": "users" } ], "type": "version_conflict_engine_exception", "reason": "[1]: version conflict, document already exists (current version [1])", "index_uuid": "vgfH8pmURlmodwq8Zp0elw", "shard": "0", "index": "users" }, "status": 409 }
GET
找到文档,返回HTTP 200
- 文档元信息
- _index / _type /
- 版本信息,同一个ID的文档,即将被删除,Version 号也会不断增加
- _source 中默认包含了文档的所有信息
- 文档元信息
- 找不到文档,返回HTTP 404
GET users/_doc/1 { "_index" : "users", "_type" : "_doc", "_id" : "1", "_version" : 3, "result" : "updated", "_shards" : { "total" : 2, "successful" : 2, "failed" : 0 }, "_seq_no" : 3, "_primary_term" : 1 }
Index 文档
- Index 跟 Create 不一样的地方 : 如果文档不存在,就索引新的文档、否则现有文档会被删除,新的文档被索引。版本信息 + 1
PUT users/_doc/1 { "tags":["guitar","ball","reading"] } { "_index" : "users", "_type" : "_doc", "_id" : "1", "_version" : 6, // 版本 "result" : "updated", "_shards" : { "total" : 2, "successful" : 2, "failed" : 0 }, "_seq_no" : 6, "_primary_term" : 1 }
Update 文档
- Update 方法不会删除原来的文档,而是实现真正的数据更新
- Post 方法 / Payload 需要包含在 “doc” 中
POST /users/_update/1 { "doc": { "albums" : ["Alnum1","Alumb2"] } } //操作完 返回结果 { "_index" : "users", "_type" : "_doc", "_id" : "1", "_version" : 8, "result" : "updated", "_shards" : { "total" : 2, "successful" : 2, "failed" : 0 }, "_seq_no" : 8, "_primary_term" : 1 } GET users/_doc/1 { "_index" : "users", "_type" : "_doc", "_id" : "1", "_version" : 8, "_seq_no" : 8, "_primary_term" : 1, "found" : true, "_source" : { "tags" : [ "guitar", "ball", "reading" ], "albums" : [ "Alnum1", "Alumb2", "Alumb3" ] } }
- Index 跟 Create 不一样的地方 : 如果文档不存在,就索引新的文档、否则现有文档会被删除,新的文档被索引。版本信息 + 1
BULK API
目的是在一次API调用中,对不同的索引进行操作
支持四种类型的操作
- Index
- Create
- Update
- Delete
可以在URL中指Index,也可以在请求的Payload中进行
操作中单条操作失败,并不会影响其他操作
返回结果包括每一条操作执行的结果
POST _bulk { "index" : { "_index" : "test", "_id" : "1" } } { "field1" : "value1" } { "delete" : { "_index" : "test", "_id" : "2" } } { "create" : { "_index" : "test2", "_id" : "3" } } { "field1" : "value3" } { "update" : {"_index" : "test" , "_id" : "1" } } { "doc" : {"field2" : "value2"} } //返回4个操作结果 { "took" : 324, "errors" : false, "items" : [ { "index" : { "_index" : "test", "_type" : "_doc", "_id" : "1", "_version" : 5, "result" : "updated", "_shards" : { "total" : 2, "successful" : 2, "failed" : 0 }, "_seq_no" : 5, "_primary_term" : 1, "status" : 200 } }, { "delete" : { "_index" : "test", "_type" : "_doc", "_id" : "2", "_version" : 2, "result" : "not_found", "_shards" : { "total" : 2, "successful" : 2, "failed" : 0 }, "_seq_no" : 6, "_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" : 6, "result" : "updated", "_shards" : { "total" : 2, "successful" : 2, "failed" : 0 }, "_seq_no" : 7, "_primary_term" : 1, "status" : 200 } } ] }
批量读取- mget
- 批量操作,可以减少网络连接所产生的开销,提高性能
GET /_mget { "docs" : [ { "_index" : "test", "_id" : "1" }, { "_index" : "test", "_id" : "2" } ] } //返回结果 { "docs" : [ { "_index" : "test", "_type" : "_doc", "_id" : "1", "_version" : 6, "_seq_no" : 7, "_primary_term" : 1, "found" : true, "_source" : { "field1" : "value1", "field2" : "value2" } }, { "_index" : "test", "_type" : "_doc", "_id" : "2", "found" : false } ] }
批量查询 -msearch
- 对不同的索引,进行不同的search
// 由于我这边没有测试数据 所以复制了下代码没有敲 POST kibana_sample_data_ecommerce/_msearch {} {"query" : {"match_all" : {}},"size":1} {"index" : "kibana_sample_data_flights"} {"query" : {"match_all" : {}},"size":2}
常见错误返回
问题 原因 无法连接 网络故障或者集群挂了 连接无法关闭 网络故障或者节点出错 429 集群过于繁忙(重试或者增加节点已增加吞吐量) 4xx 请求体格式有误 500 集群内部错误
常见问题
- Q:不要发送过多的数据
- A:一般1000-5000个左右文档,5-15M
个人总结
` index 方法 就是 先删除 后先入 ,update 方法就是 修改原数据,两者都会使版本号往上增加
本作品采用《CC 协议》,转载必须注明作者和本文链接