经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » Elasticsearch » 查看文章
elasticsearch6.7 05. Document APIs(9)Bulk API
来源:cnblogs  作者:说_不_得  时间:2019/4/15 9:10:35  对本文有异议

8、Bulk API

可以把多个indexdelete操作放在单个bulk API中执行。这样可以极大地提高索引速度。

/_bulkAPI使用如下的JSON结构:

  1. action_and_meta_data\n
  2. optional_source\n
  3. action_and_meta_data\n
  4. optional_source\n
  5. ....
  6. action_and_meta_data\n
  7. optional_source\n

注意,最后一行数据必须要以\n结尾。发送请求时,Content-Type 标头应设置为 application /x-ndjson。

action可以是indexcreatedeleteupdate操作。indexcreate 操作需要在下一行指定一个文档数据。index操作相当于 index APIcreate操作相当于op_type=createindex操作。delete操作不要指定文档参数,参考delete APIupdate操作仅需文档的部分参数,docupsertscript ,或其他一些可选项。

如果使用curl导入文本作为参数,你必须使用--data-binary标记而不是-d参数。使用--data-binary的json文本数据不能换行:

  1. $ cat requests
  2. { "index" : { "_index" : "test", "_type" : "_doc", "_id" : "1" } }
  3. { "field1" : "value1" }
  4. $ curl -s -H "Content-Type: application/x-ndjson" -XPOST localhost:9200/_bulk --data-binary "@requests"; echo
  5. {"took":7, "errors": false, "items":[{"index":{"_index":"test","_type":"_doc","_id":"1","_version":1,"result":"created","forced_refresh":false}}]}

因为使用\n作为分割符,所以请确保json数据不是格式化的(需要在同一行)。例如:

  1. POST _bulk
  2. { "index" : { "_index" : "test", "_type" : "_doc", "_id" : "1" } }
  3. { "field1" : "value1" }
  4. { "delete" : { "_index" : "test", "_type" : "_doc", "_id" : "2" } }
  5. { "create" : { "_index" : "test", "_type" : "_doc", "_id" : "3" } }
  6. { "field1" : "value3" }
  7. { "update" : {"_id" : "1", "_type" : "_doc", "_index" : "test"} }
  8. { "doc" : {"field2" : "value2"} }

返回结果:

  1. {
  2. "took": 30,
  3. "errors": false,
  4. "items": [
  5. {
  6. "index": {
  7. "_index": "test",
  8. "_type": "_doc",
  9. "_id": "1",
  10. "_version": 1,
  11. "result": "created",
  12. "_shards": {
  13. "total": 2,
  14. "successful": 1,
  15. "failed": 0
  16. },
  17. "status": 201,
  18. "_seq_no" : 0,
  19. "_primary_term": 1
  20. }
  21. },
  22. {
  23. "delete": {
  24. "_index": "test",
  25. "_type": "_doc",
  26. "_id": "2",
  27. "_version": 1,
  28. "result": "not_found",
  29. "_shards": {
  30. "total": 2,
  31. "successful": 1,
  32. "failed": 0
  33. },
  34. "status": 404,
  35. "_seq_no" : 1,
  36. "_primary_term" : 2
  37. }
  38. },
  39. {
  40. "create": {
  41. "_index": "test",
  42. "_type": "_doc",
  43. "_id": "3",
  44. "_version": 1,
  45. "result": "created",
  46. "_shards": {
  47. "total": 2,
  48. "successful": 1,
  49. "failed": 0
  50. },
  51. "status": 201,
  52. "_seq_no" : 2,
  53. "_primary_term" : 3
  54. }
  55. },
  56. {
  57. "update": {
  58. "_index": "test",
  59. "_type": "_doc",
  60. "_id": "1",
  61. "_version": 2,
  62. "result": "updated",
  63. "_shards": {
  64. "total": 2,
  65. "successful": 1,
  66. "failed": 0
  67. },
  68. "status": 200,
  69. "_seq_no" : 3,
  70. "_primary_term" : 4
  71. }
  72. }
  73. ]
  74. }

可以使用 /_bulk, /{index}/_bulk, {index}/{type}/_bulk,在url中指定indextype参数。

每个操作都会分配到对应的分片执行,仅仅action_meta_data 数据在所请求的分片中解析(因为需要预先找出该操作在那个分片上执行)。

客户端应尽量使用{index}/{type}/_bulk减少数据传输。

返回结果会保存每个操作的返回结果,并且即使某个操作失败了也不会影响剩下的操作。

bulk api 没有规定每个bulk中应该包含多少个操作,具体操作数量应该和你操作的工作量相关。(例如工作量少可以包含多点,工作量大的包含少点)

如果使用HTTP API ,确保客户端不要发送 HTTP chunks,因为这样会降低速度。

8.1 乐观锁( Optimistic Concurrency Control)

批量API调用中的每个索引和删除操作可以在其各自的操作和元数据行中包括if_seq_no和if_primary_term参数。if_seq_no和if_primary_term参数根据对现有文档的最后一次修改来设置。有关更多详细信息,请参阅乐观并发控制

8.2 版本号(Versioning)

每个 bulk项 都可指定 _versionversion字段。它的行为和indexdelete操作一样,具体还要基于映射中_version设置。它同样也支持 version_type 查阅versioning

8.3 路由(Routing)

bulk中的每一项都可以指定_routingrouting字段。它会根据_routing自动跟踪索引 / 删除操作的行为。

8.4 等待活跃分片(Wait For Active Shards)

在执行bulk之前,可以指定最小活跃分片数。有关更多详细信息和用法示例,查阅这里

8.5 刷新(refresh)

设置bulk操作所做的更改到搜索可见的时间。查阅 refresh

只有收到批量请求的分片才会受到刷新的影响。想象一下这样的请求_bulk?refresh = wait_for,其中包含三个文档,这些文档恰好被路由到具有五个分片的索引,且请求打落在其中3个不同分片上。那么请求将会等待这三个分片刷新。其他两个分片不参与_bulk请求。

8.6 更新

使用更新操作时,retry_on_conflict可用作操作本身的字段(不在额外的有效负载行中),以指定在版本冲突的情况下应重试更新的次数。

更新操作支持的参数有:doc(部分文档)upsert, doc_as_upsert,script, params (用于脚本), lang (用于脚本)_source

更新操作的示例:

  1. POST _bulk
  2. { "update" : {"_id" : "1", "_type" : "_doc", "_index" : "index1", "retry_on_conflict" : 3} }
  3. { "doc" : {"field" : "value"} }
  4. { "update" : { "_id" : "0", "_type" : "_doc", "_index" : "index1", "retry_on_conflict" : 3} }
  5. { "script" : { "source": "ctx._source.counter += params.param1", "lang" : "painless", "params" : {"param1" : 1}}, "upsert" : {"counter" : 1}}
  6. { "update" : {"_id" : "2", "_type" : "_doc", "_index" : "index1", "retry_on_conflict" : 3} }
  7. { "doc" : {"field" : "value"}, "doc_as_upsert" : true }
  8. { "update" : {"_id" : "3", "_type" : "_doc", "_index" : "index1", "_source" : true} }
  9. { "doc" : {"field" : "value"} }
  10. { "update" : {"_id" : "4", "_type" : "_doc", "_index" : "index1"} }
  11. { "doc" : {"field" : "value"}, "_source": true}

安全

查阅URL-based access control

原文链接:http://www.cnblogs.com/wtc1994/p/10708065.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号