1

Here is something that's been driving me crazy since an hour now. I'm working on a side project which involves accessing ElasticSearch with Javascript. As a part of the tests, I wanted to create an index. Here is a very simple snippet that, in my mind, should do this, and print the messages returned from the ElasticSearch server:

var es = require('elasticsearch');

var es_client = new es.Client({host: "localhost:9200"});
var breaker = Math.floor((Math.random() * 100) + 1);

var create_promise = es_client.indices.create({index: "test-index-" + breaker});
create_promise.then(function(x) {
    console.log(x);
}, function(err) { console.log(err);});

What happens when I go to a directory, run npm install elasticsearch, and then run this code with NodeJS, is that the request is made, but the promise does not return due to some reason. I would expect this code to run to the end, and finish once the response from ES server comes back. Instead, the process just hangs. Any ideas why?

I know that an index can be created just by adding a document to it, but this weird behavior just bugged me, and I couldn't figure out the reason or the sense behind it.

Ulas Turkmen
  • 475
  • 4
  • 16
  • Possible duplicate of [How do I convert an existing callback API to promises](http://stackoverflow.com/questions/22519784/how-do-i-convert-an-existing-callback-api-to-promises) – Benjamin Gruenbaum Jan 09 '15 at 14:01
  • After looking at the source code I'm almost certain this is a bug - please open an issue in the tracker on github – Benjamin Gruenbaum Jan 09 '15 at 14:06

1 Answers1

1

By default the client keeps persistent connections to elasticsearch so that subsequent requests to the same node are much faster. This has the side effect of preventing node from closing normally until client.close() is called. You could either add this to your callback, or disable keepAlive connections by adding keepAlive: false to your client config.

Spencer Alger
  • 918
  • 2
  • 9
  • 22