Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

One _request function since they are all the same (DRY)! Initial test…

…s for add and query.

Signed-off-by: Nick Campbell <nicholas.j.campbell@gmail.com>
  • Loading branch information...
commit 94d9e4a6bdaba571b8e5b05a5e8097c9c1e5cbef 1 parent 3fbc0d4
@ncb000gt authored
Showing with 67 additions and 40 deletions.
  1. +31 −40 lib/elasticsearch.js
  2. +36 −0 tests/test-client.js
View
71 lib/elasticsearch.js
@@ -31,6 +31,21 @@ function ElasticSearch(args) {
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) {
var def = {
host: this.config.host,
@@ -38,49 +53,20 @@ ElasticSearch.prototype.add = function(opts, doc, cb) {
method: ((opts.id)?'PUT':'POST'),
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.query = function(opts, query, cb) {
+ if (opts && query && !cb && typeof (query) == 'function') {
+ cb = query;
+ query = opts;
+ opts = {};
+ }
var def = {
host: this.config.host,
port: this.config.port,
@@ -89,7 +75,7 @@ ElasticSearch.prototype.query = function(opts, query, cb) {
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) {
@@ -100,10 +86,14 @@ ElasticSearch.prototype.queryAll = function(opts, query, cb) {
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 chunks = [],
length = 0;
@@ -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();
}
-
View
36 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');
+ });
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.