From ade5abfa16527e16c77d205663d53df4d130346f Mon Sep 17 00:00:00 2001 From: Hugues Malphettes Date: Mon, 8 Jul 2013 04:40:54 +0800 Subject: [PATCH 1/6] nginx requires a Content-Length=0 header for POST/PUT/DELETE --- lib/elasticsearchclient/calls/elasticSearchCall.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/elasticsearchclient/calls/elasticSearchCall.js b/lib/elasticsearchclient/calls/elasticSearchCall.js index b77bdd4..debcd12 100644 --- a/lib/elasticsearchclient/calls/elasticSearchCall.js +++ b/lib/elasticsearchclient/calls/elasticSearchCall.js @@ -88,6 +88,10 @@ 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(''); } } From 41fa5ec968cb938f860f53497f5e8667358a3e19 Mon Sep 17 00:00:00 2001 From: Hugues Malphettes Date: Fri, 13 Jun 2014 18:14:43 +0800 Subject: [PATCH 2/6] options.auth.setHeaders to use a custom function to set the authentication headers - support for Hawk --- lib/elasticsearchclient/calls/core.js | 19 +++++++++++-------- .../calls/elasticSearchCall.js | 2 +- .../elasticSearchClient.js | 13 +++++++++++-- 3 files changed, 23 insertions(+), 11 deletions(-) diff --git a/lib/elasticsearchclient/calls/core.js b/lib/elasticsearchclient/calls/core.js index 9eea2ba..67a339e 100644 --- a/lib/elasticsearchclient/calls/core.js +++ b/lib/elasticsearchclient/calls/core.js @@ -88,7 +88,7 @@ ElasticSearchClient.prototype.search = function(indexName, typeName, queryObj, o var objArgs=[] var path='' - + // Assumes quite a lot about the input for(var i=0;i Date: Fri, 13 Jun 2014 12:36:37 +0200 Subject: [PATCH 3/6] Linting --- lib/elasticsearchclient/calls/core.js | 78 +++++++++++++-------------- 1 file changed, 39 insertions(+), 39 deletions(-) diff --git a/lib/elasticsearchclient/calls/core.js b/lib/elasticsearchclient/calls/core.js index 67a339e..51d914b 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,22 +86,22 @@ ElasticSearchClient.prototype.search = function(indexName, typeName, queryObj, o arguments[arguments.length-1]=false; } - var objArgs=[] - var path='' + var objArgs=[]; + var path=''; - // Assumes quite a lot about the input + // 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. @@ -211,16 +211,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. @@ -239,11 +239,11 @@ ElasticSearchClient.prototype.count = function(indexName, typeName, query, optio } 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); -} +}; ElasticSearchClient.prototype.bulk = function(commandArray, options, cb) { @@ -253,7 +253,7 @@ ElasticSearchClient.prototype.bulk = function(commandArray, options, cb) { options = null; } - var path = '/_bulk' + var path = '/_bulk'; var qs = ''; if (options) { if (options._index) { @@ -265,19 +265,19 @@ ElasticSearchClient.prototype.bulk = function(commandArray, options, cb) { } delete options._index; } - qs = querystring.stringify(options) if (qs) { + qs = querystring.stringify(options); 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. @@ -308,13 +308,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. @@ -328,10 +328,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 From 3ff926f97991ddfed15e610f4d5b09c587d0cce6 Mon Sep 17 00:00:00 2001 From: Yves Le Maout Date: Fri, 13 Jun 2014 12:36:53 +0200 Subject: [PATCH 4/6] Do not append "?" if there is no query --- lib/elasticsearchclient/calls/core.js | 48 ++++++++++++++++----------- 1 file changed, 28 insertions(+), 20 deletions(-) diff --git a/lib/elasticsearchclient/calls/core.js b/lib/elasticsearchclient/calls/core.js index 51d914b..fc2d17e 100644 --- a/lib/elasticsearchclient/calls/core.js +++ b/lib/elasticsearchclient/calls/core.js @@ -103,19 +103,24 @@ ElasticSearchClient.prototype.search = function(indexName, typeName, queryObj, o path+='/_search'; - // Is options sent as an object or a string - // Assumes that objArgs[0] is the queryObj - if (options !== undefined){ - switch(typeof options){ - case 'object': - objArgs.push(options); - path += "?" + querystring.stringify(objArgs[1]); - break; - case 'string': - path += "?" + options; - break; - } - } + // Is options sent as an object or a string + // Assumes that objArgs[0] is the queryObj + if (options !== undefined){ + switch(typeof options){ + case 'object': + objArgs.push(options); + var qs = querystring.stringify(objArgs[1]); + if (qs.length) { + path += "?" + qs; + } + break; + case 'string': + if (options.length) { + path += "?" + options; + } + break; + } + } var data = objArgs[0] || {}; return this.createCall({path:path,method: 'POST',data:JSON.stringify(data)}, this.clientOptions, cb); @@ -181,7 +186,7 @@ ElasticSearchClient.prototype.multisearch = function(commandArray, options, cb) var commandBuffer=''; for(var i =0; i Date: Fri, 13 Jun 2014 19:19:01 +0800 Subject: [PATCH 5/6] Avoid double check on empty string --- lib/elasticsearchclient/calls/core.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/elasticsearchclient/calls/core.js b/lib/elasticsearchclient/calls/core.js index fc2d17e..2ab0b0b 100644 --- a/lib/elasticsearchclient/calls/core.js +++ b/lib/elasticsearchclient/calls/core.js @@ -274,7 +274,7 @@ ElasticSearchClient.prototype.bulk = function(commandArray, options, cb) { delete options._index; } qs = querystring.stringify(options); - if (qs && qs.length) { + if (qs) { path += "?" + qs; } } From b7e6013d234d4af1c45656c433882a9f90d3e55a Mon Sep 17 00:00:00 2001 From: Yves Le Maout Date: Fri, 13 Jun 2014 17:20:45 +0200 Subject: [PATCH 6/6] Linting --- .../calls/elasticSearchCall.js | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/lib/elasticsearchclient/calls/elasticSearchCall.js b/lib/elasticsearchclient/calls/elasticSearchCall.js index 2b43c38..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,16 +64,16 @@ 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) { @@ -94,7 +94,7 @@ ElasticSearchCall.prototype.exec = function (cb) { } request.end(''); } -} +}; /** * Wrap the default data event @@ -102,7 +102,7 @@ ElasticSearchCall.prototype.exec = function (cb) { */ ElasticSearchCall.prototype.data = function (callback) { this.on('data', callback); -} +}; /** * wrap the default done event @@ -110,7 +110,7 @@ ElasticSearchCall.prototype.data = function (callback) { */ ElasticSearchCall.prototype.done = function (callback) { this.on('done', callback); -} +}; /** * wrap the default error event @@ -118,4 +118,4 @@ ElasticSearchCall.prototype.done = function (callback) { */ ElasticSearchCall.prototype.error = function (callback) { this.on('error', callback); -} +};