elasticsearch 当使用@elastic/elasticStack客户端时,如何获取ElasticStack数据库中的索引列表?

ercv8c1e  于 2023-06-21  发布在  ElasticSearch
关注(0)|答案(1)|浏览(133)

我有一个使用@elastic/elasticstack客户端的NodeJS应用程序。我试图从我的弹性数据库中获取一个索引列表。我是弹性的新手,但是当这个REST端点被调用时,我得到了695个结果,我相信我只创建了几个索引。
调用elastic来查找索引的代码是:

const express = require('express')
const app = express()
const { Client } = require('@elastic/elasticsearch')
const clientOpts = {};  // connect info not shown
const client = new Client(clientOpts)

app.get('/es/indices', async (req, res) => {
    let queryInfo = {
        index: '_all'
    };
    client.indices.stats(queryInfo).then(result => {
        console.log('/es/index got =', JSON.stringify(result.body, null,2));
        res.send(result.body);
    }).catch(ex => {
        console.error('/es/index failed ', ex);
        res.status(500).send({ error: { message: ex.message, stack: ex.stack } });
    })
})

下面是一个简短的片段显示的结果和我的索引之一。

"data": {
    "_shards": {
      "total": 695,
      "successful": 695,
      "failed": 0
    },

      "myindex": {
        "uuid": "xuuoc19mTJyfvjZlyMZVxB",
        "primaries": {
          "docs": {
            "count": 303375,
            "deleted": 0
          },

(Q)如何获取弹性数据库中包含用户数据的索引列表?

查找解决方案

弹性文档

我找到了Elastic文档,但除了重复参数的名称外,它几乎没有什么有用的信息。例如,URL indices.stats将函数定义为:

client.indices.stats({
  metric: string | string[],
  index: string | string[],
  completion_fields: string | string[],
  fielddata_fields: string | string[],
  fields: string | string[],
  groups: string | string[],
  level: 'cluster' | 'indices' | 'shards',
  types: string | string[],
  include_segment_file_sizes: boolean,
  include_unloaded_segments: boolean,
  expand_wildcards: 'open' | 'closed' | 'hidden' | 'none' | 'all',
  forbid_closed_indices: boolean
})

但几乎没有解释参数值的含义/作用。
metric字符串|string[] -限制特定度量返回的信息。
我使用的是ElasticSearch 7.17版本,这就是为什么URL指向该版本。

类似SO问题

我找到了问题/答案:https://stackoverflow.com/a/17429465/3281336,我尝试了,但aliases API返回的远远超过我想要的。

eivgtgni

eivgtgni1#

我发现client.cat() API可以更好地满足我的需求。下面是代码:

app.get('/es/indices', async (req, res) => {
    let paramInfo = {
        index: '_all',
        format: 'json',
        bytes: 'g',
        pri: true
    };
    client.cat.indices(paramInfo).then(result => {
//         console.log('/es/index got =', JSON.stringify(result.body, null,2));
        let f1 = _.filter(result.body, (item) => Number(item["docs.count"]) > 0);
        let f2 = _.map(f1, (item) => ({name: item.index, count: Number(item["docs.count"]) }));
        let f3 = _.sortBy(f2, item => item.name);
        res.send(f3);
    }).catch(ex => {
        console.error('/es/index failed ', ex);
        res.status(500).send({ error: { message: ex.message, stack: ex.stack } });
    })
})

result ing JSON形状看起来像这样:

/es/index got = [
  {
    "health": "green",
    "status": "open",
    "index": "acme-my-cool-data",
    "uuid": "ciPwz2fUTeWPaDLxiR48nA",
    "pri": "1",
    "rep": "1",
    "docs.count": "180",
    "docs.deleted": "0",
    "store.size": "0",
    "pri.store.size": "0"
  },
  {
    "health": "green",
    "status": "open",
    "index": "acme-workspace-3be0c9a988b441b693f84d12791388c2",
    "uuid": "ciPwz2fUTeWPaDLxiR48nB",
    "pri": "1",
    "rep": "1",
    "docs.count": "0",
    "docs.deleted": "0",
    "store.size": "0",
    "pri.store.size": "0"
  },

因此,let f1=处的代码将过滤掉所有包含"doc.count": 0的结果。在我的例子中,存在许多零文档的索引(不知道为什么)。
let f2处的代码将形状转换为UI想要的结果,即索引及其计数的列表,因此{name, count}
let f3中的代码对结果进行排序,以便它们以有意义的顺序返回。

相关问题