Elasticsearch(036):es中批量操作之mget(批量查询)

分手后的思念是犯贱 2022-12-17 05:27 570阅读 0赞

mget(批量查询)

一、介绍

mgetes提供的一种批量的读取操作的API。

mget API允许根据索引,类型(可选)id(以及可能的路由)获取多个文档。响应包括一个docs数组。该数组具有与原始多重获取请求相对应的所有提取文档(如果特定的get失败,则在响应中包含包含此错误的对象)。成功获取的结构与get API提供的文档的结构相似。

二、示例用法

2.1 测试数据准备

示例索引结构如下

  1. PUT example
  2. PUT example/docs/_mapping
  3. {
  4. "properties": {
  5. "id": { "type": "long"},
  6. "name": { "type": "text"},
  7. "counter": { "type": "integer"},
  8. "tags": { "type": "text"}
  9. }
  10. }
  11. PUT example/docs/1
  12. {
  13. "id": 1,
  14. "name": "admin",
  15. "counter": 1,
  16. "tags": ["gray"]
  17. }
  18. PUT example/docs/2
  19. {
  20. "id": 2,
  21. "name": "zhangsan",
  22. "counter": 1,
  23. "tags": ["black"]
  24. }
  25. PUT example/docs/3
  26. {
  27. "id": 3,
  28. "name": "lisi",
  29. "counter": 1,
  30. "tags": ["purple"]
  31. }
  32. PUT example_test
  33. PUT example_test/docs/_mapping
  34. {
  35. "properties": {
  36. "id": { "type": "long"},
  37. "name": { "type": "text"},
  38. "counter": { "type": "integer"},
  39. "tags": { "type": "text"}
  40. }
  41. }
  42. PUT example_test/docs/1
  43. {
  44. "id": 1,
  45. "name": "test-admin",
  46. "counter": 1,
  47. "tags": ["gray"]
  48. }
  49. PUT example_test/docs/2
  50. {
  51. "id": 2,
  52. "name": "test-zhangsan",
  53. "counter": 1,
  54. "tags": ["black"]
  55. }
  56. PUT example_test/docs/3
  57. {
  58. "id": 3,
  59. "name": "test-lisi",
  60. "counter": 1,
  61. "tags": ["purple"]
  62. }
  63. #查询example所有数据
  64. GET example/docs/_search
  65. {
  66. "query": {
  67. "match_all": { }
  68. }
  69. }
  70. #查询example_test所有数据
  71. GET example_test/docs/_search
  72. {
  73. "query": {
  74. "match_all": { }
  75. }
  76. }

2.2 传统的单条查询,在面对需要查询多个时,需要一次次发送请求到es

示例查询

  1. GET example/docs/1
  2. GET example/docs/2
  3. GET example_test/docs/1
  4. GET example_test/docs/2

这样其实局限性,多次的网络请求很容易造成资源浪费降低性能

2.3 mget之查询(index不同)

示例如下

  1. GET _mget
  2. {
  3. "docs": [
  4. {
  5. "_index": "example",
  6. "_type": "docs",
  7. "_id": "1"
  8. },
  9. {
  10. "_index": "example",
  11. "_type": "docs",
  12. "_id": "2"
  13. },
  14. {
  15. "_index": "example_test",
  16. "_type": "docs",
  17. "_id": "1"
  18. },
  19. {
  20. "_index": "example_test",
  21. "_type": "docs",
  22. "_id": "2"
  23. }
  24. ]
  25. }

返回数据如下

  1. {
  2. "docs": [
  3. {
  4. "_index": "example",
  5. "_type": "docs",
  6. "_id": "1",
  7. "_version": 1,
  8. "found": true,
  9. "_source": {
  10. "id": 1,
  11. "name": "admin",
  12. "counter": 1,
  13. "tags": [
  14. "gray"
  15. ]
  16. }
  17. },
  18. {
  19. "_index": "example",
  20. "_type": "docs",
  21. "_id": "2",
  22. "_version": 1,
  23. "found": true,
  24. "_source": {
  25. "id": 2,
  26. "name": "zhangsan",
  27. "counter": 1,
  28. "tags": [
  29. "black"
  30. ]
  31. }
  32. },
  33. {
  34. "_index": "example_test",
  35. "_type": "docs",
  36. "_id": "1",
  37. "_version": 1,
  38. "found": true,
  39. "_source": {
  40. "id": 1,
  41. "name": "test-admin",
  42. "counter": 1,
  43. "tags": [
  44. "gray"
  45. ]
  46. }
  47. },
  48. {
  49. "_index": "example_test",
  50. "_type": "docs",
  51. "_id": "2",
  52. "_version": 1,
  53. "found": true,
  54. "_source": {
  55. "id": 2,
  56. "name": "test-zhangsan",
  57. "counter": 1,
  58. "tags": [
  59. "black"
  60. ]
  61. }
  62. }
  63. ]
  64. }

2.4 mget之查询(index相同,type不同)

不建议掌握,type有可能在后续的版本废除。这里只做一下简单示例。

  1. GET _mget
  2. {
  3. "docs": [
  4. {
  5. "_index": "example",
  6. "_type": "docs1",
  7. "_id": "1"
  8. },
  9. {
  10. "_index": "example",
  11. "_type": "docs2",
  12. "_id": "1"
  13. },
  14. ]

2.5 mget之查询(index相同,type相同)。

示例如下

  1. GET _mget
  2. {
  3. "docs": [
  4. {
  5. "_index": "example",
  6. "_type": "docs",
  7. "_id": "1"
  8. },
  9. {
  10. "_index": "example",
  11. "_type": "docs",
  12. "_id": "2"
  13. },
  14. {
  15. "_index": "example",
  16. "_type": "docs",
  17. "_id": "3"
  18. }
  19. ]
  20. }
  21. #index和type相同的话可以简化成如下
  22. GET example/docs/_mget
  23. {
  24. "docs": [
  25. {
  26. "_id": "1"
  27. },
  28. {
  29. "_id": "2"
  30. },
  31. {
  32. "_id": "3"
  33. }
  34. ]
  35. }

返回结果

  1. {
  2. "docs": [
  3. {
  4. "_index": "example",
  5. "_type": "docs",
  6. "_id": "1",
  7. "_version": 1,
  8. "found": true,
  9. "_source": {
  10. "id": 1,
  11. "name": "admin",
  12. "counter": 1,
  13. "tags": [
  14. "gray"
  15. ]
  16. }
  17. },
  18. {
  19. "_index": "example",
  20. "_type": "docs",
  21. "_id": "2",
  22. "_version": 1,
  23. "found": true,
  24. "_source": {
  25. "id": 2,
  26. "name": "zhangsan",
  27. "counter": 1,
  28. "tags": [
  29. "black"
  30. ]
  31. }
  32. },
  33. {
  34. "_index": "example",
  35. "_type": "docs",
  36. "_id": "3",
  37. "_version": 1,
  38. "found": true,
  39. "_source": {
  40. "id": 3,
  41. "name": "lisi",
  42. "counter": 1,
  43. "tags": [
  44. "purple"
  45. ]
  46. }
  47. }
  48. ]
  49. }

mget是很重要的,一般来说,进行查询的时候,如果一次性查询多条数据的话,那么一定要用batch批量操作的api,尽可能减少网络开销次数,可以将性能大幅度提升。

发表评论

表情:
评论列表 (有 0 条评论,570人围观)

还没有评论,来说两句吧...

相关阅读

    相关 ESmget批量查询

    1.批量查询的好处 1)一条一条查询,需要发送多次,网络开销大,批量查询可以解决很多网络的开销 2)使用mget 2.使用mget进行查询操作 1)如果批量查询的对象不

    相关 elasticsearch mget批量查询

    一、介绍 批量查询的好处 就是一条一条的查询,比如说要查询100条数据,那么就要发送100次网络请求,这个开销还是很大的,如果进行批量查询的话,查询100条数据,就

    相关 es mget批量查询

    批量查询的好处 就是一条一条的查询,比如说要查询100条数据,那么就要发送100次网络请求,这个开销还是很大的,如果进行批量查询的话, 查询100条数据,就只要发送1次