多个 GET API

多 GET API 允许基于索引,类型(可选)和ID(也可能路由)获取多个文档。响应包括获取的 docs 列表,每个文件的结构都类似于 GET API 提供文件的结构。下面是一个例子:

  1. curl 'localhost:9200/_mget' -d '{
  2. "docs" : [
  3. {
  4. "_index" : "test",
  5. "_type" : "type",
  6. "_id" : "1"
  7. },
  8. {
  9. "_index" : "test",
  10. "_type" : "type",
  11. "_id" : "2"
  12. }
  13. ]
  14. }'

mget也可以针对一个索引(在 body 体中不需要):

  1. curl 'localhost:9200/test/_mget' -d '{
  2. "docs" : [
  3. {
  4. "_type" : "type",
  5. "_id" : "1"
  6. },
  7. {
  8. "_type" : "type",
  9. "_id" : "2"
  10. }
  11. ]
  12. }'

类型如下:

  1. curl 'localhost:9200/test/type/_mget' -d '{
  2. "docs" : [
  3. {
  4. "_id" : "1"
  5. },
  6. {
  7. "_id" : "2"
  8. }
  9. ]
  10. }'

在这种情况下,id 可以被用作发起简单的请求:

  1. curl 'localhost:9200/test/type/_mget' -d '{
  2. "ids" : ["1", "2"]
  3. }'

可选类型

该 MGET API 允许_type是可选的。将其设置为 _all 或空,以获取第一个文档匹配所有类型的ID。

如果不设置类型,许多文件共享相同的 _id,你最终将只得到第一个匹配的文件。

例如,如果你有文件1包含 typeA 和 typeB ,那么下面的请求会给你同一个文档两次:

  1. curl 'localhost:9200/test/_mget' -d '{
  2. "ids" : ["1", "1"]
  3. }'

你需要在这种情况下,明确设置_type

  1. GET /test/_mget/
  2. {
  3. "docs" : [
  4. {
  5. "_type":"typeA",
  6. "_id" : "1"
  7. },
  8. {
  9. "_type":"typeB",
  10. "_id" : "1"
  11. }
  12. ]
  13. }

Source filtering

默认情况下,_source将为每个文档返回(如果储存)。类似于 GET API,你可以检索的只是部分 _source使用的 _source参数。您还可以使用URL参数 _source_source_include_source_exclude 来指定默认值。例如:

  1. curl 'localhost:9200/_mget' -d '{
  2. "docs" : [
  3. {
  4. "_index" : "test",
  5. "_type" : "type",
  6. "_id" : "1",
  7. "_source" : false
  8. },
  9. {
  10. "_index" : "test",
  11. "_type" : "type",
  12. "_id" : "2",
  13. "_source" : ["field3", "field4"]
  14. },
  15. {
  16. "_index" : "test",
  17. "_type" : "type",
  18. "_id" : "3",
  19. "_source" : {
  20. "include": ["user"],
  21. "exclude": ["user.location"]
  22. }
  23. }
  24. ]
  25. }'

Fields

通过每个文档来可以指定具体存储字段,类似于 Get API 中 stored_fields 参数。例如:

  1. curl 'localhost:9200/_mget' -d '{
  2. "docs" : [
  3. {
  4. "_index" : "test",
  5. "_type" : "type",
  6. "_id" : "1",
  7. "stored_fields" : ["field1", "field2"]
  8. },
  9. {
  10. "_index" : "test",
  11. "_type" : "type",
  12. "_id" : "2",
  13. "stored_fields" : ["field3", "field4"]
  14. }
  15. ]
  16. }'

或者,可以指定 stored_fields作为默认值被应用到所有文件中来查询字符串参数。

  1. curl 'localhost:9200/test/type/_mget?stored_fields=field1,field2' -d '{
  2. "docs" : [
  3. {
  4. "_id" : "1" (1)
  5. },
  6. {
  7. "_id" : "2",
  8. "stored_fields" : ["field3", "field4"] (2)
  9. }
  10. ]
  11. }'

(1)返回 field1field2

(2)返回 field3field4

Generated fields

见 “Generated fields”

Routing

您也可以指定 routing 作为参数:

  1. curl 'localhost:9200/_mget?routing=key1' -d '{
  2. "docs" : [
  3. {
  4. "_index" : "test",
  5. "_type" : "type",
  6. "_id" : "1",
  7. "_routing" : "key2"
  8. },
  9. {
  10. "_index" : "test",
  11. "_type" : "type",
  12. "_id" : "2"
  13. }
  14. ]
  15. }'

在这个例子中,文件 test/type/2将从对应于 routing = key1 的分片中获取。但文件 test/type/1将被从对应于 routing = key1 的分片中获取。

安全

见 URL-based access control