您可以使用_msearch API 来实现。
下面是一个示例查询,说明如何应用它:
POST myalias/_msearch
{"index" : "myindex_news_sports"}
{"query" : {"match_all" : {}}, "from" : 0, "size" : 1}
{"index" : "myindex_news_economics"}
{"query" : {"match_all" : {}}, "from" : 0, "size" : 1}
基本上,_msearch 允许您使用相同的 API 搜索多个请求。有两个不同的查询,您可以在其中为两个不同的索引指定size。
请注意,结果将出现在单独的 JSON 组中(两个结果,每个查询一个)。
示例响应:
{
"took" : 4,
"responses" : [
{ <---- Response for Index 1
"took" : 4,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "myindex_news_sports",
"_type" : "_doc",
"_id" : "2",
"_score" : 1.0,
"_source" : {
"fooNested" : {
"sigma" : 9,
"theta" : 1
}
}
},
{
"_index" : "myindex_news_sports",
"_type" : "_doc",
"_id" : "4",
"_score" : 1.0,
"_source" : {
"fooNested" : {
"sigma" : 9,
"theta" : 1
}
}
}
]
},
"status" : 200
},
{ <---- Response for Index 2
"took" : 2,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 4,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "myindex_news_economics",
"_type" : "_doc",
"_id" : "2",
"_score" : 1.0,
"_source" : {
"fooNested" : {
"sigma" : 9,
"theta" : 1
}
}
},
{
"_index" : "myindex_news_economics",
"_type" : "_doc",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"fooNested" : {
"sigma" : 9,
"theta" : 1
}
}
}
]
},
"status" : 200
}
]
}
不确定这是否适合您,希望对您有所帮助。