如何解决如何找到与弹性搜索别名关联的所有索引?
使用弹性搜索 SDK https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/index.html 如何找到与弹性搜索别名关联的所有索引
我们确实有sdk方法猫。我可以迭代并找到相关索引的别名。但是有没有其他优雅的方法可以实现相同的目标?
解决方法
您可以将别名(或名称数组)作为参数传递。 Docs
const { Client } = require("@elastic/elasticsearch");
var client;
client = new Client({
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);
});
输出
[
{
alias: 'alias_name',index: 'index_name',filter: '-','routing.index': '-','routing.search': '-',is_write_index: '-'
}
]
如果你只想要索引名称
const { Client } = require("@elastic/elasticsearch");
var client;
client = new Client({
node: "http://localhost:9200",name: "alias_name" })
.then((result) => {
const clean_indices = result.body.map(r => r.index)
console.log(clean_indices);
})
.catch((error) => {
console.log(error);
});
,
这是我现在想出来的。
const { Client } = require('@elastic/elasticsearch');
const async = require('async');
var client;
client = new Client({
"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)
});
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。