简体   繁体   English

Elasticsearch仅显示带有节点js的所有索引的名称

[英]Elasticsearch show only the name of all indices with node js

I am using ejs framework with node js. 我正在使用带有节点js的ejs框架。 I can get all the index names in json format. 我可以获得json格式的所有索引名称。 But what I want is just the names of the indices. 但是我想要的只是索引的名称。 My code looks like as follows- 我的代码如下所示-

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

if I use response in callback, I get the following output: 如果我在回调中使用response,则会得到以下输出:

{
  "index-1": {
    "aliases": {}
  },
  "index-2": {
    "aliases": {}
  },
  "index-3": {
    "aliases": {}
  },
  "index-4": {
    "aliases": {}
  }
}

and when I use response.hits.hits in callback I get the error:"cannot read property 'hits' of undefined". 当我在回调中使用response.hits.hits时,出现错误:“无法读取未定义的属性'hits'”。 I want to show only the index names as a list. 我只想将索引名称显示为列表。 FYI, in front end, I passed the response say as "results" : 仅供参考,在前端,我通过了响应,并说“结果”:

        <h1>Index</h1>
        <% for(var i=0; i < results.length; i++) { %>
          <%= results[i].indices %>
        <% } %>

which shows nothing ofcourse. 这什么也没显示。

edit_1: 编辑_1:

I import the module as follows: in my index.js : 我将模块导入如下:在我的index.js

router.post('/indexes', function (req, res) {
    elasticModule.allIndexes(req.body, function (data) {
        res.render('elasticGui', { title: 'Elasticsearch GUI', results: data });
    });
});

When looking at your response structure 在查看您的回复结构时

{
  "index-1": {
    "aliases": {}
  },
  "index-2": {
    "aliases": {}
  },
  "index-3": {
    "aliases": {}
  },
  "index-4": {
    "aliases": {}
  }
}

you don't see any hits , do you? 您没有看到任何hits ,是吗? So response.hits is undefined and the error happens when trying to reference hits out of response.hits 因此, response.hits是未定义的,并且尝试从response.hits引用hits时会发生错误

You simply need to change your code like this: 您只需要像这样更改代码:

callback(Object.keys(response));

And then in your view you can iterate over that array like this: 然后在您的视图中,您可以像这样遍历该数组:

    <% results.forEach(function(index) { %>
      <%= index %>
    <% }); %>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM