Skip to content

Commit

Permalink
One _request function since they are all the same (DRY)! Initial test…
Browse files Browse the repository at this point in the history
…s for add and query.

Signed-off-by: Nick Campbell <nicholas.j.campbell@gmail.com>
  • Loading branch information
ncb000gt committed Mar 2, 2011
1 parent 3fbc0d4 commit 94d9e4a
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 40 deletions.
71 changes: 31 additions & 40 deletions lib/elasticsearch.js
Expand Up @@ -31,56 +31,42 @@ function ElasticSearch(args) {


module.exports = ElasticSearch; module.exports = ElasticSearch;


ElasticSearch.prototype.status = function(opts, cb) {
if (opts && !cb) {
cb = opts;
opts = {};
}
var def = {
host: this.config.host,
port: this.config.port,
method: 'GET',
path: '/'+this.config.index+'/_status'
};

this._request(mapConfig(def, opts), cb);
}

ElasticSearch.prototype.add = function(opts, doc, cb) { ElasticSearch.prototype.add = function(opts, doc, cb) {
var def = { var def = {
host: this.config.host, host: this.config.host,
port: this.config.port, port: this.config.port,
method: ((opts.id)?'PUT':'POST'), method: ((opts.id)?'PUT':'POST'),
path: '/' + this.config.index + '/' + opts.type + '/' + (opts.id || '') path: '/' + this.config.index + '/' + opts.type + '/' + (opts.id || '')
}; };
var query = {};
if (opts.query) {
query = {query: opts.query};
delete opts.query;
}


this._add(mapConfig(def, opts), query, cb); this._request(mapConfig(def, opts), doc, cb);
} }


ElasticSearch.prototype._add = function(opts, doc, cb) {
var req = http.request(opts, function(res) {
var chunks = [],
length = 0;
res.on('data', function(chunk) {
chunks.push(chunk);
length += chunk.length; //can't reliably use headers
});
res.on('error', cb);
res.on('end', function() {
var buf = new Buffer(length),
total = 0;
chunks.forEach(function chunk_cb(chunk) {
chunk.copy(buf, total, 0);
total += buf.length;
});

if (cb) {
cb(null, buf);
}
});
});

req.write(JSON.stringify(doc));
req.end();
}


ElasticSearch.prototype.delete = function(opts, cb) { ElasticSearch.prototype.delete = function(opts, cb) {
} }


ElasticSearch.prototype._delete = function(opts, cb) {
}

ElasticSearch.prototype.query = function(opts, query, cb) { ElasticSearch.prototype.query = function(opts, query, cb) {
if (opts && query && !cb && typeof (query) == 'function') {
cb = query;
query = opts;
opts = {};
}
var def = { var def = {
host: this.config.host, host: this.config.host,
port: this.config.port, port: this.config.port,
Expand All @@ -89,7 +75,7 @@ ElasticSearch.prototype.query = function(opts, query, cb) {
path: '/' + this.config.index + '/_search' path: '/' + this.config.index + '/_search'
}; };


this._query(mapConfig(def, opts), query, cb); this._request(mapConfig(def, opts), query, cb);
} }


ElasticSearch.prototype.queryAll = function(opts, query, cb) { ElasticSearch.prototype.queryAll = function(opts, query, cb) {
Expand All @@ -100,10 +86,14 @@ ElasticSearch.prototype.queryAll = function(opts, query, cb) {
path: '/_search' path: '/_search'
}; };


this._query(mapConfig(def, opts), query, cb); this._request(mapConfig(def, opts), query, cb);
} }


ElasticSearch.prototype._query = function(opts, query, cb) { ElasticSearch.prototype._request = function(opts, data, cb) {
if (data && !cb) {
cb = data;
data = undefined;
}
var req = http.request(opts, function(res) { var req = http.request(opts, function(res) {
var chunks = [], var chunks = [],
length = 0; length = 0;
Expand All @@ -126,7 +116,8 @@ ElasticSearch.prototype._query = function(opts, query, cb) {
}); });
}); });


req.write(JSON.stringify(query)); if (data) {
req.write(JSON.stringify(data));
}
req.end(); req.end();
} }

36 changes: 36 additions & 0 deletions tests/test-client.js
@@ -0,0 +1,36 @@
var lib = require('../index'),
assert = require('assert');

module.exports = {
'test client connection': function(beforeExit) {
var n = 0;
var client = lib.createClient({index:'_all'});
client.status({}, function(err, status) {
status = JSON.parse(status.toString());
assert.ok(status.ok);
n++;
});
beforeExit(function() {
assert.equal(1, n, 'callbacks');
});
},
'add and find document': function(beforeExit) {
var n = 0;
var doc = {field1: 'test field', field2: 'second test field'};
var client = lib.createClient({index:'test'});
client.add({type:'info', id:'1'}, doc, function(err, result) {
result = JSON.parse(result.toString());
assert.ok(result.ok);
n++;
client.query({query: {field: {field1: 'test'}}}, function(err, result) {
result = JSON.parse(result.toString());
assert.equal(result.hits.total, 1);
assert.deepEqual(result.hits.hits[0]._source, doc);
n++;
});
});
beforeExit(function() {
assert.equal(2, n, 'callbacks');
});
}
}

0 comments on commit 94d9e4a

Please sign in to comment.