diff --git a/.jshintrc b/.jshintrc new file mode 100644 index 0000000..4f4a593 --- /dev/null +++ b/.jshintrc @@ -0,0 +1,80 @@ +{ + // Settings + "passfail" : false, // Stop on first error. + "maxerr" : 500, // Maximum errors before stopping. + "multistr" : true, + + + // Predefined globals whom JSHint will ignore. + "node" : false, + + "predef" : [ // Extra globals. + "__dirname", + "Buffer", + "event", + "exports", + "global", + "main", + "module", + "process", + "require", + "setTimeout", + + "after", + "afterEach", + "afterAll", + "before", + "beforeEach", + "beforeAll", + "context", + "describe", + "it" + ], + + // Development. + "debug" : false, // Allow debugger statements e.g. browser breakpoints. + "devel" : true, // Allow development statements e.g. `console.log();`. + + + // EcmaScript 5. + "strict" : false, // Require `use strict` pragma in every file. + "globalstrict" : false, // Allow global "use strict" (also enables 'strict'). + + + "asi" : false, // Tolerate Automatic Semicolon Insertion (no semicolons). + "laxbreak" : false, // Tolerate unsafe line breaks e.g. `return [\n] x` without semicolons. + "bitwise" : true, // Prohibit bitwise operators (&, |, ^, etc.). + "boss" : false, // Tolerate assignments inside if, for & while. Usually conditions & loops are for comparison, not assignments. + "curly" : true, // Require {} for every new block or scope. + "eqeqeq" : false, // Require triple equals i.e. `===`. + "eqnull" : false, // Tolerate use of `== null`. + "evil" : false, // Tolerate use of `eval`. + "expr" : false, // Tolerate `ExpressionStatement` as Programs. + "forin" : false, // Tolerate `for in` loops without `hasOwnProperty`. + "immed" : true, // Require immediate invocations to be wrapped in parens e.g. `( function(){}() );` + "latedef" : true, // Prohibit variable use before definition. + "loopfunc" : true, // Allow functions to be defined within loops. + //"maxparams" : 4, + //"maxdepth" : 5, + //"maxcomplexity" : 10, + "noarg" : true, // Prohibit use of `arguments.caller` and `arguments.callee`. + "regexp" : false, // Prohibit `.` and `[^...]` in regular expressions. + "regexdash" : false, // Tolerate unescaped last dash i.e. `[-...]`. + "scripturl" : true, // Tolerate script-targeted URLs. + "shadow" : false, // Allows re-define variables later in code e.g. `var x=1; x=2;`. + "supernew" : false, // Tolerate `new function () { ... };` and `new Object;`. + "undef" : true, // Require all non-global variables be declared before they are used. + + + "newcap" : false, // Require capitalization of all constructor functions e.g. `new F()`. + "noempty" : true, // Prohibit use of empty blocks. + "nonew" : false, // Prohibit use of constructors for side-effects. + "nomen" : false, // Prohibit use of initial or trailing underbars in names. + "onevar" : false, // Allow only one `var` statement per function. + "plusplus" : false, // Prohibit use of `++` & `--`. + "sub" : true, // Tolerate all forms of subscript notation besides dot notation e.g. `dict['key']` instead of `dict.key`. + "trailing" : false, // Prohibit trailing whitespaces. (only works if white is 'true') + "white" : false, // Check against strict whitespace and indentation rules. + "indent" : 4, // Enforce consistent indenting. + "unused" : true +} diff --git a/Makefile b/Makefile index 82842d2..f5940fd 100644 --- a/Makefile +++ b/Makefile @@ -12,5 +12,8 @@ test-cov: lib-cov lib-cov: @jscoverage lib $@ -.PHONY: test test-cov +lint: + ./node_modules/.bin/jshint ./lib --config .jshintrc && \ + ./node_modules/.bin/jshint ./test --config ./test/.jshintrc +.PHONY: test test-cov diff --git a/lib/elasticsearchclient/calls/cluster.js b/lib/elasticsearchclient/calls/cluster.js index f9eeeac..212717d 100644 --- a/lib/elasticsearchclient/calls/cluster.js +++ b/lib/elasticsearchclient/calls/cluster.js @@ -3,118 +3,122 @@ ElasticSearchClient = require('../elasticSearchClient'); ElasticSearchClient.prototype.health = function(options, cb) { //Pull the callback and set it false to not clobber id. - if(arguments.length > 0 && typeof arguments[arguments.length-1]=='function'){ - cb=arguments[arguments.length-1]; - arguments[arguments.length-1]=false; + if (arguments.length > 0 && typeof arguments[arguments.length - 1] == 'function') { + cb = arguments[arguments.length - 1]; + arguments[arguments.length - 1] = false; } - var path = '/_cluster/health' + var path = '/_cluster/health'; 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.state = function(options, cb) { //Pull the callback and set it false to not clobber id. - if(arguments.length > 0 && typeof arguments[arguments.length-1]=='function'){ - cb=arguments[arguments.length-1]; - arguments[arguments.length-1]=false; + if (arguments.length > 0 && typeof arguments[arguments.length - 1] == 'function') { + cb = arguments[arguments.length - 1]; + arguments[arguments.length - 1] = false; } - var path = '/_cluster/state' + var path = '/_cluster/state'; 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.nodesInfo = function(nodes, options, cb) { //Pull the callback and set it false to not clobber id. - if(arguments.length > 0 && typeof arguments[arguments.length-1]=='function'){ - cb=arguments[arguments.length-1]; - arguments[arguments.length-1]=false; + if (arguments.length > 0 && typeof arguments[arguments.length - 1] == 'function') { + cb = arguments[arguments.length - 1]; + arguments[arguments.length - 1] = false; } - var path = '/_cluster/nodes' - if(nodes instanceof Array){ - for(n in nodes){ - if(n==0) - path+='/'+nodes[n] - else - path+=','+nodes[n] - } + var path = '/_cluster/nodes'; + if (Array.isArray(nodes)) { + for (var n in nodes) { + if (n === 0) { + path += '/' + nodes[n]; + } else { + path += ',' + nodes[n]; + } + } } 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.nodesStats = function(nodes, options, cb) { //Pull the callback and set it false to not clobber id. - if(arguments.length > 0 && typeof arguments[arguments.length-1]=='function'){ - cb=arguments[arguments.length-1]; - arguments[arguments.length-1]=false; + if (arguments.length > 0 && typeof arguments[arguments.length - 1] == 'function') { + cb = arguments[arguments.length - 1]; + arguments[arguments.length - 1] = false; } - var path = '/_cluster/nodes' - if(nodes instanceof Array){ - for(n in nodes){ - if(n==0) - path+='/'+nodes[n] - else - path+=','+nodes[n] - } - } - - path+='/stats' + var path = '/_cluster/nodes'; + if (Array.isArray(nodes)) { + for (var n in nodes) { + if (n === 0) { + path += '/' + nodes[n]; + } else { + path += ',' + nodes[n]; + } + } + } + + path += '/stats'; 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.nodesShutdown = function(nodes,options, cb) { +ElasticSearchClient.prototype.nodesShutdown = function(nodes, options, cb) { //Pull the callback and set it false to not clobber id. - if(arguments.length > 0 && typeof arguments[arguments.length-1]=='function'){ - cb=arguments[arguments.length-1]; - arguments[arguments.length-1]=false; + if (arguments.length > 0 && typeof arguments[arguments.length - 1] == 'function') { + cb = arguments[arguments.length - 1]; + arguments[arguments.length - 1] = false; + } + + var path = '/_cluster/nodes'; + if (Array.isArray(nodes)) { + for (var n in nodes) { + if (n === 0) { + path += '/' + nodes[n]; + } else { + path += ',' + nodes[n]; + } + } } - var path = '/_cluster/nodes' - if(nodes instanceof Array){ - for(n in nodes){ - if(n==0) - path+='/'+nodes[n] - else - path+=','+nodes[n] - } - } - path+='/_shutdown' + path += '/_shutdown'; var qs = ''; if (options) { - qs = querystring.stringify(options) + qs = querystring.stringify(options); } if (qs.length > 0) { path += "?" + qs; } return this.createCall({path: path, method: 'POST'}, this.clientOptions, cb); -} +}; diff --git a/lib/elasticsearchclient/calls/core.js b/lib/elasticsearchclient/calls/core.js index 9eea2ba..d18c736 100644 --- a/lib/elasticsearchclient/calls/core.js +++ b/lib/elasticsearchclient/calls/core.js @@ -1,156 +1,155 @@ -//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. - if(typeof arguments[arguments.length-1]=='function'){ - cb=arguments[arguments.length-1]; - arguments[arguments.length-1]=false; + if (typeof options === 'function') { + cb = options; + options = null; + } + + if (typeof id === 'function') { + cb = id; + options = null; + id = undefined; } var path = '/' + indexName + '/' + typeName; var qs = ''; - var method = 'POST' - + var method = 'POST'; - - if(typeof id === 'object'){ + if (typeof id === 'object') { options = id; id = undefined; } 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); -} + return this.createCall({data: JSON.stringify(document), path: path, method: method}, this.clientOptions, cb); +}; ElasticSearchClient.prototype.deleteDocument = function(indexName, typeName, documentId, options, cb) { - //Pull the callback and set it false to not clobber id. - if(typeof arguments[arguments.length-1]=='function'){ - cb=arguments[arguments.length-1]; - arguments[arguments.length-1]=false; + if (typeof options === 'function') { + cb = options; + options = null; } 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:'DELETE'}, this.clientOptions, cb); -} + 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. - if(typeof arguments[arguments.length-1]=='function'){ - cb=arguments[arguments.length-1]; - arguments[arguments.length-1]=false; + if (typeof options === 'function') { + cb = options; + options = null; } 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); -} + 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 ElasticSearchClient.prototype.search = function(indexName, typeName, queryObj, options, cb) { - //Pull the callback and set it false to not clobber id. - if(typeof arguments[arguments.length-1]=='function'){ - cb=arguments[arguments.length-1]; - arguments[arguments.length-1]=false; + //Pull the callback and set it false to not clobber id. // What id? + if (typeof arguments[arguments.length - 1] == 'function') { + cb = arguments[arguments.length - 1]; + arguments[arguments.length - 1] = false; } - var objArgs=[] - var path='' - + var objArgs = []; + var path = ''; + // Assumes quite a lot about the input - for(var i=0;i 0) { path += "?" + qs; } - var commandBuffer='' - for(var i =0; i 0) { path += "?" + qs; } - return this.createCall({path: path, method: 'PUT',data: JSON.stringify(queryObj)}, this.clientOptions, cb); -} + 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. - if(typeof arguments[arguments.length-1]=='function'){ - cb=arguments[arguments.length-1]; - arguments[arguments.length-1]=false; + if (typeof arguments[arguments.length - 1] == 'function') { + cb = arguments[arguments.length - 1]; + 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); -} + 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. - if(typeof arguments[arguments.length-1]=='function'){ - cb=arguments[arguments.length-1]; - arguments[arguments.length-1]=false; + if (typeof arguments[arguments.length - 1] == 'function') { + cb = arguments[arguments.length - 1]; + arguments[arguments.length - 1] = false; } 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); + + 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 '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 +249,7 @@ ElasticSearchClient.prototype.bulk = function(commandArray, options, cb) { options = null; } - var path = '/_bulk' + var path = '/_bulk'; var qs = ''; if (options) { if (options._index) { @@ -262,73 +261,72 @@ 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); -} + 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. - if(typeof arguments[arguments.length-1]=='function'){ - cb=arguments[arguments.length-1]; - arguments[arguments.length-1]=false; + if (typeof arguments[arguments.length - 1] == 'function') { + cb = arguments[arguments.length - 1]; + arguments[arguments.length - 1] = false; } - var path = '/' + indexName + '/' + typeName + '/'+documentId+'/_mlt'; + 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. - if(typeof arguments[arguments.length-1]=='function'){ - cb=arguments[arguments.length-1]; - arguments[arguments.length-1]=false; + if (typeof arguments[arguments.length - 1] == 'function') { + cb = arguments[arguments.length - 1]; + arguments[arguments.length - 1] = false; } document = document || {}; - var path = '/' + indexName + '/' + typeName + '/'+documentId+'/_update'; + 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 +}; diff --git a/lib/elasticsearchclient/calls/elasticSearchCall.js b/lib/elasticsearchclient/calls/elasticSearchCall.js index cc3c8ef..0e88b01 100644 --- a/lib/elasticsearchclient/calls/elasticSearchCall.js +++ b/lib/elasticsearchclient/calls/elasticSearchCall.js @@ -5,8 +5,9 @@ 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; @@ -21,14 +22,14 @@ function ElasticSearchCall(params, options, cb) { util.inherits(ElasticSearchCall, events.EventEmitter); -ElasticSearchCall.prototype.exec = function (cb) { - var self = this +ElasticSearchCall.prototype.exec = function(cb) { + var self = this; var reqOptions = { - path:this.path + this.params.path, - method:this.params.method || this.defaultMethod, - host:this.host, - port:this.port - } + path: this.path + this.params.path, + method: this.params.method || this.defaultMethod, + host: this.host, + port: this.port + }; if (typeof cb == 'function') { self.callback = cb; } @@ -43,39 +44,39 @@ ElasticSearchCall.prototype.exec = function (cb) { var request = client.request(reqOptions); if (self.timeout) { - request.setTimeout(self.timeout, function () { + request.setTimeout(self.timeout, function() { self.emit('error', new Error('timed out after ' + self.timeout + 'ms')); }); } - request.on('error', function (error) { - self.emit("error", error) - }) + request.on('error', function(error) { + self.emit("error", error); + }); - request.on('response', function (response) { + request.on('response', function(response) { var body = ""; - response.on('data', function (chunk) { + response.on('data', function(chunk) { body += chunk; }); - response.on('end', function () { + response.on('end', function() { if (typeof self.callback == 'function') { self.callback(undefined, body); } else { self.emit("data", body); - self.emit("done", 0) + self.emit("done", 0); } }); - response.on('error', function (error) { + 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')) + request.setHeader("Authorization", "Basic " + new Buffer(this.auth.username + ":" + this.auth.password).toString('base64')); } if (this.params.data) { @@ -88,28 +89,28 @@ ElasticSearchCall.prototype.exec = function (cb) { } else { request.end(''); } -} +}; /** * Wrap the default data event * @param callback */ -ElasticSearchCall.prototype.data = function (callback) { +ElasticSearchCall.prototype.data = function(callback) { this.on('data', callback); -} +}; /** * wrap the default done event * @param callback */ -ElasticSearchCall.prototype.done = function (callback) { +ElasticSearchCall.prototype.done = function(callback) { this.on('done', callback); -} +}; /** * wrap the default error event * @param callback */ -ElasticSearchCall.prototype.error = function (callback) { +ElasticSearchCall.prototype.error = function(callback) { this.on('error', callback); -} +}; diff --git a/lib/elasticsearchclient/calls/indices.js b/lib/elasticsearchclient/calls/indices.js index b85636a..34caff5 100644 --- a/lib/elasticsearchclient/calls/indices.js +++ b/lib/elasticsearchclient/calls/indices.js @@ -1,169 +1,167 @@ var querystring = require('querystring'), - ElasticSearchClient = require('../elasticSearchClient'); + ElasticSearchClient = require('../elasticSearchClient'); ElasticSearchClient.prototype.aliases = function(alias, options, cb) { //Pull the callback and set it false to not clobber id. - if(arguments.length > 0 && typeof arguments[arguments.length-1]=='function'){ - cb=arguments[arguments.length-1]; - arguments[arguments.length-1]=false; + if (arguments.length > 0 && typeof arguments[arguments.length - 1] == 'function') { + cb = arguments[arguments.length - 1]; + arguments[arguments.length - 1] = false; } var path = '/_aliases'; var qs = ''; if (options) { - qs = querystring.stringify(options) + qs = querystring.stringify(options); } if (qs.length > 0) { path += "?" + qs; } - return this.createCall({data:JSON.stringify(alias), path:path, method:'POST'}, this.clientOptions, cb); -} + return this.createCall({data: JSON.stringify(alias), path: path, method: 'POST'}, this.clientOptions, cb); +}; ElasticSearchClient.prototype.analyze = function(indexName, textToAnalyze, options, cb) { //Pull the callback and set it false to not clobber id. - if(arguments.length > 0 && typeof arguments[arguments.length-1]=='function'){ - cb=arguments[arguments.length-1]; - arguments[arguments.length-1]=false; + if (arguments.length > 0 && typeof arguments[arguments.length - 1] == 'function') { + cb = arguments[arguments.length - 1]; + arguments[arguments.length - 1] = false; } var path = '/' + indexName + '/_analyze'; var qs = ''; - var opts = options || {} - opts.text = textToAnalyze - qs = querystring.stringify(opts) + var opts = options || {}; + opts.text = textToAnalyze; + qs = querystring.stringify(opts); if (qs.length > 0) { path += "?" + qs; } - return this.createCall({path:path, method:'GET'}, this.clientOptions, cb); -} + return this.createCall({path: path, method: 'GET'}, this.clientOptions, cb); +}; ElasticSearchClient.prototype.createIndex = function(indexName, settings, options, cb) { //Pull the callback and set it false to not clobber id. - if(arguments.length > 0 && typeof arguments[arguments.length-1]=='function'){ - cb=arguments[arguments.length-1]; - arguments[arguments.length-1]=false; + if (arguments.length > 0 && typeof arguments[arguments.length - 1] == 'function') { + cb = arguments[arguments.length - 1]; + arguments[arguments.length - 1] = false; } var path = '/' + indexName; var qs = ''; if (options) { - qs = querystring.stringify(options) + qs = querystring.stringify(options); } if (qs.length > 0) { path += "?" + qs; } - return this.createCall({data:JSON.stringify(settings), path:path, method:'PUT'}, this.clientOptions, cb); -} + return this.createCall({data: JSON.stringify(settings), path: path, method: 'PUT'}, this.clientOptions, cb); +}; ElasticSearchClient.prototype.deleteIndex = function(indexName, options, cb) { //Pull the callback and set it false to not clobber id. - if(arguments.length > 0 && typeof arguments[arguments.length-1]=='function'){ - cb=arguments[arguments.length-1]; - arguments[arguments.length-1]=false; + if (arguments.length > 0 && typeof arguments[arguments.length - 1] == 'function') { + cb = arguments[arguments.length - 1]; + arguments[arguments.length - 1] = false; } var path = '/' + indexName; var qs = ''; if (options) { - qs = querystring.stringify(options) + qs = querystring.stringify(options); } if (qs.length > 0) { path += "?" + qs; } - return this.createCall({path:path, method:'DELETE'}, this.clientOptions, cb); -} - + return this.createCall({path: path, method: 'DELETE'}, this.clientOptions, cb); +}; ElasticSearchClient.prototype.openIndex = function(indexName, options, cb) { //Pull the callback and set it false to not clobber id. - if(arguments.length > 0 && typeof arguments[arguments.length-1]=='function'){ - cb=arguments[arguments.length-1]; - arguments[arguments.length-1]=false; + if (arguments.length > 0 && typeof arguments[arguments.length - 1] == 'function') { + cb = arguments[arguments.length - 1]; + arguments[arguments.length - 1] = false; } var path = '/' + indexName + '/_open'; var qs = ''; if (options) { - qs = querystring.stringify(options) + qs = querystring.stringify(options); } if (qs.length > 0) { path += "?" + qs; } - return this.createCall({path:path, method:'POST'}, this.clientOptions, cb); -} + return this.createCall({path: path, method: 'POST'}, this.clientOptions, cb); +}; ElasticSearchClient.prototype.closeIndex = function(indexName, options, cb) { //Pull the callback and set it false to not clobber id. - if(arguments.length > 0 && typeof arguments[arguments.length-1]=='function'){ - cb=arguments[arguments.length-1]; - arguments[arguments.length-1]=false; + if (arguments.length > 0 && typeof arguments[arguments.length - 1] == 'function') { + cb = arguments[arguments.length - 1]; + arguments[arguments.length - 1] = false; } var path = '/' + indexName + '/_open'; var qs = ''; if (options) { - qs = querystring.stringify(options) + qs = querystring.stringify(options); } if (qs.length > 0) { path += "?" + qs; } - return this.createCall({path:path, method:'POST'}, this.clientOptions, cb); -} - + return this.createCall({path: path, method: 'POST'}, this.clientOptions, cb); +}; ElasticSearchClient.prototype.updateSettings = function(indexName, options, cb) { //Pull the callback and set it false to not clobber id. - if(arguments.length > 0 && typeof arguments[arguments.length-1]=='function'){ - cb=arguments[arguments.length-1]; - arguments[arguments.length-1]=false; + if (arguments.length > 0 && typeof arguments[arguments.length - 1] == 'function') { + cb = arguments[arguments.length - 1]; + arguments[arguments.length - 1] = false; } var path = '/' + indexName + '/_settings'; var qs = ''; if (options) { - qs = querystring.stringify(options) + qs = querystring.stringify(options); } if (qs.length > 0) { path += "?" + qs; } - return this.createCall({path:path, method:'PUT'}, this.clientOptions, cb); -} + return this.createCall({path: path, method: 'PUT'}, this.clientOptions, cb); +}; ElasticSearchClient.prototype.getSettings = function(indexName, options, cb) { //Pull the callback and set it false to not clobber id. - if(arguments.length > 0 && typeof arguments[arguments.length-1]=='function'){ - cb=arguments[arguments.length-1]; - arguments[arguments.length-1]=false; + if (arguments.length > 0 && typeof arguments[arguments.length - 1] == 'function') { + cb = arguments[arguments.length - 1]; + arguments[arguments.length - 1] = false; } var path = '/' + indexName + '/_settings'; 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); -} + return this.createCall({path: path, method: 'GET'}, this.clientOptions, cb); +}; ElasticSearchClient.prototype.updateSettings = function(indexName, settings, options, cb) { //Pull the callback and set it false to not clobber id. - if(arguments.length > 0 && typeof arguments[arguments.length-1]=='function'){ - cb=arguments[arguments.length-1]; - arguments[arguments.length-1]=false; + if (arguments.length > 0 && typeof arguments[arguments.length - 1] == 'function') { + cb = arguments[arguments.length - 1]; + arguments[arguments.length - 1] = false; } var path = '/' + indexName + '/_settings'; var qs = ''; if (options) { - qs = querystring.stringify(options) + qs = querystring.stringify(options); } if (qs.length > 0) { path += "?" + qs; } - return this.createCall({data:JSON.stringify(settings), path:path, method:'PUT'}, this.clientOptions, cb); -} + return this.createCall({data: JSON.stringify(settings), path: path, method: 'PUT'}, this.clientOptions, cb); +}; /** The _mapping endpoint is prefixed by an optional index and or type name */ function computeMappingEndpointPath(indexName, typeName, options) { @@ -190,8 +188,8 @@ ElasticSearchClient.prototype.getMapping = function(indexName, typeName, options } var path = computeMappingEndpointPath(indexName, typeName, options); - return this.createCall({path:path, method:'get'}, this.clientOptions, cb); -} + return this.createCall({path: path, method: 'get'}, this.clientOptions, cb); +}; ElasticSearchClient.prototype.putMapping = function(indexName, typeName, mapping, options, cb) { //Pull the callback and set it false to not clobber id. @@ -202,8 +200,8 @@ ElasticSearchClient.prototype.putMapping = function(indexName, typeName, mapping //note that here indexName and typeName must be specified //we let elasticsearch asnwer something went wrong var path = computeMappingEndpointPath(indexName, typeName, options); - return this.createCall({data:JSON.stringify(mapping), path:path, method:'PUT'}, this.clientOptions, cb); -} + return this.createCall({data: JSON.stringify(mapping), path: path, method: 'PUT'}, this.clientOptions, cb); +}; ElasticSearchClient.prototype.deleteMapping = function(indexName, typeName, options, cb) { //Pull the callback and set it false to not clobber id. @@ -213,209 +211,209 @@ ElasticSearchClient.prototype.deleteMapping = function(indexName, typeName, opti } var path = computeMappingEndpointPath(indexName, typeName, options); - return this.createCall({path:path, method:'DELETE'}, this.clientOptions, cb); -} + return this.createCall({path: path, method: 'DELETE'}, this.clientOptions, cb); +}; ElasticSearchClient.prototype.refresh = function(indexName, options, cb) { //Pull the callback and set it false to not clobber id. - if(arguments.length > 0 && typeof arguments[arguments.length-1]=='function'){ - cb=arguments[arguments.length-1]; - arguments[arguments.length-1]=false; + if (arguments.length > 0 && typeof arguments[arguments.length - 1] == 'function') { + cb = arguments[arguments.length - 1]; + arguments[arguments.length - 1] = false; } var path = '/' + indexName + '/_refresh'; var qs = ''; if (options) { - qs = querystring.stringify(options) + qs = querystring.stringify(options); } if (qs.length > 0) { path += "?" + qs; } - return this.createCall({path:path, method:'POST'}, this.clientOptions, cb); -} + return this.createCall({path: path, method: 'POST'}, this.clientOptions, cb); +}; ElasticSearchClient.prototype.optimize = function(indexName, options, cb) { //Pull the callback and set it false to not clobber id. - if(arguments.length > 0 && typeof arguments[arguments.length-1]=='function'){ - cb=arguments[arguments.length-1]; - arguments[arguments.length-1]=false; + if (arguments.length > 0 && typeof arguments[arguments.length - 1] == 'function') { + cb = arguments[arguments.length - 1]; + arguments[arguments.length - 1] = false; } var path = '/' + indexName + '/_optimize'; var qs = ''; if (options) { - qs = querystring.stringify(options) + qs = querystring.stringify(options); } if (qs.length > 0) { path += "?" + qs; } - return this.createCall({path:path, method:'POST'}, this.clientOptions, cb); -} + return this.createCall({path: path, method: 'POST'}, this.clientOptions, cb); +}; ElasticSearchClient.prototype.flush = function(indexName, options, cb) { //Pull the callback and set it false to not clobber id. - if(arguments.length > 0 && typeof arguments[arguments.length-1]=='function'){ - cb=arguments[arguments.length-1]; - arguments[arguments.length-1]=false; + if (arguments.length > 0 && typeof arguments[arguments.length - 1] == 'function') { + cb = arguments[arguments.length - 1]; + arguments[arguments.length - 1] = false; } var path = '/' + indexName + '/_flush'; var qs = ''; if (options) { - qs = querystring.stringify(options) + qs = querystring.stringify(options); } if (qs.length > 0) { path += "?" + qs; } - return this.createCall({path:path, method:'POST'}, this.clientOptions, cb); -} + return this.createCall({path: path, method: 'POST'}, this.clientOptions, cb); +}; ElasticSearchClient.prototype.snapshot = function(indexName, options, cb) { //Pull the callback and set it false to not clobber id. - if(arguments.length > 0 && typeof arguments[arguments.length-1]=='function'){ - cb=arguments[arguments.length-1]; - arguments[arguments.length-1]=false; + if (arguments.length > 0 && typeof arguments[arguments.length - 1] == 'function') { + cb = arguments[arguments.length - 1]; + arguments[arguments.length - 1] = false; } var path = '/' + indexName + '/_gateway/snapshot'; var qs = ''; if (options) { - qs = querystring.stringify(options) + qs = querystring.stringify(options); } if (qs.length > 0) { path += "?" + qs; } - return this.createCall({path:path, method:'POST'}, this.clientOptions, cb); -} + return this.createCall({path: path, method: 'POST'}, this.clientOptions, cb); +}; ElasticSearchClient.prototype.defineTemplate = function(templateName, template, options, cb) { //Pull the callback and set it false to not clobber id. - if(arguments.length > 0 && typeof arguments[arguments.length-1]=='function'){ - cb=arguments[arguments.length-1]; - arguments[arguments.length-1]=false; + if (arguments.length > 0 && typeof arguments[arguments.length - 1] == 'function') { + cb = arguments[arguments.length - 1]; + arguments[arguments.length - 1] = false; } var path = '/_template/' + templateName; var qs = ''; if (options) { - qs = querystring.stringify(options) + qs = querystring.stringify(options); } if (qs.length > 0) { path += "?" + qs; } - return this.createCall({data:JSON.stringify(template), path:path, method:'PUT'}, this.clientOptions, cb); -} + return this.createCall({data: JSON.stringify(template), path: path, method: 'PUT'}, this.clientOptions, cb); +}; ElasticSearchClient.prototype.deleteTemplate = function(templateName, options, cb) { //Pull the callback and set it false to not clobber id. - if(arguments.length > 0 && typeof arguments[arguments.length-1]=='function'){ - cb=arguments[arguments.length-1]; - arguments[arguments.length-1]=false; + if (arguments.length > 0 && typeof arguments[arguments.length - 1] == 'function') { + cb = arguments[arguments.length - 1]; + arguments[arguments.length - 1] = false; } var path = '/_template/' + templateName; var qs = ''; if (options) { - qs = querystring.stringify(options) + qs = querystring.stringify(options); } if (qs.length > 0) { path += "?" + qs; } - return this.createCall({ path:path, method:'DELETE'}, this.clientOptions, cb); -} + return this.createCall({ path: path, method: 'DELETE'}, this.clientOptions, cb); +}; ElasticSearchClient.prototype.getTemplate = function(templateName, options, cb) { //Pull the callback and set it false to not clobber id. - if(arguments.length > 0 && typeof arguments[arguments.length-1]=='function'){ - cb=arguments[arguments.length-1]; - arguments[arguments.length-1]=false; + if (arguments.length > 0 && typeof arguments[arguments.length - 1] == 'function') { + cb = arguments[arguments.length - 1]; + arguments[arguments.length - 1] = false; } var path = '/_template/' + templateName; 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); -} + return this.createCall({ path: path, method: 'GET'}, this.clientOptions, cb); +}; ElasticSearchClient.prototype.status = function(indexName, options, cb) { //Pull the callback and set it false to not clobber id. - if(arguments.length > 0 && typeof arguments[arguments.length-1]=='function'){ - cb=arguments[arguments.length-1]; - arguments[arguments.length-1]=false; + if (arguments.length > 0 && typeof arguments[arguments.length - 1] == 'function') { + cb = arguments[arguments.length - 1]; + arguments[arguments.length - 1] = false; } - if(toString.call(indexName) != '[object String]'){ - options = indexName; - indexName = undefined; + if (toString.call(indexName) != '[object String]') { + options = indexName; + indexName = undefined; } var path = indexName ? '/' + indexName + '/_status' : '/_status'; 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); -} + return this.createCall({ path: path, method: 'GET'}, this.clientOptions, cb); +}; ElasticSearchClient.prototype.stats = function(indexName, options, cb) { //Pull the callback and set it false to not clobber id. - if(arguments.length > 0 && typeof arguments[arguments.length-1]=='function'){ - cb=arguments[arguments.length-1]; - arguments[arguments.length-1]=false; + if (arguments.length > 0 && typeof arguments[arguments.length - 1] == 'function') { + cb = arguments[arguments.length - 1]; + arguments[arguments.length - 1] = false; } var path = indexName ? '/' + indexName + '/_stats' : '/_stats'; 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); -} + return this.createCall({ path: path, method: 'GET'}, this.clientOptions, cb); +}; ElasticSearchClient.prototype.clearCache = function(indexName, options, cb) { //Pull the callback and set it false to not clobber id. - if(arguments.length > 0 && typeof arguments[arguments.length-1]=='function'){ - cb=arguments[arguments.length-1]; - arguments[arguments.length-1]=false; + if (arguments.length > 0 && typeof arguments[arguments.length - 1] == 'function') { + cb = arguments[arguments.length - 1]; + arguments[arguments.length - 1] = false; } var path = '/' + indexName + '/_cache/clear'; var qs = ''; if (options) { - qs = querystring.stringify(options) + qs = querystring.stringify(options); } if (qs.length > 0) { path += "?" + qs; } - return this.createCall({ path:path, method:'POST'}, this.clientOptions, cb); -} + return this.createCall({ path: path, method: 'POST'}, this.clientOptions, cb); +}; ElasticSearchClient.prototype.getSegments = function(indexName, options, cb) { //Pull the callback and set it false to not clobber id. - if(arguments.length > 0 && typeof arguments[arguments.length-1]=='function'){ - cb=arguments[arguments.length-1]; - arguments[arguments.length-1]=false; + if (arguments.length > 0 && typeof arguments[arguments.length - 1] == 'function') { + cb = arguments[arguments.length - 1]; + arguments[arguments.length - 1] = false; } var path = '/' + indexName + '/_segments'; 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); -} + return this.createCall({path: path, method: 'GET'}, this.clientOptions, cb); +}; diff --git a/lib/elasticsearchclient/calls/twitter.js b/lib/elasticsearchclient/calls/twitter.js index c71ac87..46d9bbb 100644 --- a/lib/elasticsearchclient/calls/twitter.js +++ b/lib/elasticsearchclient/calls/twitter.js @@ -2,26 +2,26 @@ var querystring = require('querystring'), ElasticSearchClient = require('../elasticSearchClient'); ElasticSearchClient.prototype.deleteTwitterRiver = function(riverName, options, cb) { - var path = '/_river/'+riverName+'/_meta'; + var path = '/_river/' + riverName + '/_meta'; var qs = ''; if (options) { - qs = querystring.stringify(options) + qs = querystring.stringify(options); } if (qs.length > 0) { path += "?" + qs; } - return this.createCall({path:path, method:'DELETE'}, this.clientOptions,cb); -} + return this.createCall({path: path, method: 'DELETE'}, this.clientOptions, cb); +}; ElasticSearchClient.prototype.createOrModifyTwitterRiver = function(riverName, riverData, options, cb) { - var path = '/_river/'+riverName+'/_meta'; + var path = '/_river/' + riverName + '/_meta'; var qs = ''; if (options) { - qs = querystring.stringify(options) + qs = querystring.stringify(options); } if (qs.length > 0) { path += "?" + qs; } - return this.createCall({data:JSON.stringify(riverData),path:path,method: 'PUT'}, this.clientOptions, cb); -} \ No newline at end of file + return this.createCall({data: JSON.stringify(riverData), path: path, method: 'PUT'}, this.clientOptions, cb); +}; \ No newline at end of file diff --git a/lib/elasticsearchclient/elasticSearchClient.js b/lib/elasticsearchclient/elasticSearchClient.js index 612a118..0a3b673 100644 --- a/lib/elasticsearchclient/elasticSearchClient.js +++ b/lib/elasticsearchclient/elasticSearchClient.js @@ -1,67 +1,67 @@ -var querystring = require('querystring'), - events = require('events'), - http = require('http'); -https = require('https'); +/*jshint unused:false */ +/*jshint predef:false */ +/*jshint undef:false */ module.exports = ElasticSearchClient; -Core = require('./calls/core') -Cluster = require('./calls/cluster') -Indices = require('./calls/indices') -Twitter = require('./calls/twitter') -ElasticSearchCall = require('./calls/elasticSearchCall.js'); +var Core = require('./calls/core'); +var Cluster = require('./calls/cluster'); +var Indices = require('./calls/indices'); +var Twitter = require('./calls/twitter'); +var ElasticSearchCall = require('./calls/elasticSearchCall.js'); function ElasticSearchClient(options) { - this.clientOptions = options || {} + this.clientOptions = options || {}; } ElasticSearchClient.prototype.createCall = function(params, options, cb) { //If options.hosts round robin the hosts + var nextHost; if (options.hosts) { - var nextHost = options.hosts.shift(); + nextHost = options.hosts.shift(); options.hosts.push(nextHost); - }else{ - nextHost=options; + } else { + nextHost = options; } - if(typeof cb=='function'){ + if (typeof cb == 'function') { var call = new ElasticSearchCall(params, nextHost, cb); call.exec(); - }else{ + } else { return new ElasticSearchCall(params, nextHost); } -} +}; -var inetRE = /inet\[\/(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\:(\d{1,5})\]/ +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 * cluster in the hosts list. */ -ElasticSearchClient.prototype.discoverNodes = function(){ +ElasticSearchClient.prototype.discoverNodes = function() { var self = this; var query = this.nodesInfo(); var hosts = []; - query.on('data', function(data){ - var obj = JSON.parse(data); - var nodes = obj.nodes; - for(n in nodes){ - node = nodes[n]; - if(!"http_address" in node){ - continue; - } - - address = inetRE.exec(node.http_address); - if(!address){ - continue; - } + query.on('data', function(data) { + var obj = JSON.parse(data); + var nodes = obj.nodes; + for (var n in nodes) { + var node = nodes[n]; + if (!("http_address" in node)) { + continue; + } + + var address = inetRE.exec(node.http_address); + if (!address) { + continue; + } - hosts.push({ host : address[1], port : address[2] }); - } + hosts.push({ host : address[1], port : address[2] }); + } - if(hosts.length > 0){ - self.clientOptions.hosts = hosts; - } + if (hosts.length > 0) { + self.clientOptions.hosts = hosts; + } }); query.exec(); -} \ No newline at end of file +}; diff --git a/package.json b/package.json index a4d000d..0ed6a86 100644 --- a/package.json +++ b/package.json @@ -13,9 +13,10 @@ }, "dependencies": {}, "devDependencies": { - "mocha": "1.7.4" - , "chai": "*" - }, + "chai": "*", + "jshint": "~2.1.2", + "mocha": "1.7.4" + }, "repository": { "type": "git", "url": "git://github.com/phillro/node-elasticsearch-client.git" diff --git a/test/.jshintrc b/test/.jshintrc new file mode 100644 index 0000000..5a9a12c --- /dev/null +++ b/test/.jshintrc @@ -0,0 +1,81 @@ +{ + // Settings + "passfail" : false, // Stop on first error. + "maxerr" : 500, // Maximum errors before stopping. + "multistr" : true, + + + // Predefined globals whom JSHint will ignore. + "node" : false, + + "predef" : [ // Extra globals. + "__dirname", + "Buffer", + "event", + "exports", + "global", + "main", + "module", + "process", + "require", + "setTimeout", + + "after", + "afterEach", + "afterAll", + "before", + "beforeEach", + "beforeAll", + "context", + "describe", + "it" + ], + + // Development. + "debug" : false, // Allow debugger statements e.g. browser breakpoints. + "devel" : true, // Allow development statements e.g. `console.log();`. + + + // EcmaScript 5. + "es5" : true, // Allow EcmaScript 5 syntax. + "strict" : false, // Require `use strict` pragma in every file. + "globalstrict" : false, // Allow global "use strict" (also enables 'strict'). + + + "asi" : false, // Tolerate Automatic Semicolon Insertion (no semicolons). + "laxbreak" : false, // Tolerate unsafe line breaks e.g. `return [\n] x` without semicolons. + "bitwise" : true, // Prohibit bitwise operators (&, |, ^, etc.). + "boss" : false, // Tolerate assignments inside if, for & while. Usually conditions & loops are for comparison, not assignments. + "curly" : true, // Require {} for every new block or scope. + "eqeqeq" : false, // Require triple equals i.e. `===`. + "eqnull" : false, // Tolerate use of `== null`. + "evil" : false, // Tolerate use of `eval`. + "expr" : false, // Tolerate `ExpressionStatement` as Programs. + "forin" : false, // Tolerate `for in` loops without `hasOwnProperty`. + "immed" : true, // Require immediate invocations to be wrapped in parens e.g. `( function(){}() );` + "latedef" : true, // Prohibit variable use before definition. + "loopfunc" : true, // Allow functions to be defined within loops. + //"maxparams" : 4, + //"maxdepth" : 5, + //"maxcomplexity" : 10, + "noarg" : true, // Prohibit use of `arguments.caller` and `arguments.callee`. + "regexp" : false, // Prohibit `.` and `[^...]` in regular expressions. + "regexdash" : false, // Tolerate unescaped last dash i.e. `[-...]`. + "scripturl" : true, // Tolerate script-targeted URLs. + "shadow" : false, // Allows re-define variables later in code e.g. `var x=1; x=2;`. + "supernew" : false, // Tolerate `new function () { ... };` and `new Object;`. + "undef" : true, // Require all non-global variables be declared before they are used. + + + "newcap" : false, // Require capitalization of all constructor functions e.g. `new F()`. + "noempty" : true, // Prohibit use of empty blocks. + "nonew" : false, // Prohibit use of constructors for side-effects. + "nomen" : false, // Prohibit use of initial or trailing underbars in names. + "onevar" : false, // Allow only one `var` statement per function. + "plusplus" : false, // Prohibit use of `++` & `--`. + "sub" : true, // Tolerate all forms of subscript notation besides dot notation e.g. `dict['key']` instead of `dict.key`. + "trailing" : true, // Prohibit trailing whitespaces. (only works if white is 'true') + "white" : true, // Check against strict whitespace and indentation rules. + "indent" : 4, // Enforce consistent indenting. + "unused" : true +} diff --git a/test/cluster.test.js b/test/cluster.test.js index 2de9607..f969414 100644 --- a/test/cluster.test.js +++ b/test/cluster.test.js @@ -11,14 +11,11 @@ var serverOptions = { }*/ }; - - - var elasticSearchClient = new ElasticSearchClient(serverOptions); -describe("ElasticSearchClient Cluster apis", function(){ - describe("#health", function(){ - it("should provide health stats", function(done){ +describe("ElasticSearchClient Cluster apis", function() { + describe("#health", function() { + it("should provide health stats", function(done) { elasticSearchClient.health() .on('data', function(data) { data = JSON.parse(data); @@ -29,10 +26,9 @@ describe("ElasticSearchClient Cluster apis", function(){ }); }); - - describe("#state", function(){ - it("should test state", function(done){ - elasticSearchClient.state({filter_nodes:true}) + describe("#state", function() { + it("should test state", function(done) { + elasticSearchClient.state({filter_nodes: true}) .on('data', function(data) { data = JSON.parse(data); data.routing_nodes.should.be.ok; @@ -42,10 +38,10 @@ describe("ElasticSearchClient Cluster apis", function(){ }); }); - describe("#nodesInfo", function(){ - it("should provide nodes' info", function(done){ + describe("#nodesInfo", function() { + it("should provide nodes' info", function(done) { elasticSearchClient.nodesInfo([]) - .on('data', function( data) { + .on('data', function(data) { data = JSON.parse(data); data.ok.should.be.ok; done(); @@ -54,10 +50,10 @@ describe("ElasticSearchClient Cluster apis", function(){ }); }); - describe("#nodesStats", function(){ - it("should provide node statistics", function(done){ + describe("#nodesStats", function() { + it("should provide node statistics", function(done) { elasticSearchClient.nodesStats([]) - .on('data', function( data) { + .on('data', function(data) { data = JSON.parse(data); data.nodes.should.be.ok; done(); @@ -66,15 +62,15 @@ describe("ElasticSearchClient Cluster apis", function(){ }); }); - describe("#nodeShutdown", function(){ + describe("#nodeShutdown", function() { /* * Temporarily disabled so that other tests have chance to succeed before * the node shuts down */ - it.skip("should shut down the node", function(done){ + it.skip("should shut down the node", function(done) { elasticSearchClient.nodesShutdown([]) - .on('data', function( data) { + .on('data', function(data) { data = JSON.parse(data); data.ok.should.be.ok; done(); @@ -82,5 +78,4 @@ describe("ElasticSearchClient Cluster apis", function(){ .exec(); }); }); - }); diff --git a/test/core.test.js b/test/core.test.js index 7a25cab..24c916e 100644 --- a/test/core.test.js +++ b/test/core.test.js @@ -16,32 +16,24 @@ var objName = 'your_object_name'; var elasticSearchClient = new ElasticSearchClient(serverOptions); -describe("ElasticSearchClient Core api", function(){ - - before(function(done){ - /* - * To allow running tests individually `mocha --grep search` - */ - elasticSearchClient.index(indexName, objName, {'name':'sushi'}, "sushi") - .on('data', function(){ - done(); - }) - .exec(); +describe("ElasticSearchClient Core api", function() { + before(function(done) { + elasticSearchClient.index(indexName, objName, {name: 'sushi', description: 'foo'}, 'sushi', done); }); - describe("#index", function(){ - it("should index a json object", function(done){ - elasticSearchClient.index(indexName, objName, {'name':'sushi'}) + describe("#index", function() { + it("should index a json object", function(done) { + elasticSearchClient.index(indexName, objName, {'name': 'sushi'}) .on('data', function(data) { data = JSON.parse(data); data.ok.should.be.ok; done(); }) .exec(); - }) + }); - it("should index an object with given id under the same id", function(done){ - elasticSearchClient.index(indexName, objName, {'name':'name', id:"9999"}, "1111") + it("should index an object with given id under the same id", function(done) { + elasticSearchClient.index(indexName, objName, {'name': 'name', id: "9999"}, "1111") .on('data', function(data) { data = JSON.parse(data); data._id.should.equal("1111"); @@ -51,27 +43,48 @@ describe("ElasticSearchClient Core api", function(){ }); }); - describe("#index canonical", function(){ - it("should index a json object", function(done){ - elasticSearchClient.index(indexName, objName, {'name':'sushi'}, function(err,data){ + describe("#index canonical", function() { + it("should index a json object", function(done) { + elasticSearchClient.index(indexName, objName, {'name': 'sushi'}, function(err, data) { + should.not.exist(err); data = JSON.parse(data); data.ok.should.be.ok; done(); - }) - }) + }); + }); - it("should index an object with given id under the same id", function(done){ - elasticSearchClient.index(indexName, objName, {'name':'name', id:"9999"}, "1111", function(err,data){ + it("should allow options to be passed in", function(done) { + elasticSearchClient.index(indexName, objName, {'name': 'sushi'}, {}, function(err, data) { + should.not.exist(err); data = JSON.parse(data); - data._id.should.equal("1111"); + data.ok.should.be.ok; done(); - }) + }); }); - }); + context("when an id is passed in", function() { + it("should index an object with given id under the same id", function(done) { + elasticSearchClient.index(indexName, objName, {name: 'name', id: "9999"}, "1111", function(err, data) { + should.not.exist(err); + data = JSON.parse(data); + data._id.should.equal("1111"); + done(); + }); + }); + + it("should allow options to be passed in", function(done) { + elasticSearchClient.index(indexName, objName, {'name': 'sushi'}, "1111", {}, function(err, data) { + should.not.exist(err); + data = JSON.parse(data); + data.ok.should.be.ok; + done(); + }); + }); + }); + }); - describe("#get", function(){ - it("should fetch the row by id", function(done){ + describe("#get", function() { + it("should fetch the row by id", function(done) { elasticSearchClient.get(indexName, objName, "sushi") .on('data', function(data) { data = JSON.parse(data); @@ -80,23 +93,39 @@ describe("ElasticSearchClient Core api", function(){ data._source.should.be.ok; done(); }) - .exec() + .exec(); }); }); - describe("#get canonical", function(){ - it("should fetch the row by id", function(done){ - elasticSearchClient.get(indexName, objName, "sushi", function(err,data){ + describe("#get canonical", function() { + it("should fetch the row by id", function(done) { + elasticSearchClient.get(indexName, objName, "sushi", function(err, data) { + should.not.exist(err); data = JSON.parse(data); data.exists.should.exist; data._id.should.equal("sushi"); + data._source.name.should.be.ok; + data._source.description.should.be.ok; data._source.should.be.ok; done(); - }) + }); + }); + + it("should allow specifying fields to be returned", function(done) { + elasticSearchClient.get(indexName, objName, "sushi", {fields: 'name'}, function(err, data) { + should.not.exist(err); + data = JSON.parse(data); + data.exists.should.exist; + data._id.should.equal('sushi'); + data.fields.should.be.ok; + data.fields.name.should.equal('sushi'); + should.not.exist(data.fields.description); + done(); + }); }); }); - describe("#multiget", function(){ + describe("#multiget", function() { var check = function(err, data, callback) { data = JSON.parse(data); data.docs.should.exist; @@ -104,7 +133,8 @@ describe("ElasticSearchClient Core api", function(){ data.docs[0]._source.should.be.ok; callback(err); }; - it("should fetch the row by id via multiget event style", function(done){ + + it("should fetch the row by id via multiget event style", function(done) { elasticSearchClient.multiget(indexName, objName, [ "sushi" ], {}) .on('data', function(data) { check(null, data, done); @@ -114,51 +144,56 @@ describe("ElasticSearchClient Core api", function(){ }) .exec(); }); - it("should fetch the row by id via multiget canonical", function(done){ - elasticSearchClient.multiget(indexName, objName, [ "sushi" ] , function(err, data) { + + it("should fetch the row by id via multiget canonical", function(done) { + elasticSearchClient.multiget(indexName, objName, [ "sushi" ], function(err, data) { check(err, data, done); }); }); - it("should fetch the row by doc via multiget canonical", function(done){ - elasticSearchClient.multiget(indexName, objName, [{_id: "sushi"}] , function(err, data) { + + it("should fetch the row by doc via multiget canonical", function(done) { + elasticSearchClient.multiget(indexName, objName, [{_id: "sushi"}], function(err, data) { check(err, data, done); }); }); }); - describe("#update", function(){ - it("should update the existing doc by id", function(done){ - elasticSearchClient.update(indexName, objName, "sushi", {doc:{occupation: "play"}}) + describe("#update", function() { + it("should update the existing doc by id", function(done) { + elasticSearchClient.update(indexName, objName, "sushi", {doc: {occupation: "play"}}) .on('data', function(data) { data = JSON.parse(data); data.should.be.ok; data._id.should.equal("sushi"); done(); }) - .exec() + .exec(); }); - }); - describe("#update canonical", function(){ - it("should update the existing doc by id", function(done){ - elasticSearchClient.update(indexName, objName, "sushi", {doc:{occupation: "play"}}, function(err,data){ + describe("#update canonical", function() { + it("should update the existing doc by id", function(done) { + elasticSearchClient.update(indexName, objName, "sushi", {doc: {occupation: "play"}}, function(err, data) { data = JSON.parse(data); data.should.be.ok; data._id.should.equal("sushi"); done(); - }) + }); }); - }); - describe("#search", function(){ - it("should search based on given query", function(done){ - var qryObj = { - "query" : { - "term" : { "name" : "sushi" } + describe("#search", function() { + var qryObj; + + before(function() { + qryObj = { + query: { + term: {name: 'sushi'} } }; + }); + + it("should search based on given query", function(done) { elasticSearchClient.search(indexName, objName, qryObj) .on('data', function(data) { data = JSON.parse(data); @@ -169,12 +204,7 @@ describe("ElasticSearchClient Core api", function(){ .exec(); }); - it("should search even if collection name not present", function(done){ - var qryObj = { - "query" : { - "term" : { "name" : "sushi" } - } - }; + it("should search even if collection name not present", function(done) { elasticSearchClient.search(indexName, qryObj) .on('data', function(data) { data = JSON.parse(data); @@ -185,12 +215,7 @@ describe("ElasticSearchClient Core api", function(){ .exec(); }); - it("should search even if index_name is not present", function(done){ - var qryObj = { - "query" : { - "term" : { "name" : "sushi" } - } - }; + it("should search even if index_name is not present", function(done) { elasticSearchClient.search(qryObj) .on('data', function(data) { data = JSON.parse(data); @@ -202,57 +227,79 @@ describe("ElasticSearchClient Core api", function(){ }); }); - describe("#search canonical", function(){ - it("should search based on given query", function(done){ - var qryObj = { - "query" : { - "term" : { "name" : "sushi" } + describe("#search canonical", function() { + var qryObj; + + before(function() { + qryObj = { + query: { + term: {name: 'sushi'} } }; - elasticSearchClient.search(indexName, objName, qryObj, function(err,data){ + }); + + it("should search based on given query", function(done) { + elasticSearchClient.search(indexName, objName, qryObj, function(err, data) { + should.not.exist(err); data = JSON.parse(data); data.should.not.be.undefined.null.empty; data.hits.total.should.be.gte(0); done(); - }) + }); }); - it("should search even if collection name not present", function(done){ - var qryObj = { - "query" : { - "term" : { "name" : "sushi" } - } - }; - elasticSearchClient.search(indexName, qryObj, function(err,data){ + it("should allow options to be passed in when index_name is not present", function(done) { + var options = {}; + elasticSearchClient.search(indexName, qryObj, options, function(err, data) { + should.not.exist(err); data = JSON.parse(data); data.should.not.be.undefined.null.empty; data.hits.total.should.be.gte(0); done(); - }) + }); }); - it("should search even if index_name is not present", function(done){ - var qryObj = { - "query" : { - "term" : { "name" : "sushi" } - } - }; - elasticSearchClient.search(qryObj, function(err, data){ + // This fails + it.skip("should allow options to be passed in when index_name is not present", function(done) { + var options = {}; + elasticSearchClient.search(indexName, objName, qryObj, options, function(err, data) { + should.not.exist(err); + data = JSON.parse(data); + console.log(data); + data.should.not.be.undefined.null.empty; + data.hits.total.should.be.gte(0); + done(); + }); + }); + + it("should search even if collection name not present", function(done) { + elasticSearchClient.search(indexName, qryObj, function(err, data) { + should.not.exist(err); + data = JSON.parse(data); + data.should.not.be.undefined.null.empty; + data.hits.total.should.be.gte(0); + done(); + }); + }); + + it("should search even if index_name is not present", function(done) { + elasticSearchClient.search(qryObj, function(err, data) { + should.not.exist(err); data = JSON.parse(data); data.should.not.be.undefined.null.empty; data.hits.total.should.be.at.least(0); done(); - }) + }); }); }); - describe("#percolate", function(){ - it("should percolate", function(done){ + describe("#percolate", function() { + it("should percolate", function(done) { var doc = { "doc" : { "field1" : "value1" } - } + }; elasticSearchClient.percolate(indexName, objName, doc) .on('data', @@ -265,8 +312,8 @@ describe("ElasticSearchClient Core api", function(){ }); }); - describe("#percolator", function(){ - it("should be a percolator", function(done){ + describe("#percolator", function() { + it("should be a percolator", function(done) { var qryObj = { query: { bool: { @@ -291,15 +338,17 @@ describe("ElasticSearchClient Core api", function(){ }); }); - describe("#bulk", function(){ + describe("#bulk", function() { var cmdArr = [ - {index:{_index:indexName,_type:objName,_id:'bulkedsushi'}}, - {name:'anothersushi'} + {index: {_index: indexName, _type: objName, _id: 'bulkedsushi'}}, + {name: 'anothersushi'} ]; + var cmdArrNoIndexAndNoType = [ - {index:{_id:'bulkedsushi'}}, - {name:'anothersushi'} + {index: {_id: 'bulkedsushi'}}, + {name: 'anothersushi'} ]; + var check = function(data, done) { data = JSON.parse(data); data.items.should.be.ok; @@ -307,40 +356,45 @@ describe("ElasticSearchClient Core api", function(){ data.items[0].index.should.be.ok; done(); }; + it("should index via bulk canonical", function(done) { - elasticSearchClient.bulk(cmdArr, function(err,data){ - check(data,done); + elasticSearchClient.bulk(cmdArr, function(err, data) { + check(data, done); }); }); + it("should index via bulk canonical even if options are passed", function(done) { - elasticSearchClient.bulk(cmdArr, {}, function(err,data){ - check(data,done); + elasticSearchClient.bulk(cmdArr, {}, function(err, data) { + check(data, done); }); }); + it("should index via bulk event style", function(done) { elasticSearchClient.bulk(cmdArr) - .on('data', function(data){ - check(data,done); + .on('data', function(data) { + check(data, done); }) .exec(); }); + it("should index via bulk event style pass index and type via the options", function(done) { elasticSearchClient.bulk(cmdArrNoIndexAndNoType, { _index: indexName, _type: objName}) - .on('data', function(data){ - check(data,done); + .on('data', function(data) { + check(data, done); }) .exec(); }); + it("should index via bulk canonical pass index and type via the options", function(done) { - elasticSearchClient.bulk(cmdArrNoIndexAndNoType, { _index: indexName, _type: objName}, function(err,data){ - check(data,done); + elasticSearchClient.bulk(cmdArrNoIndexAndNoType, {_index: indexName, _type: objName}, function(err, data) { + check(data, done); }); }); }); - - describe("#count", function(){ - it("should fetch count of given query", function(done){ - var qryStr = 'name:name' + + describe("#count", function() { + it("should fetch count of given query", function(done) { + var qryStr = 'name:name'; elasticSearchClient.count(indexName, objName, qryStr) .on('data', function(data) { data = JSON.parse(data); @@ -350,11 +404,11 @@ describe("ElasticSearchClient Core api", function(){ }) .exec(); }); - }); + }); - describe('#moreLikeThis', function(){ - it('should show results more like this', function(done){ - elasticSearchClient.moreLikeThis(indexName, objName, '1111',{}) + describe('#moreLikeThis', function() { + it('should show results more like this', function(done) { + elasticSearchClient.moreLikeThis(indexName, objName, '1111', {}) .on('data', function(data) { data = JSON.parse(data); data.should.not.be.null.undefined.empty; @@ -365,8 +419,8 @@ describe("ElasticSearchClient Core api", function(){ }); }); - describe("#deleteDocument", function(){ - it("should delete the row by id", function(done){ + describe("#deleteDocument", function() { + it("should delete the row by id", function(done) { elasticSearchClient.deleteDocument(indexName, objName, 1111) .on('data', function(data) { data = JSON.parse(data); @@ -375,17 +429,41 @@ describe("ElasticSearchClient Core api", function(){ data._id.should.equal("1111"); done(); }) - .exec() + .exec(); + }); + }); + + describe("#deleteDocument canonical", function() { + it("should delete the row by id", function(done) { + elasticSearchClient.deleteDocument(indexName, objName, 1111, function(err, data) { + should.not.exist(err); + data = JSON.parse(data); + data.ok.should.be.ok; + data.found.should.exist; + data._id.should.equal("1111"); + done(); + }) + }); + + it("should allow options to be passed in", function(done) { + elasticSearchClient.deleteDocument(indexName, objName, 1111, {}, function(err, data) { + should.not.exist(err); + data = JSON.parse(data); + data.ok.should.be.ok; + data.found.should.exist; + data._id.should.equal("1111"); + done(); + }) }); - }); + }); - describe('#deleteByQuery', function(){ - it('should delete objects matching given query', function(done){ + describe('#deleteByQuery', function() { + it('should delete objects matching given query', function(done) { var qryObj = { term : { name: 'name' } - } + }; elasticSearchClient.deleteByQuery(indexName, objName, qryObj) .on('data', function(data) { data = JSON.parse(data); diff --git a/test/indices.test.js b/test/indices.test.js index dbc8bad..3422119 100644 --- a/test/indices.test.js +++ b/test/indices.test.js @@ -11,32 +11,30 @@ var serverOptions = { }*/ }; - var indexName = 'your_index_name'; var objName = 'your_object_name'; -var testIndex = "your_test_index_name" +var testIndex = "your_test_index_name"; var elasticSearchClient = new ElasticSearchClient(serverOptions); - -var tweet = {"tweet" : { - "properties" : { - "message" : {"type" : "string", "store" : "yes"} +var tweet = { + tweet: { + properties: { + message: {type: "string", store: "yes"} } } }; var template = { - "template" : "te*", - "settings" : { - "number_of_shards" : 1 - } - }; - + template: "te*", + settings: { + number_of_shards : 1 + } +}; -describe("ElasticSearchClient indices api", function(){ - before(function(done){ +describe("ElasticSearchClient indices api", function() { + before(function(done) { elasticSearchClient.index(indexName, objName, {"id": "sushi", name: "sushi"}) .on('data', function(data) { done(); @@ -44,16 +42,16 @@ describe("ElasticSearchClient indices api", function(){ .exec(); }); - describe("#createIndex", function(){ - it("should create an index", function(done){ + describe("#createIndex", function() { + it("should create an index", function(done) { elasticSearchClient.createIndex(testIndex) .on('data', function(data) { data = JSON.parse(data); data.should.be.ok; - if(data.error){ + if (data.error) { data.error.should.contain("IndexAlreadyExistsException"); data.status.should.equal(400); - }else{ + } else { data.ok.should.be.ok; data.acknowledged.should.be.ok; } @@ -63,35 +61,35 @@ describe("ElasticSearchClient indices api", function(){ }); }); - describe("#deleteIndex @slow", function(){ - it("should delete index", function(done){ + describe("#deleteIndex @slow", function() { + it("should delete index", function(done) { elasticSearchClient.deleteIndex(testIndex) .on('data', function(data) { data = JSON.parse(data); data.should.be.ok; - if(data.error){ + if (data.error) { data.error.should.contain("IndexMissingException"); - data.status.should.equal(404) - }else{ + data.status.should.equal(404); + } else { data.ok.should.be.ok; data.acknowledged.should.be.ok; } - done() + done(); }) .exec(); }); }); - describe("Aliases", function(){ - it("should alias", function(done){ + describe("Aliases", function() { + it("should alias", function(done) { var aliases = { "actions": [ { "add" : { "index" : "1", "alias" : "alias1" } } ] - } + }; elasticSearchClient.createIndex(indexName, aliases) .on('data', function(data) { // console.log(data); @@ -105,11 +103,11 @@ describe("ElasticSearchClient indices api", function(){ }); - describe("#analyze", function(){ - it("should analyze", function(done){ - elasticSearchClient.analyze(indexName,'this is a ') + describe("#analyze", function() { + it("should analyze", function(done) { + elasticSearchClient.analyze(indexName, 'this is a ') .on('data', function(data) { - console.log(data) + console.log(data); data = JSON.parse(data); data.should.be.ok; done(); @@ -118,8 +116,8 @@ describe("ElasticSearchClient indices api", function(){ }); }); - describe("#openIndex", function(){ - it("should open the given index", function(done){ + describe("#openIndex", function() { + it("should open the given index", function(done) { elasticSearchClient.openIndex(indexName) .on('data', function(data) { data = JSON.parse(data); @@ -131,9 +129,8 @@ describe("ElasticSearchClient indices api", function(){ }); }); - - describe("#getSettings", function(){ - it("should get settings", function(done){ + describe("#getSettings", function() { + it("should get settings", function(done) { elasticSearchClient.getSettings(indexName) .on('data', function(data) { data = JSON.parse(data); @@ -145,100 +142,109 @@ describe("ElasticSearchClient indices api", function(){ }); }); - - describe("#putMapping", function(){ + describe("#putMapping", function() { var check = function(err, data, done) { data = JSON.parse(data); data.ok.should.be.ok; data.acknowledged.should.be.true; done(err); }; + var checkError = function(err, data, done) { data = JSON.parse(data); data.error.should.be.a('string'); data.status.should.eql(500); done(err); }; - it("should put mappings one object one index", function(done){ + + it("should put mappings one object one index", function(done) { elasticSearchClient.putMapping(indexName, objName, tweet) .on('data', function(data) { check(null, data, done); }) .exec(); }); - it("should put mappings one object one index canonical style no options", function(done){ + + it("should put mappings one object one index canonical style no options", function(done) { elasticSearchClient.putMapping(indexName, objName, tweet, function(err, data) { check(err, data, done); }); }); - it("should put mappings one object one index canonical style with options", function(done){ + + it("should put mappings one object one index canonical style with options", function(done) { elasticSearchClient.putMapping(indexName, objName, tweet, {}, function(err, data) { check(err, data, done); }); }); - it("should fail to put a mapping if the typeName is unspecified", function(done){ + + it("should fail to put a mapping if the typeName is unspecified", function(done) { elasticSearchClient.putMapping(indexName, null, { does_nothing: 'no matter'}) .on('data', function(data) { checkError(null, data, done); }) .exec(); }); - it("should fail to put a mapping if the typeName is unspecified canonical style", function(done){ + + it("should fail to put a mapping if the typeName is unspecified canonical style", function(done) { elasticSearchClient.putMapping(indexName, null, { does_nothing: 'no matter'}, function(err, data) { checkError(null, data, done); }); }); - it("should fail to put a mapping if the typeName is unspecified canonical style with options", function(done){ + + it("should fail to put a mapping if the typeName is unspecified canonical style with options", function(done) { elasticSearchClient.putMapping(indexName, null, { does_nothing: 'no matter'}, { pretty: true }, function(err, data) { checkError(null, data, done); }); }); }); - - describe("#getMapping", function(){ + describe("#getMapping", function() { var check = function(data, propertyToCheck, done) { data = JSON.parse(data); data.should.be.ok; data[propertyToCheck].should.exist; done(null, data); }; - it("should get mappings for one object in one index", function(done){ + + it("should get mappings for one object in one index", function(done) { elasticSearchClient.getMapping(indexName, objName) .on('data', function(data) { check(data, objName, done); }) .exec(); }); - it("should get mappings for all objects in one index", function(done){ + + it("should get mappings for all objects in one index", function(done) { elasticSearchClient.getMapping(indexName, null) .on('data', function(data) { check(data, indexName, done); }) .exec(); }); - it("should get mappings for all indexes", function(done){ + + it("should get mappings for all indexes", function(done) { elasticSearchClient.getMapping() .on('data', function(data) { check(data, indexName, done); }) .exec(); }); - it("should get mappings for one object in one index canonical no options", function(done){ + + it("should get mappings for one object in one index canonical no options", function(done) { elasticSearchClient.getMapping(indexName, objName, function(err, data) { - check(data, objName, done); - }); + check(data, objName, done); + }); }); - it("should get mappings for one object in one index canonical with options", function(done){ + + it("should get mappings for one object in one index canonical with options", function(done) { elasticSearchClient.getMapping(indexName, objName, {}, function(err, data) { - check(data, objName, done); - }); + check(data, objName, done); + }); }); }); - - describe("#refresh", function(){ - it("should refresh", function(done){ + describe("#refresh", function() { + it("should refresh", function(done) { elasticSearchClient.refresh(indexName) .on('data', function(data) { data = JSON.parse(data); @@ -249,9 +255,8 @@ describe("ElasticSearchClient indices api", function(){ }); }); - - describe("#optimize @slow", function(){ - it("should optimize", function(done){ + describe("#optimize @slow", function() { + it("should optimize", function(done) { elasticSearchClient.optimize(indexName) .on('data', function(data) { // console.log(data) @@ -263,9 +268,8 @@ describe("ElasticSearchClient indices api", function(){ }); }); - - describe("#flush", function(){ - it("should flush", function(done){ + describe("#flush", function() { + it("should flush", function(done) { elasticSearchClient.flush(indexName) .on('data', function(data) { // console.log(data); @@ -277,9 +281,8 @@ describe("ElasticSearchClient indices api", function(){ }); }); - - describe("#snapShot", function(){ - it("should take a snapshot", function(done){ + describe("#snapShot", function() { + it("should take a snapshot", function(done) { elasticSearchClient.snapshot(indexName) .on('data', function(data) { // console.log(data) @@ -291,8 +294,8 @@ describe("ElasticSearchClient indices api", function(){ }); }); - describe("#defineTemplate", function(){ - it("should define a template", function(done){ + describe("#defineTemplate", function() { + it("should define a template", function(done) { elasticSearchClient.defineTemplate('Template', template) .on('data', function(data) { // console.log(data) @@ -304,9 +307,8 @@ describe("ElasticSearchClient indices api", function(){ }); }); - - describe("#getTemplate", function(){ - it("should get a template", function(done){ + describe("#getTemplate", function() { + it("should get a template", function(done) { elasticSearchClient.getTemplate('Template', template) .on('data', function(data) { // console.log(data) @@ -318,9 +320,8 @@ describe("ElasticSearchClient indices api", function(){ }); }); - - describe("#deleteTemplate", function(){ - it("should delete a template", function(done){ + describe("#deleteTemplate", function() { + it("should delete a template", function(done) { elasticSearchClient.deleteTemplate('Template', template) .on('data', function(data) { // console.log(data) @@ -332,9 +333,8 @@ describe("ElasticSearchClient indices api", function(){ }); }); - - describe("#status", function(){ - it("should provide status", function(done){ + describe("#status", function() { + it("should provide status", function(done) { elasticSearchClient.status(indexName) .on('data', function(data) { // console.log(data) @@ -346,9 +346,8 @@ describe("ElasticSearchClient indices api", function(){ }); }); - - describe("#stats", function(){ - it("should provide stats", function(done){ + describe("#stats", function() { + it("should provide stats", function(done) { elasticSearchClient.stats(indexName) .on('data', function(data) { data = JSON.parse(data); @@ -359,9 +358,8 @@ describe("ElasticSearchClient indices api", function(){ }); }); - - describe("#clearCache", function(){ - it("should clear the cache", function(done){ + describe("#clearCache", function() { + it("should clear the cache", function(done) { elasticSearchClient.clearCache(indexName) .on('data', function(data) { data = JSON.parse(data); @@ -372,8 +370,8 @@ describe("ElasticSearchClient indices api", function(){ }); }); - describe("deleteMapping#", function(){ - it("should delete mappings", function(done){ + describe("deleteMapping#", function() { + it("should delete mappings", function(done) { elasticSearchClient.deleteMapping(indexName, objName) .on('data', function(data) { data = JSON.parse(data); @@ -384,14 +382,13 @@ describe("ElasticSearchClient indices api", function(){ }); }); - - describe("#updateSettings", function(){ - it("should update settings", function(done){ + describe("#updateSettings", function() { + it("should update settings", function(done) { var settings = { "index" : { "number_of_replicas" : 4 } - } + }; elasticSearchClient.updateSettings(indexName, settings) .on('data', function(data) { data = JSON.parse(data); @@ -403,9 +400,8 @@ describe("ElasticSearchClient indices api", function(){ }); }); - - describe("#getSegments", function(){ - it("should get segments", function(done){ + describe("#getSegments", function() { + it("should get segments", function(done) { elasticSearchClient.getSegments(indexName) .on('data', function(data) { data = JSON.parse(data); @@ -417,9 +413,8 @@ describe("ElasticSearchClient indices api", function(){ }); }); - - describe("#closeIndex", function(){ - it("should close the given index", function(done){ + describe("#closeIndex", function() { + it("should close the given index", function(done) { elasticSearchClient.closeIndex(indexName) .on('data', function(data) { data = JSON.parse(data); @@ -431,8 +426,7 @@ describe("ElasticSearchClient indices api", function(){ }); }); - - after(function(done){ + after(function(done) { elasticSearchClient.deleteIndex(indexName) .on('data', function(data) { done(); diff --git a/test/twitter.test.js b/test/twitter.test.js index f163001..197858e 100644 --- a/test/twitter.test.js +++ b/test/twitter.test.js @@ -4,29 +4,29 @@ var assert = require('assert') , elasticSearchClient, conf, serverOptions, riverData; var indexName = 'your_index_name'; -var objTypeName = 'status' -var riverName = 'ma_twitter_river' +var objTypeName = 'status'; +var riverName = 'ma_twitter_river'; //Number of tweets to collect before indexing -var bulkSize = 50 +var bulkSize = 50; -try{ +try { conf = require('./conf').test; - + serverOptions = conf.es_server_options; elasticSearchClient = new ElasticSearchClient(serverOptions); riverData = { //This is the river type not the object type - "type":"twitter", - "twitter":conf.twitter, - "index":{ - "index":indexName, - "type":objTypeName, - "bulk_size":bulkSize + "type": "twitter", + "twitter": conf.twitter, + "index": { + "index": indexName, + "type": objTypeName, + "bulk_size": bulkSize } }; -}catch(e){ +} catch (e) { console.log(e); console.log(" \n\ *****************************************************************\n \ @@ -37,36 +37,36 @@ try{ "); } -describe('twitter api', function(){ - describe('#createOrModifyTwitterRiver', function(){ - it('should create/modify the twitter river', function(done){ +describe('twitter api', function() { + describe('#createOrModifyTwitterRiver', function() { + it('should create/modify the twitter river', function(done) { - if(!conf || !elasticSearchClient) return done(); + if (!conf || !elasticSearchClient) return done(); elasticSearchClient.createOrModifyTwitterRiver(riverName, riverData) - .on('data', function (data) { + .on('data', function(data) { data = JSON.parse(data); data.ok.should.be.ok; done(); }) - .on('error', function (error) { + .on('error', function(error) { - }).exec() + }).exec(); }); }); - describe('#deleteTwitterRiver', function(){ - it('should delete the twitter river with given name', function(done){ + describe('#deleteTwitterRiver', function() { + it('should delete the twitter river with given name', function(done) { - if(!conf || !elasticSearchClient) return done(); + if (!conf || !elasticSearchClient) return done(); elasticSearchClient.deleteTwitterRiver(riverName) - .on('data', function (data) { + .on('data', function(data) { data = JSON.parse(data); data.ok.should.be.ok; done(); }) - .on('error', function (error) { + .on('error', function(error) { }).exec(); });