Permalink
Browse files

*NOT WORKING* Refactoring some modules, new index object. This is a s…

…tart towards customizing index settings through the api.

Signed-off-by: Nick Campbell <nicholas.j.campbell@gmail.com>
  • Loading branch information...
1 parent abfe004 commit ec4cd90801efcb02ba15ae7b2621a1c7d650d622 @ncb000gt committed Mar 4, 2011
Showing with 121 additions and 72 deletions.
  1. +28 −72 lib/elasticsearch.js
  2. +25 −0 lib/index.js
  3. +68 −0 lib/utils.js
View
@@ -1,32 +1,23 @@
var http = require('http_compat'),
+ utils = require('./utils'),
+ Query = require('./query'),
+ Index = require('./index'),
sys = require('sys');
-function mapConfig(origin, mapper, cb) {
- var new_config = {};
- for (var p in origin) {
- new_config[p] = origin[p];
- }
- for (var p in mapper) {
- new_config[p] = mapper[p];
- }
- if (cb) {
- cb(new_config);
- } else {
- return new_config;
- }
-}
-
function ElasticSearch(args) {
var opts = args[0],
cb = ((args.length>1)?args[1]:undefined);
- var def = {
- host: 'localhost',
- port: 9200
- };
if (!(opts.index)) {
throw new Error('Must specify an index for ES.');
+ } else if (!(opts.index instanceof Index)) {
+ var index;
+ if (opts.index instanceof String) {
+ index = new Index({name: opts.index});
+ } else {
+ index = new Index(opts.index);
+ }
}
- this.config = mapConfig(def, opts);
+ this.config = utils.mapConfig(utils.default_config, opts);
}
module.exports = ElasticSearch;
@@ -43,7 +34,7 @@ ElasticSearch.prototype.status = function(opts, cb) {
path: '/'+this.config.index+'/_status'
};
- this._request(mapConfig(def, opts), cb);
+ utils.request(utils.mapConfig(def, opts), cb);
}
ElasticSearch.prototype.add = function(opts, doc, cb) {
@@ -56,14 +47,26 @@ ElasticSearch.prototype.add = function(opts, doc, cb) {
host: this.config.host,
port: this.config.port,
method: ((opts.id)?'PUT':'POST'),
- path: '/' + this.config.index + '/' + opts.type + '/' + (opts.id || '')
+ path: '/' + this.config.index + '/' + (opts.type || 'doc') + '/' + (opts.id || '')
};
- this._request(mapConfig(def, opts), doc, cb);
+ utils.request(utils.mapConfig(def, opts), doc, cb);
}
ElasticSearch.prototype.delete = function(opts, cb) {
+ if (opts && !cb && typeof (opts) == 'function') {
+ cb = opts;
+ opts = {};
+ }
+ var def = {
+ host: this.config.host,
+ port: this.config.port,
+ method: 'DELETE',
+ path: '/' + this.config.index + '/' + ((opts.type)?(opts.type+(opts.id)?'/'+opts.id:''):'')
+ };
+
+ utils.request(utils.mapConfig(def, opts), undefined, cb);
}
ElasticSearch.prototype.query = function(opts, query, cb) {
@@ -76,11 +79,10 @@ ElasticSearch.prototype.query = function(opts, query, cb) {
host: this.config.host,
port: this.config.port,
method: 'POST',
- noend: true,
path: '/' + this.config.index + '/_search'
};
- this._request(mapConfig(def, opts), query, cb);
+ utils.request(utils.mapConfig(def, opts), query, cb);
}
ElasticSearch.prototype.queryAll = function(opts, query, cb) {
@@ -91,51 +93,5 @@ ElasticSearch.prototype.queryAll = function(opts, query, cb) {
path: '/_search'
};
- this._request(mapConfig(def, 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 statusCode = res.statusCode;
- 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 += chunk.length;
- });
-
- //TODO: figure out a better way to do this, not sure it's possible with inconsistent messages from ES...
- var res_json = {};
- try {
- res_json = JSON.parse(buf.toString());
- } catch(e) {
- res_json = {status: statusCode, error: buf.toString()};
- }
- if (cb) {
- //Success based status codes.
- if (!((""+statusCode).match(/2\d\d/))) {
- cb(res_json);
- } else {
- cb(null, res_json);
- }
- }
- });
- });
-
- if (data) {
- req.write(JSON.stringify(data));
- }
- req.end();
+ utils.request(utils.mapConfig(def, opts), query, cb);
}
View
@@ -0,0 +1,25 @@
+var utils = require('./utils');
+
+function Index(opts) {
+ if (!(opts.name)) {
+ throw new Error('Must specify an index name.');
+ }
+
+ this.name = opts.name;
+ this.config =
+ this.idx_config = opts.config;
+}
+
+module.exports = Index;
+
+Index.prototype.create = function(opts, cb) {
+ if (opts && typeof(opts) == 'function') {
+ cb = opts;
+ opts = {};
+ }
+ var config = utils.mapConfig(this.config, opts);
+ var req_opts = {
+ method: ((config.mappings)?'POST':'PUT'),
+ };
+ utils.request(req_opts, config, cb);
+}
View
@@ -0,0 +1,68 @@
+module.exports.default_config = {
+ host: 'localhost',
+ port: 9200
+};
+module.exports.request = function(opts, data, cb) {
+ if (data && !cb && typeof(data) == 'function') {
+ cb = data;
+ data = undefined;
+ }
+ var req = http.request(opts, function(res) {
+ var statusCode = res.statusCode;
+ var chunks = [],
+ length = 0;
+ res.on('data', function(chunk) {
+ chunks.push(chunk);
+ length += chunk.length; //can't reliably use headers
+ });
+ if (cb) {
+ 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 += chunk.length;
+ });
+
+ //TODO: figure out a better way to do this, not sure it's possible with inconsistent messages from ES...
+ var res_json = {};
+ try {
+ res_json = JSON.parse(buf.toString());
+ } catch(e) {
+ res_json = {status: statusCode, error: buf.toString()};
+ }
+ if (cb) {
+ //Success based status codes.
+ if (!((""+statusCode).match(/2\d\d/))) {
+ cb(res_json);
+ } else {
+ cb(null, res_json);
+ }
+ }
+ });
+ });
+
+ if (data) {
+ req.write(JSON.stringify(data));
+ }
+ if (!(opts.noend)) {
+ req.end();
+ }
+}
+
+module.exports.mapConfig = function(origin, mapper, cb) {
+ var new_config = {};
+ for (var p in origin) {
+ new_config[p] = origin[p];
+ }
+ for (var p in mapper) {
+ new_config[p] = mapper[p];
+ }
+ if (cb) {
+ cb(new_config);
+ } else {
+ return new_config;
+ }
+}

0 comments on commit ec4cd90

Please sign in to comment.