使用弹性搜索 SDKhttps://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/index.html如何查找与弹性搜索别名关联的所有索引
我们确实有 sdk 方法 cat.aliases,我可以在其中迭代并找到相关的索引。但是有没有其他优雅的方法可以实现相同的?
您可以将别名(或名称数组)作为参数传递。Docs
const { } = require("@elastic/elasticsearch");
var client;
client = new ({
node: "http://localhost:9200",
maxRetries: 5,
requestTimeout: 60000,
sniffOnStart: true,
});
client.cat
.aliases({ format: "json", name: "alias_name" })
.then((result) => {
console.log(result.body);
})
.catch((error) => {
console.log(error);
});
Output
[
{
alias: 'alias_name',
index: 'index_name',
filter: '-',
'routing.index': '-',
'routing.search': '-',
is_write_index: '-'
}
]
如果你只想要索引名称
const { } = require("@elastic/elasticsearch");
var client;
client = new ({
node: "http://localhost:9200",
maxRetries: 5,
requestTimeout: 60000,
sniffOnStart: true,
});
client.cat
.aliases({ format: "json", name: "alias_name" })
.then((result) => {
const clean_indices = result.body.map(r => r.index)
console.log(clean_indices);
})
.catch((error) => {
console.log(error);
});
这就是我现在想出来的。
const { } = require('@elastic/elasticsearch');
const async = require('async');
var client;
client = new ({
"node": "http://localhost:9200",
"maxRetries": 5,
"requestTimeout": 60000,
"sniffOnStart": true
});
client.cat.aliases({format:"json"}).then((result) => {
let indexes={};
result.body.forEach(element => {
if(!indexes[element.alias]){
indexes[element.alias] = [];
}
indexes[element.alias].push(element.index);
});
console.log(JSON.stringify(indexes,null,2));
}).catch((error) => {
console.log(error)
});
本站系公益性非盈利分享网址,本文来自用户投稿,不代表码文网立场,如若转载,请注明出处
评论列表(31条)