ThinkChat🤖让你学习和工作更高效,注册即送10W Token,即刻开启你的AI之旅 广告
# 多个 GET API 多 GET API 允许基于索引,类型(可选)和ID(也可能路由)获取多个文档。响应包括获取的 `docs` 列表,每个文件的结构都类似于 GET API 提供文件的结构。下面是一个例子: ``` curl 'localhost:9200/_mget' -d '{ "docs" : [ { "_index" : "test", "_type" : "type", "_id" : "1" }, { "_index" : "test", "_type" : "type", "_id" : "2" } ] }' ``` `mget `也可以针对一个索引(在 body 体中不需要): ``` curl 'localhost:9200/test/_mget' -d '{ "docs" : [ { "_type" : "type", "_id" : "1" }, { "_type" : "type", "_id" : "2" } ] }' ``` 类型如下: ``` curl 'localhost:9200/test/type/_mget' -d '{ "docs" : [ { "_id" : "1" }, { "_id" : "2" } ] }' ``` 在这种情况下,id 可以被用作发起简单的请求: ``` curl 'localhost:9200/test/type/_mget' -d '{ "ids" : ["1", "2"] }' ``` ## 可选类型 该 MGET API 允许`_type`是可选的。将其设置为 `_all 或`空,以获取第一个文档匹配所有类型的ID。 如果不设置类型,许多文件共享相同的 `_id`,你最终将只得到第一个匹配的文件。 例如,如果你有文件1包含 typeA 和 typeB ,那么下面的请求会给你同一个文档两次: ``` curl 'localhost:9200/test/_mget' -d '{ "ids" : ["1", "1"] }' ``` 你需要在这种情况下,明确设置`_type`: ``` GET /test/_mget/ { "docs" : [ { "_type":"typeA", "_id" : "1" }, { "_type":"typeB", "_id" : "1" } ] } ``` Source filtering 默认情况下,`_source `将为每个文档返回(如果储存)。类似于 GET  API,你可以检索的只是部分 `_source `使用的 `_source`参数。您还可以使用URL参数 `_source`,`_source_include`及`_source_exclude 来指定`默认值。例如: ``` curl 'localhost:9200/_mget' -d '{ "docs" : [ { "_index" : "test", "_type" : "type", "_id" : "1", "_source" : false }, { "_index" : "test", "_type" : "type", "_id" : "2", "_source" : ["field3", "field4"] }, { "_index" : "test", "_type" : "type", "_id" : "3", "_source" : { "include": ["user"], "exclude": ["user.location"] } } ] }' ``` ## Fields 通过每个文档来可以指定具体存储字段,类似于 Get API 中 stored_fields 参数。例如: ``` curl 'localhost:9200/_mget' -d '{ "docs" : [ { "_index" : "test", "_type" : "type", "_id" : "1", "stored_fields" : ["field1", "field2"] }, { "_index" : "test", "_type" : "type", "_id" : "2", "stored_fields" : ["field3", "field4"] } ] }' ``` 或者,可以指定 `stored_fields `作为默认值被应用到所有文件中来查询字符串参数。 ``` curl 'localhost:9200/test/type/_mget?stored_fields=field1,field2' -d '{ "docs" : [ { "_id" : "1" (1) }, { "_id" : "2", "stored_fields" : ["field3", "field4"] (2) } ] }' ``` (1)返回 `field1 `和 `field2` (2)返回 `field3 `和 `field4` ## Generated fields 见 “Generated fields” ## Routing 您也可以指定 routing 作为参数: ``` curl 'localhost:9200/_mget?routing=key1' -d '{ "docs" : [ { "_index" : "test", "_type" : "type", "_id" : "1", "_routing" : "key2" }, { "_index" : "test", "_type" : "type", "_id" : "2" } ] }' ``` 在这个例子中,文件 `test/type/2 `将从对应于 routing = `key1 的分片中获取。`但文件 `test/type/1`将被从对应于 routing = `key1 的分片中获取。` ## 安全 见  URL-based access control