簡體   English   中英

Elasticsearch僅顯示帶有節點js的所有索引的名稱

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM