[英]Elasticsearch show only the name of all indices with node js
我正在使用带有节点js的ejs框架。 我可以获得json格式的所有索引名称。 但是我想要的只是索引的名称。 我的代码如下所示-
var client = require('../routes/Connection.js');
//display all indexes
module.exports.allIndexes = function (searchData, callback) {
client.indices.getAliases({
index: "_all",
level: "indices"
}, function (error, response, status) {
if (error) {
console.log("search error: " + error)
}
else {
//callback(response);---> this works
callback(response.hits.hits); // ---> this doesn't
}
});
}
如果我在回调中使用response,则会得到以下输出:
{
"index-1": {
"aliases": {}
},
"index-2": {
"aliases": {}
},
"index-3": {
"aliases": {}
},
"index-4": {
"aliases": {}
}
}
当我在回调中使用response.hits.hits时,出现错误:“无法读取未定义的属性'hits'”。 我只想将索引名称显示为列表。 仅供参考,在前端,我通过了响应,并说“结果”:
<h1>Index</h1>
<% for(var i=0; i < results.length; i++) { %>
<%= results[i].indices %>
<% } %>
这什么也没显示。
编辑_1:
我将模块导入如下:在我的index.js
:
router.post('/indexes', function (req, res) {
elasticModule.allIndexes(req.body, function (data) {
res.render('elasticGui', { title: 'Elasticsearch GUI', results: data });
});
});
在查看您的回复结构时
{
"index-1": {
"aliases": {}
},
"index-2": {
"aliases": {}
},
"index-3": {
"aliases": {}
},
"index-4": {
"aliases": {}
}
}
您没有看到任何hits
,是吗? 因此, response.hits
是未定义的,并且尝试从response.hits
引用hits
时会发生错误
您只需要像这样更改代码:
callback(Object.keys(response));
然后在您的视图中,您可以像这样遍历该数组:
<% results.forEach(function(index) { %>
<%= index %>
<% }); %>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.