0

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

1 Answer 1

1

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;
    });
});
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Val. I just copy/pasted your rec but get the same error. Error: Response for getList SHOULD be an array and not an object or something else
First, thank you very much. Your code offered did need minor tweaks. With that said it was perfect for helping me solve this problem. Second, thanks for confirming that the response was just a plain JSON object. It is what I suspected but didn't know how to go about solving the problem. These were the minor tweaks (and if you feel i've done anything amateurish please share): a. what === "_stats/index,store" b. as a part of the if: return extractedData.indices; c. part of else: return extractedData; d. $scope.indices = response; e. added ")" to close the Restangular.all then section.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.