• ElasticSearch中批量操作(批量查询_mget、批量插入删除_bulk)


    有时候可以通过批量操作来减少网络请求。如:批量查询、批量插入数据。

    批量查询_mget

    1. # 当查询的数据都存在时
    2. POST /test/user/_mget
    3. # 请求数据
    4. {
    5. "ids": [
    6. "1002",
    7. "1003"
    8. ]
    9. }
    10. # 响应结果
    11. {
    12. "docs": [
    13. {
    14. "_index": "test",
    15. "_type": "user",
    16. "_id": "1002",
    17. "_version": 5,
    18. "_seq_no": 10,
    19. "_primary_term": 1,
    20. "found": true,
    21. "_source": {
    22. "id": 1002,
    23. "name": "李四",
    24. "age": 23,
    25. "sex": "女"
    26. }
    27. },
    28. {
    29. "_index": "test",
    30. "_type": "user",
    31. "_id": "1003",
    32. "_version": 1,
    33. "_seq_no": 11,
    34. "_primary_term": 1,
    35. "found": true,
    36. "_source": {
    37. "id": 1003,
    38. "name": "王五",
    39. "age": 27,
    40. "sex": "男"
    41. }
    42. }
    43. ]
    44. }

    当某一条数据不存在,不影响整体响应,需要通过found的值进行判断是否查询到数据。 

    1. # 当查询的数据有不存在的时候
    2. POST /test/user/_mget
    3. # 请求数据
    4. {
    5. "ids": [
    6. "1002",
    7. "1007"
    8. ]
    9. }
    10. # 响应结果
    11. {
    12. "docs": [
    13. {
    14. "_index": "test",
    15. "_type": "user",
    16. "_id": "1002",
    17. "_version": 5,
    18. "_seq_no": 10,
    19. "_primary_term": 1,
    20. "found": true,
    21. "_source": {
    22. "id": 1002,
    23. "name": "李四",
    24. "age": 23,
    25. "sex": "女"
    26. }
    27. },
    28. {
    29. "_index": "test",
    30. "_type": "user",
    31. "_id": "1007",
    32. "found": false
    33. }
    34. ]
    35. }

    _bulk操作

            在Elasticsearch中,支持批量的插入、修改、删除操作,都是通过_bulk的api完成的。 请求格式如下:(请求格式不同寻常)

    1. { action: { metadata }}\n
    2. { request body }\n
    3. { action: { metadata }}\n
    4. { request body }\n
    5. ...

    批量插入数据:

    1. {"create":{"_index":"test","_type":"user","_id":2001}}
    2. {"id":2001,"name":"name1","age": 20,"sex": "男"}
    3. {"create":{"_index":"test","_type":"user","_id":2002}}
    4. {"id":2002,"name":"name2","age": 20,"sex": "男"}
    5. {"create":{"_index":"test","_type":"user","_id":2003}}
    6. {"id":2003,"name":"name3","age": 20,"sex": "男"}

    注意:最后一行要有一个回车。

    1. POST /test/user/_bulk
    2. #请求数据
    3. {"create":{"_index":"test","_type":"user","_id":2001}}
    4. {"id":2001,"name":"name1","age": 20,"sex": "男"}
    5. {"create":{"_index":"test","_type":"user","_id":2002}}
    6. {"id":2002,"name":"name2","age": 20,"sex": "男"}
    7. {"create":{"_index":"test","_type":"user","_id":2003}}
    8. {"id":2003,"name":"name3","age": 20,"sex": "男"}
    9. # 响应结果
    10. {
    11. "took": 12,
    12. "errors": false,
    13. "items": [
    14. {
    15. "create": {
    16. "_index": "test",
    17. "_type": "user",
    18. "_id": "2001",
    19. "_version": 1,
    20. "result": "created",
    21. "_shards": {
    22. "total": 2,
    23. "successful": 1,
    24. "failed": 0
    25. },
    26. "_seq_no": 13,
    27. "_primary_term": 1,
    28. "status": 201
    29. }
    30. },
    31. {
    32. "create": {
    33. "_index": "test",
    34. "_type": "user",
    35. "_id": "2002",
    36. "_version": 1,
    37. "result": "created",
    38. "_shards": {
    39. "total": 2,
    40. "successful": 1,
    41. "failed": 0
    42. },
    43. "_seq_no": 14,
    44. "_primary_term": 1,
    45. "status": 201
    46. }
    47. },
    48. {
    49. "create": {
    50. "_index": "test",
    51. "_type": "user",
    52. "_id": "2003",
    53. "_version": 1,
    54. "result": "created",
    55. "_shards": {
    56. "total": 2,
    57. "successful": 1,
    58. "failed": 0
    59. },
    60. "_seq_no": 15,
    61. "_primary_term": 1,
    62. "status": 201
    63. }
    64. }
    65. ]
    66. }

     批量删除

    1. {"delete":{"_index":"test","_type":"user","_id":2001}}
    2. {"delete":{"_index":"test","_type":"user","_id":2002}}
    3. {"delete":{"_index":"test","_type":"user","_id":2003}}
    1. POST /test/user/_bulk
    2. # 请求数据
    3. {"delete":{"_index":"test","_type":"user","_id":2001}}
    4. {"delete":{"_index":"test","_type":"user","_id":2002}}
    5. {"delete":{"_index":"test","_type":"user","_id":2003}}
    6. # 响应数据
    7. {
    8. "took": 11,
    9. "errors": false,
    10. "items": [
    11. {
    12. "delete": {
    13. "_index": "test",
    14. "_type": "user",
    15. "_id": "2001",
    16. "_version": 2,
    17. "result": "deleted",
    18. "_shards": {
    19. "total": 2,
    20. "successful": 1,
    21. "failed": 0
    22. },
    23. "_seq_no": 16,
    24. "_primary_term": 1,
    25. "status": 200
    26. }
    27. },
    28. {
    29. "delete": {
    30. "_index": "test",
    31. "_type": "user",
    32. "_id": "2002",
    33. "_version": 2,
    34. "result": "deleted",
    35. "_shards": {
    36. "total": 2,
    37. "successful": 1,
    38. "failed": 0
    39. },
    40. "_seq_no": 17,
    41. "_primary_term": 1,
    42. "status": 200
    43. }
    44. },
    45. {
    46. "delete": {
    47. "_index": "test",
    48. "_type": "user",
    49. "_id": "2003",
    50. "_version": 2,
    51. "result": "deleted",
    52. "_shards": {
    53. "total": 2,
    54. "successful": 1,
    55. "failed": 0
    56. },
    57. "_seq_no": 18,
    58. "_primary_term": 1,
    59. "status": 200
    60. }
    61. }
    62. ]
    63. }

    ElasticSearch一次请求多少性能最高?

    • 整个批量请求需要被加载到接受我们请求节点的内存里,所以请求越大,给其它请求可用的内存就越小。有一 个最佳的bulk请求大小。超过这个大小,性能不再提升而且可能降低。
    • 最佳大小,当然并不是一个固定的数字。它完全取决于你的硬件、你文档的大小和复杂度以及索引和搜索的负 载。
    • 幸运的是,这个最佳点(sweetspot)还是容易找到的:试着批量索引标准的文档,随着大小的增长,当性能开始 降低,说明你每个批次的大小太大了。开始的数量可以在1000~5000个文档之间,如果你的文档非常大,可以使用较小的批次。
    • 通常着眼于你请求批次的物理大小是非常有用的。一千个1kB的文档和一千个1MB的文档大不相同。一个好的 批次最好保持在5-15MB大小间。
  • 相关阅读:
    python数据类型及用法
    嵌入式软件开发新趋势:DevOps
    FreeRTOS移植
    蓝蓝设计为教育行业提供软件UI交互设计服务
    Field Play:Runge-Kutta
    【架构设计】作为架构师你应该掌握的画图技术
    IPETRONIK数采与第三方软件集成
    Programming Differential Privacy第十章THE EXPONENTIAL MECHANISM指数机制
    在 Android 中使用 Lambda 的原理
    8.12 PowerBI系列之DAX函数专题-分组内动态TopN和others
  • 原文地址:https://blog.csdn.net/weixin_44799217/article/details/126090874