angularjs - 再次调用Elasticsearch数据

标签 angularjs rest elasticsearch restangular

我正在尝试使用restangular api从elasticsearch rest调用中捕获特定数据。我已经尝试了几种不同的方法(包括尝试使用addResponseInterceptor)。我只是没有正确地执行它,或者不了解如何/是否以可以由API处理的方式格式化数据。

我正在做的elasticsearch调用是“_stats / index,store”。它返回的数据是:

{
  "_shards": {
    "total": 12,
    "successful": 6,
    "failed": 0
  },
  "_all": {
    "primaries": {
      "store": {
        "size_in_bytes": 34177,
        "throttle_time_in_millis": 0
      }
    },
    "total": {
      "store": {
        "size_in_bytes": 34177,
        "throttle_time_in_millis": 0
      }
    }
  },
  "indices": {
    "logstash-2015.05.09": {
      "primaries": {
        "store": {
          "size_in_bytes": 575,
          "throttle_time_in_millis": 0
        }
      },
      "total": {
        "store": {
          "size_in_bytes": 575,
          "throttle_time_in_millis": 0
        }
      }
    },
    ".kibana": {
      "primaries": {
        "store": {
          "size_in_bytes": 33602,
          "throttle_time_in_millis": 0
        }
      },
      "total": {
        "store": {
          "size_in_bytes": 33602,
          "throttle_time_in_millis": 0
        }
      }
    }
  }
}

我感兴趣的数据是每个索引。对于如何使用restangular api捕获此问题的任何帮助将不胜感激。

我尝试使用以下内容使用restangular api获取数据:
app.controller('MainController', ['$scope', 'Restangular',
  function($scope, Restangular) {
    var restCall = Restangular.all('_stats/index,store');
    var allData = restCall.get();
    var allData2 = restCall.getList();
  }]);

get和getList失败,并显示不同的错误。

得到的返回:
TypeError:无法读取未定义的属性“toString”

getList返回:
错误:对getList的响应应该是数组,而不是对象或其他东西

谢谢格雷格

最佳答案

矩形使用 promise 。可以尝试这样做:

app.controller('MainController', ['$scope', 'Restangular',
  function($scope, Restangular) {
      Restangular.all('_stats/index,store').getList().then(function(response) {
        $scope.indices = response.getList("indices");
      });
  }
]);

但是,由于Restangular的getList()调用期望响应包含一个JSON数组,而Elasticsearch响应是一个普通的JSON对象(即其中没有任何数组),因此我们需要告诉Restangular在响应中找到数组的位置。既然没有,我们可以截取响应并自己构建一个using addResponseInterceptor
app.config(function(RestangularProvider) {
    // add a response intereceptor
    RestangularProvider.addResponseInterceptor(function(data, operation, what, url, response, deferred) {
      var extractedData;
      if (operation === "getList" && what === "indices") {
        extractedData = {indices: Object.keys(data.indices)};

        // now we have an array called "indices" with the index names "logstash-2015.05.09" and ".kibana" in it

      } else {
        extractedData = data.data;
      }
      return extractedData;
    });
});

关于angularjs - 再次调用Elasticsearch数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30673819/

相关文章:

javascript - 将 JSON 对象与现有对象进行比较

angularjs - 幻灯片中的幻灯片内的 ionic View

rest - 服务栈:IService 和 IRestService

elasticsearch - elasticsearch排序与得分-效率?

javascript - 有必要在 Angular 指令链接内命名函数吗?

javascript - 附加动态变量后从 json 对象获取值

rest - 当我们检查接收时间更长时,带有 $search 过滤器的 Microsoft Graph Mail API 不起作用

jquery - getJSON 调用适用于 IE 7 但不适用于 Firefox 3

elasticsearch - ElasticSearch 2.x:more_like_this查询和嵌套对象

node.js - 在Elasticsearch 6x中何处添加索引缓冲区大小