2

Is there a way to cancel requests/queries to Elasticsearch using elasticjs? The web app I am working on performs a request/query every 5 seconds, but I would like to cancel the request if for some reason the response doesn't show up in 5 seconds (so the browser doesn't pile up a bunch of requests that are unnecessary since the queries are happening repeatedly). I understand this would not prevent Elasticsearch from completing the query, but I would like to at least cancel the request in the browser.

Example:

var request = ejs.Request().doSearch();
var dataFromElasticsearch;

request.then(function (data) {
    dataFromElasticsearch = data;   
});

setTimeout(function () {
    if (!dataFromElasticsearch) {
        //do something here to cancel request
    }
}, 5000)
chevin99
  • 4,946
  • 7
  • 24
  • 32

1 Answers1

1

Per documentation for elasticsearch.js (3.1, at the time of writing):

...calling the API will return an object (either a promise or just a plain object) which has an abort() method. Calling that abort method ends the HTTP request, but it will not end the work Elasticsearch is doing.

Specifically for your example:

setTimeout(function () {
    if (!dataFromElasticsearch) {
        request.abort();
    }
}, 5000)
timkang
  • 26
  • 2
  • This solution doesn't actually work with my specific example, because the example I gave was using elasticjs 1.1, which uses the `doSearch()` method, but elasticjs 1.2 doesn't use `doSearch()`. Anyone who stumbles on this answer should note that `abort()` is not an **elastic.js** method, but rather **elasticsearch.js**. Rather than building the request like the example in my question, it would be better to use that [latest elastic.js examples (v 1.2 as of right now)](https://github.com/fullscale/elastic.js) and use `abort()` as @timkang suggests with elasticsearch.js. – chevin99 Jan 20 '15 at 14:28