diff --git a/lib/elasticsearchclient/calls/core.js b/lib/elasticsearchclient/calls/core.js index 9eea2ba..2ab0b0b 100644 --- a/lib/elasticsearchclient/calls/core.js +++ b/lib/elasticsearchclient/calls/core.js @@ -1,6 +1,6 @@ //Core var querystring = require('querystring'), -ElasticSearchClient = require('../elasticSearchClient') +ElasticSearchClient = require('../elasticSearchClient'); ElasticSearchClient.prototype.index = function(indexName, typeName, document, id, options, cb) { //Pull the callback and set it false to not clobber id. @@ -11,7 +11,7 @@ ElasticSearchClient.prototype.index = function(indexName, typeName, document, id var path = '/' + indexName + '/' + typeName; var qs = ''; - var method = 'POST' + var method = 'POST'; @@ -21,21 +21,21 @@ ElasticSearchClient.prototype.index = function(indexName, typeName, document, id } if (id) { - path += "/" + id - method = 'PUT' + path += "/" + id; + method = 'PUT'; } if (options) { - qs = querystring.stringify(options) + qs = querystring.stringify(options); } if (qs.length > 0) { path += "?" + qs; } return this.createCall({data:JSON.stringify(document),path:path,method: method}, this.clientOptions, cb); -} +}; ElasticSearchClient.prototype.deleteDocument = function(indexName, typeName, documentId, options, cb) { @@ -48,7 +48,7 @@ ElasticSearchClient.prototype.deleteDocument = function(indexName, typeName, doc var path = '/' + indexName + '/' + typeName + '/' + documentId; var qs = ''; if (options) { - qs = querystring.stringify(options) + qs = querystring.stringify(options); } if (qs.length > 0) { @@ -56,7 +56,7 @@ ElasticSearchClient.prototype.deleteDocument = function(indexName, typeName, doc } return this.createCall({path:path, method:'DELETE'}, this.clientOptions, cb); -} +}; ElasticSearchClient.prototype.get = function(indexName, typeName, documentId, options, cb) { //Pull the callback and set it false to not clobber id. @@ -68,14 +68,14 @@ ElasticSearchClient.prototype.get = function(indexName, typeName, documentId, op var path = '/' + indexName + '/' + typeName + '/' + documentId; var qs = ''; if (options) { - qs = querystring.stringify(options) + qs = querystring.stringify(options); } if (qs.length > 0) { path += "?" + qs; } return this.createCall({path:path, method:'GET'}, this.clientOptions, cb); -} +}; // indexName, typeName, queryObj, options // TODO: Not a very robust piece of code - need to be refactored to be more tolerant to input @@ -86,40 +86,45 @@ ElasticSearchClient.prototype.search = function(indexName, typeName, queryObj, o arguments[arguments.length-1]=false; } - var objArgs=[] - var path='' - - // Assumes quite a lot about the input + var objArgs=[]; + var path=''; + + // Assumes quite a lot about the input for(var i=0;i 0) { path += "?" + qs; } - var commandBuffer='' + var commandBuffer=''; for(var i =0; i 0) { path += "?" + qs; } return this.createCall({path: path, method: 'PUT',data: JSON.stringify(queryObj)}, this.clientOptions, cb); -} +}; ElasticSearchClient.prototype.percolate = function(indexName, typeName, doc, options, cb) { //Pull the callback and set it false to not clobber id. @@ -208,16 +216,16 @@ ElasticSearchClient.prototype.percolate = function(indexName, typeName, doc, opt arguments[arguments.length-1]=false; } - var path = '/' + indexName + '/' + typeName+'/_percolate' + var path = '/' + indexName + '/' + typeName+'/_percolate'; var qs = ''; if (options) { - qs = querystring.stringify(options) + qs = querystring.stringify(options); } if (qs.length > 0) { path += "?" + qs; } return this.createCall({path: path, method: 'GET',data: JSON.stringify(doc)}, this.clientOptions, cb); -} +}; ElasticSearchClient.prototype.count = function(indexName, typeName, query, options, cb) { //Pull the callback and set it false to not clobber id. @@ -227,20 +235,23 @@ ElasticSearchClient.prototype.count = function(indexName, typeName, query, optio } var path = '/' + indexName + '/' + typeName + '/_count'; - + switch(typeof query){ - case 'string': - var qs = '?q=' + querystring.stringify(query); - if (options) { - qs +='&'+ querystring.stringify(options) - } - return this.createCall({path: path+qs, method: 'GET'}, this.clientOptions, cb); + case 'string': + var qs = '?q=' + querystring.stringify(query); + if (options) { + var queryfied = querystring.stringify(options); + if (queryfied.length) { + qs +='&'+ queryfied; + } + } + return this.createCall({path: path+qs, method: 'GET'}, this.clientOptions, cb); case 'object': - return this.createCall({path : path, method : 'POST', data : JSON.stringify(query)}, this.clientOptions, cb); + return this.createCall({path : path, method : 'POST', data : JSON.stringify(query)}, this.clientOptions, cb); } - throw "unsupported query type: " + typeof(query); -} + throw "unsupported query type: " + typeof(query); +}; ElasticSearchClient.prototype.bulk = function(commandArray, options, cb) { @@ -250,7 +261,7 @@ ElasticSearchClient.prototype.bulk = function(commandArray, options, cb) { options = null; } - var path = '/_bulk' + var path = '/_bulk'; var qs = ''; if (options) { if (options._index) { @@ -262,19 +273,19 @@ ElasticSearchClient.prototype.bulk = function(commandArray, options, cb) { } delete options._index; } - qs = querystring.stringify(options) + qs = querystring.stringify(options); if (qs) { path += "?" + qs; } } - var commandBuffer='' + var commandBuffer=''; for(var i =0; i 0) { path += "?" + qs; } return this.createCall({path: path, method: 'DELETE',data: JSON.stringify(queryObj)}, this.clientOptions, cb); -} +}; ElasticSearchClient.prototype.moreLikeThis = function(indexName, typeName, documentId, options, cb) { //Pull the callback and set it false to not clobber id. @@ -305,13 +316,13 @@ ElasticSearchClient.prototype.moreLikeThis = function(indexName, typeName, docum var path = '/' + indexName + '/' + typeName + '/'+documentId+'/_mlt'; var qs = ''; if (options) { - qs = querystring.stringify(options) + qs = querystring.stringify(options); } if (qs.length > 0) { path += "?" + qs; } return this.createCall({path: path, method: 'GET'}, this.clientOptions, cb); -} +}; ElasticSearchClient.prototype.update = function(indexName, typeName, documentId, document, options, cb) { //Pull the callback and set it false to not clobber id. @@ -325,10 +336,10 @@ ElasticSearchClient.prototype.update = function(indexName, typeName, documentId, var path = '/' + indexName + '/' + typeName + '/'+documentId+'/_update'; var qs = ''; if (options) { - qs = querystring.stringify(options) + qs = querystring.stringify(options); } if (qs.length > 0) { path += "?" + qs; } return this.createCall({data: JSON.stringify(document), path: path, method: 'POST'}, this.clientOptions, cb); -} \ No newline at end of file +}; \ No newline at end of file diff --git a/lib/elasticsearchclient/calls/elasticSearchCall.js b/lib/elasticsearchclient/calls/elasticSearchCall.js index b77bdd4..ba80698 100644 --- a/lib/elasticsearchclient/calls/elasticSearchCall.js +++ b/lib/elasticsearchclient/calls/elasticSearchCall.js @@ -6,7 +6,7 @@ var events = require('events'), module.exports = ElasticSearchCall; function ElasticSearchCall(params, options, cb) { - var self = this + var self = this; self.host = options.host || 'localhost'; self.port = options.port || 80; self.secure = options.secure || false; @@ -23,14 +23,14 @@ function ElasticSearchCall(params, options, cb) { util.inherits(ElasticSearchCall, events.EventEmitter); ElasticSearchCall.prototype.exec = function (cb) { - var self = this + var self = this; var reqOptions = { path:this.path + this.params.path, method:this.params.method || this.defaultMethod, host:this.host, port:this.port, agent: this.agent - } + }; if (typeof cb == 'function') { self.callback = cb; } @@ -51,8 +51,8 @@ ElasticSearchCall.prototype.exec = function (cb) { } request.on('error', function (error) { - self.emit("error", error) - }) + self.emit("error", error); + }); request.on('response', function (response) { var body = ""; @@ -64,20 +64,20 @@ ElasticSearchCall.prototype.exec = function (cb) { self.callback(undefined, body); } else { self.emit("data", body); - self.emit("done", 0) + self.emit("done", 0); } }); response.on('error', function (error) { if (typeof self.callback == 'function') { self.callback(error); } else { - self.emit("error", error) + self.emit("error", error); } - }) + }); }); if (this.auth) { - request.setHeader("Authorization", "Basic " + new Buffer(this.auth.username + ":" + this.auth.password).toString('base64')) + this.auth.setHeaders(this, request, reqOptions); } if (this.params.data) { @@ -88,9 +88,13 @@ ElasticSearchCall.prototype.exec = function (cb) { request.setHeader('Content-Length', Buffer.byteLength(this.params.data, 'utf8')); request.end(this.params.data); } else { + if (reqOptions.method === 'POST' || reqOptions.method === 'PUT' || reqOptions.method === 'DELETE') { + //olderversions of nginx really want this. + request.setHeader('Content-Length', 0); + } request.end(''); } -} +}; /** * Wrap the default data event @@ -98,7 +102,7 @@ ElasticSearchCall.prototype.exec = function (cb) { */ ElasticSearchCall.prototype.data = function (callback) { this.on('data', callback); -} +}; /** * wrap the default done event @@ -106,7 +110,7 @@ ElasticSearchCall.prototype.data = function (callback) { */ ElasticSearchCall.prototype.done = function (callback) { this.on('done', callback); -} +}; /** * wrap the default error event @@ -114,4 +118,4 @@ ElasticSearchCall.prototype.done = function (callback) { */ ElasticSearchCall.prototype.error = function (callback) { this.on('error', callback); -} +}; diff --git a/lib/elasticsearchclient/elasticSearchClient.js b/lib/elasticsearchclient/elasticSearchClient.js index 80966f9..9758dcd 100644 --- a/lib/elasticsearchclient/elasticSearchClient.js +++ b/lib/elasticsearchclient/elasticSearchClient.js @@ -11,8 +11,17 @@ Twitter = require('./calls/twitter') River = require('./calls/river') ElasticSearchCall = require('./calls/elasticSearchCall.js'); +/** + * options.auth.setHeaders optional function to set the authorization header(s). + * If it does not exist but options.auth exists, default to Basic Authentication. + */ function ElasticSearchClient(options) { this.clientOptions = options || {} + if (typeof options.auth === 'object' && options.auth !== null && typeof options.auth.setHeaders !== 'function') { + options.auth.setHeaders = function(call, request, reqOptions) { + request.setHeader("Authorization", "Basic " + new Buffer(options.auth.username + ":" + options.auth.password).toString('base64')); + }; + } } ElasticSearchClient.prototype.createCall = function(params, options, cb) { @@ -36,7 +45,7 @@ var inetRE = /inet\[\/(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\:(\d{1,5})\]/ /** * Discover nodes of the elasticsearch cluster. - * Call this function to automatically add all nodes of the + * Call this function to automatically add all nodes of the * cluster in the hosts list. */ ElasticSearchClient.prototype.discoverNodes = function(){ @@ -51,7 +60,7 @@ ElasticSearchClient.prototype.discoverNodes = function(){ if(!"http_address" in node){ continue; } - + address = inetRE.exec(node.http_address); if(!address){ continue;