Search code examples
node.jselasticsearchamazon-elasticsearch

Elastic Search update doesn't return any result with node.js


I have an elastic search connection in my code as below.

const config = require('../../config/index');
const logger = require('winston');

var elasticsearch = require('elasticsearch');
var elasticClient;

var state = {
  connection: null,
}

exports.connect = function (done) {
try {
    logger.info("elasticsearch 000");
    if (state.connection) return done()
    elasticClient = new elasticsearch.Client({
      host: config.elasticSearch.url,
      log: 'info'
    });

    state.connection = elasticClient;
    logger.info("elasticsearch connected on url : ", config.elasticSearch.url);
    done()

  } catch (e) {
    logger.info("elasticsearch connect exception ", e)
  }

}

exports.get = function () {
  return state.connection
}

I'm using this connection in this way...

function Update(_id, data, callback) {    
 elasticClient.get().update({
    index: indexName,
    type: tablename,
    id: _id,
    retry_on_conflict: 5,
    body: {
        doc: data,
        doc_as_upsert: true
    }
 }, (err, results) => {
    if (err) {
        console.log("= = = = [elasticClient Update err]= = = = =", err);
    }
    return callback(err, results)
})

}

Issue: When I call this update function, It's not returning any data... And I got this error...

error :  StatusCodeError: Request Timeout after 30000ms
/node_modules/elasticsearch/src/lib/transport.js:397:9

Note: For Elastic Search connection, I'm using Amazon Elastic Search service and I'm passing its VPC endpoint.

Node version: 12.14.1

Elasticsearch version 6.3

Package.json : "elasticsearch": "16.6.0"


Solution

  • When you say "local terminal", do you mean a terminal in your laptop? If so, notice ElasticSearch should not be open to the public Internet, as exposing your database like that is prone to data leaks (a single misconfiguration and you're done).

    Anyways, if from some computer A you expect to be able to connect to ElasticSearch and you are not being able, then the first things I'd check are the security groups and in which VPC/subnet the database is located. If the DB is in a private subnet (as it should be) then it's ok not to be able to access it from outside.

    Think of how your security model should work. Usually, your ElasticSearch would be in a private subnet, and you would only connect to it from instances that are connected to that subnet and that are allowed in the rules of your security groups.