node-esc
node-esc is a small client that helps you interact with ElasticSearch.
Installation
$ npm install node-esc
Usage
var ESIndex = require('node-esc');
var esi = new ESIndex('http://localhost:9200/twitter');
esi.addType('tweet');
esi.tweet.getById(tweetId, function (err, tweet) {
console.log(tweet);
});
API
Each type added to ESIndex instance has following methods:
###query(urlParam, respBody, callback)
call index with urlParam added to url and respBody passed as responseBody, and pass ElasicSearch response to callback.
With this method you are able to do any operation on type.
esi.tweet.query('_search', {query: {term: {user: 'foobar'}}}, function (err, resp) {
console.log(resp);
});
###index(id, data, callback)
add (or update) document to index (http://www.elasticsearch.org/guide/reference/api/index_.html)
When first parameter (id) is absent then _id from data will be used
esi.tweet.index(1, {user: 'foo', message: 'lorem ipsum'}, function (err, resp) {
console.log(resp);
});
###delete(id/doc, callback)
delete document (http://www.elasticsearch.org/guide/reference/api/delete.html) with passed id (when first param is object then use _id as document identifier)
esi.tweet.delete(1, function (err, resp) {
console.log(resp);
});
###update(id, configObject, callback)
update document with passed id. Send configObject as response body (http://www.elasticsearch.org/guide/reference/api/update.html)
esi.tweet.update(1, {doc: {message: 'new message content'}}, function (err, resp) {
console.log(resp);
});
###getById(id, callback)
get document by id (http://www.elasticsearch.org/guide/reference/api/get.html) (number or string) and pass response from ElasticSearch to callback.
esi.tweet.getById(1, function (err, doc) {
console.log(doc);
});
###mGetById(ids, callback)
get multiple documents by array of ids () and pass response from ElasticSearch to callback.
esi.tweet.mGetById([1, 2], function (err, docs) {
console.log(docs);
});
###search(searchConfig, callback)
execute search in type, with searchConfig passed as request body (http://www.elasticsearch.org/guide/reference/api/search/request-body.html) and pass ElasticSearch reponse to callback.
esi.tweet.search({query: {term: {message: 'foo'}}}, function (err, results) {
console.log(results);
});