经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » Elasticsearch » 查看文章
elasticsearch之单请求多查询
来源:cnblogs  作者:无风听海  时间:2023/1/6 8:53:52  对本文有异议

一、需要解决的问题

有的时候我们需要同时执行多个查询,并且需要得到每个单独查询的搜索结果,elasticsearch提供了multi search此需求的支持;

二、elasticsearch multi search简介

elasticsearch提供了multi search api来支持一个请求执行多个查询;

multi search api的请求体使用换行分割的JSON格式;

  1. header\n
  2. body\n
  3. header\n
  4. body\n

multi search返回的结果是responses数组,每个查询对应一个数组元素;每个数组元素都有一个status字段指示查询是否执行成功,如果执行失败则error字段返回错误信息;

每个查询可以通过自己的header设置查询执行的index,也可以是空的JSON对象,这是在URL中指定的index执行查询;

三、数据准备

index以下四个文档

  1. PUT /multi_test/_doc/1
  2. {
  3. "name":"Google Chrome"
  4. }
  5. PUT /multi_test/_doc/2
  6. {
  7. "name":"NotePad"
  8. }
  9. PUT /multi_test/_doc/3
  10. {
  11. "name":"Word"
  12. }
  13. PUT /multi_test/_doc/4
  14. {
  15. "name":"PyCharm"
  16. }

查询查看已经索引的数据

  1. GET /multi_test/_search
  2. {
  3. "took":0,
  4. "timed_out":false,
  5. "_shards":{
  6. "total":5,
  7. "successful":5,
  8. "skipped":0,
  9. "failed":0
  10. },
  11. "hits":{
  12. "total":4,
  13. "max_score":1,
  14. "hits":[
  15. {
  16. "_index":"multi_test",
  17. "_type":"_doc",
  18. "_id":"2",
  19. "_score":1,
  20. "_source":{
  21. "name":"NotePad"
  22. }
  23. },
  24. {
  25. "_index":"multi_test",
  26. "_type":"_doc",
  27. "_id":"4",
  28. "_score":1,
  29. "_source":{
  30. "name":"PyCharm"
  31. }
  32. },
  33. {
  34. "_index":"multi_test",
  35. "_type":"_doc",
  36. "_id":"1",
  37. "_score":1,
  38. "_source":{
  39. "name":"Google Chrome"
  40. }
  41. },
  42. {
  43. "_index":"multi_test",
  44. "_type":"_doc",
  45. "_id":"3",
  46. "_score":1,
  47. "_source":{
  48. "name":"Word"
  49. }
  50. }
  51. ]
  52. }
  53. }

四、查询测试

我们构造以下四个查询同时执行

  1. POST /multi_test/_msearch
  2. {}
  3. {"query":{"match":{"name":"google"}}}
  4. {}
  5. {"query":{"match":{"name":"nohit"}}}
  6. {}
  7. {"query":{"match":{"name":"word"}}}
  8. {}
  9. {"query":{"bool":{"should":[{"match":{"name":"word"}}, {"match":{"name":"pycharm"}}]}}}

我们可以看到不管查询是否命中结果,都会有一个responses数组元素对应;同时responses数组元素的顺序与查询是一 一对应的;

  1. {
  2. "responses":[
  3. {
  4. "took":0,
  5. "timed_out":false,
  6. "_shards":{
  7. "total":5,
  8. "successful":5,
  9. "skipped":0,
  10. "failed":0
  11. },
  12. "hits":{
  13. "total":1,
  14. "max_score":0.2876821,
  15. "hits":[
  16. {
  17. "_index":"multi_test",
  18. "_type":"_doc",
  19. "_id":"1",
  20. "_score":0.2876821,
  21. "_source":{
  22. "name":"Google Chrome"
  23. }
  24. }
  25. ]
  26. },
  27. "status":200
  28. },
  29. {
  30. "took":0,
  31. "timed_out":false,
  32. "_shards":{
  33. "total":5,
  34. "successful":5,
  35. "skipped":0,
  36. "failed":0
  37. },
  38. "hits":{
  39. "total":0,
  40. "max_score":null,
  41. "hits":[
  42. ]
  43. },
  44. "status":200
  45. },
  46. {
  47. "took":0,
  48. "timed_out":false,
  49. "_shards":{
  50. "total":5,
  51. "successful":5,
  52. "skipped":0,
  53. "failed":0
  54. },
  55. "hits":{
  56. "total":1,
  57. "max_score":0.2876821,
  58. "hits":[
  59. {
  60. "_index":"multi_test",
  61. "_type":"_doc",
  62. "_id":"3",
  63. "_score":0.2876821,
  64. "_source":{
  65. "name":"Word"
  66. }
  67. }
  68. ]
  69. },
  70. "status":200
  71. },
  72. {
  73. "took":0,
  74. "timed_out":false,
  75. "_shards":{
  76. "total":5,
  77. "successful":5,
  78. "skipped":0,
  79. "failed":0
  80. },
  81. "hits":{
  82. "total":2,
  83. "max_score":0.6931472,
  84. "hits":[
  85. {
  86. "_index":"multi_test",
  87. "_type":"_doc",
  88. "_id":"4",
  89. "_score":0.6931472,
  90. "_source":{
  91. "name":"PyCharm"
  92. }
  93. },
  94. {
  95. "_index":"multi_test",
  96. "_type":"_doc",
  97. "_id":"3",
  98. "_score":0.2876821,
  99. "_source":{
  100. "name":"Word"
  101. }
  102. }
  103. ]
  104. },
  105. "status":200
  106. }
  107. ]
  108. }

五、multi search对search template的支持

multi search api也支持search template;

muti search内联search template查询

  1. POST /multi_test/_msearch/template
  2. {}
  3. { "source" : "{ \"query\": { \"match\": { \"name\" : \"{{name}}\" } } } }", "params": { "name": "google" } }
  4. {}
  5. { "source" : "{ \"query\": { \"match_{{template}}\": {} } }", "params": { "template": "all" } }

查询结果为

  1. {
  2. "responses":[
  3. {
  4. "took":0,
  5. "timed_out":false,
  6. "_shards":{
  7. "total":5,
  8. "successful":5,
  9. "skipped":0,
  10. "failed":0
  11. },
  12. "hits":{
  13. "total":1,
  14. "max_score":0.2876821,
  15. "hits":[
  16. {
  17. "_index":"multi_test",
  18. "_type":"_doc",
  19. "_id":"1",
  20. "_score":0.2876821,
  21. "_source":{
  22. "name":"Google Chrome"
  23. }
  24. }
  25. ]
  26. }
  27. },
  28. {
  29. "took":8,
  30. "timed_out":false,
  31. "_shards":{
  32. "total":5,
  33. "successful":5,
  34. "skipped":0,
  35. "failed":0
  36. },
  37. "hits":{
  38. "total":4,
  39. "max_score":1,
  40. "hits":[
  41. {
  42. "_index":"multi_test",
  43. "_type":"_doc",
  44. "_id":"2",
  45. "_score":1,
  46. "_source":{
  47. "name":"NotePad"
  48. }
  49. },
  50. {
  51. "_index":"multi_test",
  52. "_type":"_doc",
  53. "_id":"4",
  54. "_score":1,
  55. "_source":{
  56. "name":"PyCharm"
  57. }
  58. },
  59. {
  60. "_index":"multi_test",
  61. "_type":"_doc",
  62. "_id":"1",
  63. "_score":1,
  64. "_source":{
  65. "name":"Google Chrome"
  66. }
  67. },
  68. {
  69. "_index":"multi_test",
  70. "_type":"_doc",
  71. "_id":"3",
  72. "_score":1,
  73. "_source":{
  74. "name":"Word"
  75. }
  76. }
  77. ]
  78. }
  79. }
  80. ]
  81. }

也可以直接新建search template

  1. POST /_scripts/multi_test_name_template/
  2. {
  3. "script":{
  4. "lang":"mustache",
  5. "source":{
  6. "query":{
  7. "bool":{
  8. "should":[
  9. {
  10. "match":{
  11. "name":"{{name}}"
  12. }
  13. },
  14. {
  15. "wildcard":{
  16. "name":"*{{name}}*"
  17. }
  18. }
  19. ]
  20. }
  21. }
  22. }
  23. }
  24. }

使用新建的search template进行搜索

  1. POST /multi_test/_msearch/template
  2. {}
  3. { "id": "multi_test_name_template", "params": { "name": "oo" } }
  4. {}
  5. { "id": "multi_test_name_template", "params": { "name": "notepad" } }

搜索结果如下

  1. {
  2. "responses":[
  3. {
  4. "took":1,
  5. "timed_out":false,
  6. "_shards":{
  7. "total":5,
  8. "successful":5,
  9. "skipped":0,
  10. "failed":0
  11. },
  12. "hits":{
  13. "total":1,
  14. "max_score":1,
  15. "hits":[
  16. {
  17. "_index":"multi_test",
  18. "_type":"_doc",
  19. "_id":"1",
  20. "_score":1,
  21. "_source":{
  22. "name":"Google Chrome"
  23. }
  24. }
  25. ]
  26. }
  27. },
  28. {
  29. "took":1,
  30. "timed_out":false,
  31. "_shards":{
  32. "total":5,
  33. "successful":5,
  34. "skipped":0,
  35. "failed":0
  36. },
  37. "hits":{
  38. "total":1,
  39. "max_score":1.6931472,
  40. "hits":[
  41. {
  42. "_index":"multi_test",
  43. "_type":"_doc",
  44. "_id":"2",
  45. "_score":1.6931472,
  46. "_source":{
  47. "name":"NotePad"
  48. }
  49. }
  50. ]
  51. }
  52. }
  53. ]
  54. }

在search template中使用嵌套字段作为参数

  1. POST /multi_test/_msearch/template
  2. {}
  3. { "source" : "{ \"query\": { \"match\": { \"name\" : \"{{person.name}}\" } } } }", "params": { "person":{"name": "google"} } }

搜索结果如下

  1. {
  2. "responses":[
  3. {
  4. "took":3,
  5. "timed_out":false,
  6. "_shards":{
  7. "total":5,
  8. "successful":5,
  9. "skipped":0,
  10. "failed":0
  11. },
  12. "hits":{
  13. "total":1,
  14. "max_score":0.2876821,
  15. "hits":[
  16. {
  17. "_index":"multi_test",
  18. "_type":"_doc",
  19. "_id":"1",
  20. "_score":0.2876821,
  21. "_source":{
  22. "name":"Google Chrome"
  23. }
  24. }
  25. ]
  26. }
  27. }
  28. ]
  29. }

原文链接:https://www.cnblogs.com/wufengtinghai/p/17028555.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号