【问题标题】:how can I find all of the indexes associated with an Elastic search alias?如何找到与 Elastic 搜索别名关联的所有索引?
【发布时间】:2021-03-04 21:10:07
【问题描述】:

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

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

【问题讨论】:

    标签: elasticsearch alias


    【解决方案1】:

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

    【讨论】:

    • 是的,我也做了同样的事情,还添加了别名。非常感谢:)
    【解决方案2】:

    这就是我现在想出的。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多