pra*_*sso 3 node.js elasticsearch
弹性搜索代码:
POST /_msearch
{ "index": "INDEX_NAME_1", "type": "TYPE_NAME_1" }
{ "query": { "match_all": {}}}
{ "index": "INDEX_NAME_2", "type": "TYPE_NAME_2" }
{ "query": { "match_all": {}}}
Run Code Online (Sandbox Code Playgroud)
参考链接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);
}
})
});
Run Code Online (Sandbox Code Playgroud)
上面的节点 js 查询适用于一种类型:“callEnd”。请帮助将弹性代码(两种类型)转换为节点 js 代码。
这是msearch 文档。
在你的情况下,它会是这样的:
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.
});
Run Code Online (Sandbox Code Playgroud)
请注意, msearch 会返回 promise 本身,因此无需自己创建。