弹性搜索代码:
POST /_msearch
{ "index": "INDEX_NAME_1", "type": "TYPE_NAME_1" }
{ "query": { "match_all": {}}}
{ "index": "INDEX_NAME_2", "type": "TYPE_NAME_2" }
{ "query": { "match_all": {}}}
参考链接http://teknosrc.com/execute-multiple-search-query-elasticsearch/#comment-8578(参见示例1)
节点js代码:
return new Promise(function (resolve, reject) {
elasticClient.search({
index: '*:logstash-prod-*',
type: 'callEnd',
size: 10000,
body: {
query: {
range: {
"@timestamp": {
"gte": startTime,
"lte": endTime
}
}
}
},
}, function (error, response, status) {
if (error) {
reject(error);
}
else {
console.log("show the response" + JSON.stringify(response));
let elasticResponse=response.hits.hits;
resolve(response);
}
})
});
以上节点js查询适用于一种类型:“callEnd”。请帮助将弹性代码(两种类型)转换为节点js代码。
答案 0 :(得分:2)
在您的情况下,将是这样的:
const queryBody = [
{ index: '*:logstash-prod-*', type: 'callEnd1' },
{
query: {
range: {
'@timestamp': {
'gte': startTime,
'lte': endTime
}
}
},
size: 10000
},
{ index: '*:logstash-prod-*', type: 'callEnd2' },
{
query: {
range: {
'@timestamp': {
'gte': startTime,
'lte': endTime
}
}
},
size: 10000
}
];
return elasticClient
.msearch({ body: queryBody })
.then(result => {
console.log('show the response' + JSON.stringify(result));
return result;
})
.catch(error => {
// TODO Handle error here.
});
请注意,msearch返回的信息本身就是promise,因此无需自己创建。