IMPORTANT: elasticsearch.js has been replaced by the new Elasticsearch JavaScript client. We strongly advise you to migrate to the new client. To learn more, see the migration guide.
Say hello to Elasticsearch
editSay hello to Elasticsearch
editAlmost 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
editSend 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
editSkip 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
editPrevent 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! });