如何找到与 Elastic 搜索别名关联的所有索引?

how can I find all of the indexes associated with an Elastic search alias?

使用弹性搜索 SDK https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/index.html 如何找到与弹性搜索别名关联的所有索引

我们确实有 sdk 方法 cat。我可以在其中迭代并找到关联索引的别名。但是有没有其他优雅的方法可以达到同样的目的?

这就是我现在想到的。

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)
});

您可以将别名(或名称数组)作为参数传递。 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",
  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);
  });