我有一个使用@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返回的远远超过我想要的。
1条答案
按热度按时间eivgtgni1#
我发现
client.cat()
API可以更好地满足我的需求。下面是代码:result
ing JSON形状看起来像这样:因此,
let f1=
处的代码将过滤掉所有包含"doc.count": 0
的结果。在我的例子中,存在许多零文档的索引(不知道为什么)。let f2
处的代码将形状转换为UI想要的结果,即索引及其计数的列表,因此{name, count}
。let f3
中的代码对结果进行排序,以便它们以有意义的顺序返回。