I'm trying to use the restangular api to capture specific data from an elasticsearch rest call. I've tried it several different ways (including an attempt to use the addResponseInterceptor). Either I'm just not doing it correctly or I don't understand how/if the data is formatted in a way that can be processed by the API.
The elasticsearch call i'm making is '_stats/index,store'. The data returned by it is:
{
"_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
}
}
}
}
}
The data I'm interested in is every one of the indices. Any help on how to capture this using the restangular api would be greatly appreciated.
I've tried using the following to get the data using the 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();
}]);
The get and getList fail with different errors.
The get returns: TypeError: Cannot read property 'toString' of undefined
The getList returns: Error: Response for getList SHOULD be an array and not an object or something else
Thank you, Gregg
Restangular uses promises. Can try doing it like this:
app.controller('MainController', ['$scope', 'Restangular',
function($scope, Restangular) {
Restangular.all('_stats/index,store').getList().then(function(response) {
$scope.indices = response.getList("indices");
});
}
]);
However, since Restangular's getList()
call expects the response to contain a JSON array and the Elasticsearch response is a plain JSON object (i.e. without any arrays in it), we need to tell Restangular where to find the array in the response. Since there are none we can intercept the response and build one ourselves 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;
});
});