09.multi-get api操作
文章目錄
- 1. 多個(gè) GET API
- 2. Source filtering
- 3. Fields
- 4. Routing
1. 多個(gè) GET API
多 GET API 允許基于索引,類型(可選)和ID(也可能路由)獲取多個(gè)文檔。響應(yīng)包括獲取的 docs 列表,每個(gè)文件的結(jié)構(gòu)都類似于 GET API 提供文件的結(jié)構(gòu)。下面是一個(gè)例子:
GET /_mget {"docs" : [{"_index" : "test","_type" : "_doc","_id" : "1"},{"_index" : "test","_type" : "_doc","_id" : "2"}] }mget也可以針對(duì)一個(gè)索引(在 body 體中不需要index名稱):
GET /test/_mget {"docs" : [{"_type" : "_doc","_id" : "1"},{"_type" : "_doc","_id" : "2"}] }類型如下:
GET /test/_doc/_mget {"docs" : [{"_id" : "1"},{"_id" : "2"}] }在這種情況下,id 可以被用作發(fā)起簡(jiǎn)單的請(qǐng)求:
GET /test/_doc/_mget {"ids" : ["1", "2"] }2. Source filtering
默認(rèn)情況下,每個(gè)文檔返回_source(如果儲(chǔ)存)。類似于 GET API,你可以檢索的只是部分 _source,使用 _source參數(shù)。您還可以使用URL參數(shù) _source,_source_include及_source_exclude 來(lái)指定默認(rèn)值。例如:
GET /_mget {"docs" : [{"_index" : "test","_type" : "_doc","_id" : "1","_source" : false},{"_index" : "test","_type" : "_doc","_id" : "2","_source" : ["field3", "field4"]},{"_index" : "test","_type" : "_doc","_id" : "3","_source" : {"include": ["user"],"exclude": ["user.location"]}}] }3. Fields
通過每個(gè)文檔來(lái)可以指定具體存儲(chǔ)字段,類似于 Get API 中 stored_fields 參數(shù)。例如:
GET /_mget {"docs" : [{"_index" : "test","_type" : "_doc","_id" : "1","stored_fields" : ["field1", "field2"]},{"_index" : "test","_type" : "_doc","_id" : "2","stored_fields" : ["field3", "field4"]}] }或者,可以指定 stored_fields作為默認(rèn)值被應(yīng)用到所有文件中來(lái)查詢字符串參數(shù)。
GET /test/_doc/_mget?stored_fields=field1,field2 {"docs" : [{"_id" : "1" },{"_id" : "2","stored_fields" : ["field3", "field4"] }] }(1)返回 field1和 field2
(2)返回 field3和 field4
4. Routing
您也可以指定 routing 作為參數(shù):
GET /_mget?routing=key1 {"docs" : [{"_index" : "test","_type" : "_doc","_id" : "1","routing" : "key2"},{"_index" : "test","_type" : "_doc","_id" : "2"}] }在這個(gè)例子中,doc id 為2的doc 會(huì)從 routing = key1 的分片中獲取。但文件 doc id 1的doc 將被從對(duì)應(yīng)于 routing = key1 的分片中獲取。
總結(jié)
以上是生活随笔為你收集整理的09.multi-get api操作的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 08.update_by_query操作
- 下一篇: 10.bulk操作