使用Multi Get 可以通过索引名、类型名、文档id一次得到一个文档集合,文档可以来自同一个索引库,也可以来自不同索引库。
1、准备数据
PUT /lib/user/1 { "first_name":"Jane1", "last_name":"Smith1", "age":31, "about":"I like to collect rock albums1", "interests":[ "music1" ] } PUT /lib/user/2 { "first_name":"Jane2", "last_name":"Smith2", "age":32, "about":"I like to collect rock albums2", "interests":[ "music2" ] } PUT /lib/user/3 { "first_name":"Jane3", "last_name":"Smith3", "age":33, "about":"I like to collect rock albums1", "interests":[ "music3" ] } PUT /lib2/user2/1 { "first_name":"Jane1", "last_name":"Smith1", "age":31, "about":"I like to collect rock albums1", "interests":[ "music1" ] } PUT /lib2/user2/2 { "first_name":"Jane2", "last_name":"Smith2", "age":32, "about":"I like to collect rock albums2", "interests":[ "music2" ] } PUT /lib2/user2/3 { "first_name":"Jane3", "last_name":"Smith3", "age":31, "about":"I like to collect rock albums3", "interests":[ "music3" ] }
2、操作演示
同时查询到文档id是1、2、3的文档。会查出6条数据
GET /_mget { "docs":[ { "_index":"lib", "_type":"user", "_id":1 }, { "_index":"lib", "_type":"user", "_id":2 }, { "_index":"lib", "_type":"user", "_id":3 }, { "_index":"lib2", "_type":"user2", "_id":1 }, { "_index":"lib2", "_type":"user2", "_id":2 }, { "_index":"lib2", "_type":"user2", "_id":3 } ] }
指定每个文档显示的字段:
GET /_mget { "docs":[ { "_index":"lib", "_type":"user", "_id":1, "_source":"interests" }, { "_index":"lib", "_type":"user", "_id":2, "_source":["age","interests"] } ] }
批量获取文档的简单写法有以下两种
GET /lib/user/_mget { "docs":[ { "_id":1 }, { "_id":2 } ] }
GET /lib/user/_mget { "ids":["1","2"] }