5
votes

I'm trying to implement an elasticsearch client in NodeJS on a Cloud9 workspace and I'm just trying to get it running. My app runs on port 5678 and my MongoDB runs on 27017. I have tried searching for other answers, but I haven't really found anything particularly useful. This is the error message that I receive when trying to connect to localhost:9200.

    Elasticsearch ERROR: 2015-06-26T04:24:19Z
  Error: Request error, retrying -- connect ECONNREFUSED
      at Log.error (/home/ubuntu/workspace/node_modules/elasticsearch/src/lib/log.js:213:60)
      at checkRespForFailure (/home/ubuntu/workspace/node_modules/elasticsearch/src/lib/transport.js:192:18)
      at HttpConnector.<anonymous> (/home/ubuntu/workspace/node_modules/elasticsearch/src/lib/connectors/http.js:153:7)
      at ClientRequest.wrapper (/home/ubuntu/workspace/node_modules/elasticsearch/node_modules/lodash/index.js:3128:19)
      at ClientRequest.emit (events.js:95:17)
      at Socket.socketErrorListener (http.js:1552:9)
      at Socket.emit (events.js:95:17)
      at net.js:441:14
      at process._tickCallback (node.js:442:13)

Elasticsearch TRACE: 2015-06-26T04:24:19Z
  -> HEAD http://localhost:9200/

  <- 0


Elasticsearch WARNING: 2015-06-26T04:24:19Z
  Unable to revive connection: http://localhost:9200/

Elasticsearch WARNING: 2015-06-26T04:24:19Z
  No living connections

Trace: elasticsearch cluster is down!
    at Server (/home/ubuntu/workspace/app.js:32:13)
    at respond (/home/ubuntu/workspace/node_modules/elasticsearch/src/lib/transport.js:251:9)
    at sendReqWithConnection (/home/ubuntu/workspace/node_modules/elasticsearch/src/lib/transport.js:171:7)
    at next (/home/ubuntu/workspace/node_modules/elasticsearch/src/lib/connection_pool.js:213:7)
    at process._tickCallback (node.js:442:13)

.

My code for the elastic search client is very simple

    var client = new elasticsearch.Client({
  hosts: 'localhost:9200',
  log: 'trace'
});

client.ping({
  // ping usually has a 3000ms timeout
  requestTimeout: 30000,

  // undocumented params are appended to the query string
  hello: "elasticsearch!"
}, function (error) {
  if (error) {
    console.trace('elasticsearch cluster is down!');
  } else {
    console.log('All is well');
  }
});

If I try to connect to localhost:5678, I don't get an error refused, but the elastic cluster is still down? Any suggestions would be helpful, thanks :)

3
Is your elasticsearch running? This error pops up if ES isn't running or if it isn't running on port 9200Daniel Hoffmann-Mitscherling
Hello, thanks a lot for your response! It seems to be running, but it cannot establish a proper connection or something else is wrong that I don't know about. This is what i'm getting periodically, which is the ping requests I assume. Elasticsearch TRACE: 2015-06-26T19:57:21Z -> HEAD localhost:9200 <- 0 .Allen
I also get this at the very beginning when i run npm start Adding connection to localhost:9200 Elasticsearch DEBUG: 2015-06-26T20:10:23Z starting request { method: 'HEAD', requestTimeout: 30000, castExists: true, path: '/', query: { keepAlive: false, hello: 'elasticsearch!' } }Allen

3 Answers

1
votes
//Client.js
    const es = require('elasticsearch');
    const esClient = new es.Client({
        host: {
            protocol: 'http',
            host: 'localhost',
            port: 9200

        },
        log: 'trace'
    });

    module.exports = esClient;

//ping.js
const esClient = require('./client');

esClient.ping({
// ping usually has a 3000ms timeout
    requestTimeout: 3000
}, function (error) {
    if (error) {
        console.trace('elasticsearch cluster is down!');
    } else {
        console.log('All is well');
    }
});
0
votes

Hey to anyone who wants to know what I did. I basically just downloaded elasticsearch onto cloud9 and ran it. Then I pinged the port accordingly and it worked. Looks like a noobie mistake on my part :P

0
votes

In my case, stock elasticsearch with everything in default and using the JS client results in this error.

(short term) fix: http.cors.enabled and https.cors.allow-origin settings