简介

为了能更快的同时检索多个文档
mget API参数是一个docs数组,数组的每个节点定义一个文档的_index,_type,_id元数据

image.png

我们先创建出一些数据,然后我们看下

  1. GET /library/books/1
  2. GET /library/books/2
  3. GET /library/books/3
  4. GET /shakespeare/line/1
  5. GET /shakespeare/line/2
  6. GET /shakespeare/line/3

一起获取下

  1. # 数组[]
  2. GET /_mget
  3. {
  4. "docs": [
  5. {
  6. "_index": "library",
  7. "_type": "books",
  8. "_id": 1
  9. },
  10. {
  11. "_index": "library",
  12. "_type": "books",
  13. "_id": 2
  14. },
  15. {
  16. "_index": "library",
  17. "_type": "books",
  18. "_id": 3
  19. },
  20. {
  21. "_index": "shakespeare",
  22. "_type": "line",
  23. "_id": 1
  24. },
  25. {
  26. "_index": "shakespeare",
  27. "_type": "line",
  28. "_id": 2
  29. },
  30. {
  31. "_index": "shakespeare",
  32. "_type": "line",
  33. "_id": 3
  34. }
  35. ]
  36. }

可以指定你想获取的字段

  1. # 也可以指定_source字段,获取你想要的
  2. GET /_mget
  3. {
  4. "docs": [
  5. {
  6. "_index": "library",
  7. "_type": "books",
  8. "_id": 1,
  9. "_source": "title"
  10. },
  11. {
  12. "_index": "library",
  13. "_type": "books",
  14. "_id": 2,
  15. "_source": "name"
  16. },
  17. {
  18. "_index": "library",
  19. "_type": "books",
  20. "_id": 3,
  21. "_source": "price"
  22. },
  23. {
  24. "_index": "shakespeare",
  25. "_type": "line",
  26. "_id": 1,
  27. "_source": "name"
  28. },
  29. {
  30. "_index": "shakespeare",
  31. "_type": "line",
  32. "_id": 2,
  33. "_source": ["name","password"]
  34. },
  35. {
  36. "_index": "shakespeare",
  37. "_type": "line",
  38. "_id": 3,
  39. "_source": "password"
  40. }
  41. ]
  42. }

获取相同index相同type下不同ID的文档

  1. GET /library/books/_mget
  2. {
  3. "docs": [
  4. {"_id": 1},
  5. {"type":"books","_id":3}
  6. ]
  7. }

可以这样简便的写

  1. GET /library/books/_mget
  2. {
  3. "ids": ["1", "3"]
  4. }