New

The executive guide to generative AI

Read more

Say hello to Elasticsearch

edit
IMPORTANT: This documentation is no longer updated. Refer to Elastic's version policy and the latest documentation.

Say hello to Elasticsearch

edit

Almost all of the methods on the client accept two arguments:

  • params - an optional object/hash of parameters More info here.
  • callback - an optional function that will be called with the final result of the method. When omitted, a promise is returned. More info here.

Ping the cluster

edit

Send a HEAD request to "/" and allow up to 30 seconds for it to complete.

client.ping({
  requestTimeout: 30000,
}, function (error) {
  if (error) {
    console.error('elasticsearch cluster is down!');
  } else {
    console.log('All is well');
  }
});

Use Promises

edit

Skip the callback to get a promise back.

client.search({
  q: 'pants'
}).then(function (body) {
  var hits = body.hits.hits;
}, function (error) {
  console.trace(error.message);
});

Allow 404 responses

edit

Prevent 404 responses from being considered errors by telling the client to ignore them.

client.indices.delete({
  index: 'test_index',
  ignore: [404]
}).then(function (body) {
  // since we told the client to ignore 404 errors, the
  // promise is resolved even if the index does not exist
  console.log('index was deleted or never existed');
}, function (error) {
  // oh no!
});
Was this helpful?
Feedback