Files
elasticsearch-js/docs/examples.md
2013-11-22 16:48:30 -07:00

839 B

Examples

create the client

var es = new elasticsearch.Client({
  hosts: [
    'localhost:9200'
  ],
  log: 'trace',
  sniffOnStart: true
});

call an endpoint

es.cluster.nodeInfo({
  clear: true,
  jvm: true,
  os: ture
}, function (err, resp, status) {
    // do your thing
})

skip the callback to get a promise back

es.search({
  q: 'pants'
}).then(function (resp) {
  // use resp.body and resp.status
}, function (err) {
  // freak out!
})

abort a request

var req = es.search({
  q: 'robots'
}, function (err, body, status) {
  clearTimeout(timeout);
  // do something
});
var timeout = setTimeout(function () {
  req.abort();
}, 200);

or just use the timeout param

es.search({
  q: '*',
  timeout: 200
}).then(function (resp) {
  // Iterate all the hits
})