Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
141 changes: 76 additions & 65 deletions lib/elasticsearchclient/calls/core.js
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -11,7 +11,7 @@ ElasticSearchClient.prototype.index = function(indexName, typeName, document, id

var path = '/' + indexName + '/' + typeName;
var qs = '';
var method = 'POST'
var method = 'POST';



Expand All @@ -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) {
Expand All @@ -48,15 +48,15 @@ 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) {
path += "?" + qs;
}

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.
Expand All @@ -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
Expand All @@ -86,40 +86,45 @@ ElasticSearchClient.prototype.search = function(indexName, typeName, queryObj, o
arguments[arguments.length-1]=false;
}

var objArgs=[]
var path=''
// Assumes quite a lot about the input
var objArgs=[];
var path='';

// Assumes quite a lot about the input
for(var i=0;i<arguments.length;i++){
switch(typeof arguments[i]){
case 'object':
objArgs.push(arguments[i]);
break;
case 'string':
path+='/'+arguments[i]
path+='/'+arguments[i];
break;
}
}

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;
}
}

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);
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);
}
};

// queryObject, options
// indexName, queryObject, options
Expand All @@ -145,20 +150,23 @@ ElasticSearchClient.prototype.multiget = function(indexName, typeName, documentA

path += '/_mget';
if (objArgs[1]) {
path += "?" + querystring.stringify(objArgs[1]);
var queryfied = querystring.stringify(objArgs[1]);
if (queryfied) {
path += "?" + queryfied;
}
}

var data = {docs: []};

for (var i=0; i<objArgs[0].length; i++) {
for (i=0; i<objArgs[0].length; i++) {
var v = objArgs[0][i];
if (typeof v !== 'object') {
v = {_id: v};
}
data.docs.push(v);
}
return this.createCall({path: path, method: 'GET', data: JSON.stringify(data)}, this.clientOptions, cb);
}
};

ElasticSearchClient.prototype.multisearch = function(commandArray, options, cb) {
//Pull the callback and set it false to not clobber id.
Expand All @@ -170,18 +178,18 @@ ElasticSearchClient.prototype.multisearch = function(commandArray, options, cb)
var path = '/_msearch';
var qs = '';
if (options) {
qs = querystring.stringify(options)
qs = querystring.stringify(options);
}
if (qs.length > 0) {
path += "?" + qs;
}

var commandBuffer=''
var commandBuffer='';
for(var i =0; i<commandArray.length;i++){
commandBuffer+=JSON.stringify(commandArray[i])+'\n'
commandBuffer+=JSON.stringify(commandArray[i])+'\n';
}
return this.createCall({path: path, method: 'POST',data:commandBuffer}, this.clientOptions, cb);
}
};

ElasticSearchClient.prototype.percolator = function(indexName, typeName, queryObj, options, cb) {
//Pull the callback and set it false to not clobber id.
Expand All @@ -193,13 +201,13 @@ ElasticSearchClient.prototype.percolator = function(indexName, typeName, queryOb
var path = '/_percolator/' + indexName + '/' + typeName;
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',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.
Expand All @@ -208,16 +216,16 @@ ElasticSearchClient.prototype.percolate = function(indexName, typeName, doc, opt
arguments[arguments.length-1]=false;
}

var path = '/' + indexName + '/' + typeName+'/_percolate'
var path = '/' + indexName + '/' + typeName+'/_percolate';
var qs = '';
if (options) {
qs = querystring.stringify(options)
qs = querystring.stringify(options);
}
if (qs.length > 0) {
path += "?" + qs;
}
return this.createCall({path: path, method: 'GET',data: JSON.stringify(doc)}, this.clientOptions, cb);
}
};

ElasticSearchClient.prototype.count = function(indexName, typeName, query, options, cb) {
//Pull the callback and set it false to not clobber id.
Expand All @@ -227,20 +235,23 @@ ElasticSearchClient.prototype.count = function(indexName, typeName, query, optio
}

var path = '/' + indexName + '/' + typeName + '/_count';

switch(typeof query){
case 'string':
var qs = '?q=' + querystring.stringify(query);
if (options) {
qs +='&'+ querystring.stringify(options)
}
return this.createCall({path: path+qs, method: 'GET'}, this.clientOptions, cb);
case 'string':
var qs = '?q=' + querystring.stringify(query);
if (options) {
var queryfied = querystring.stringify(options);
if (queryfied.length) {
qs +='&'+ queryfied;
}
}
return this.createCall({path: path+qs, method: 'GET'}, this.clientOptions, cb);
case 'object':
return this.createCall({path : path, method : 'POST', data : JSON.stringify(query)}, this.clientOptions, cb);
return this.createCall({path : path, method : 'POST', data : JSON.stringify(query)}, this.clientOptions, cb);
}

throw "unsupported query type: " + typeof(query);
}
throw "unsupported query type: " + typeof(query);
};


ElasticSearchClient.prototype.bulk = function(commandArray, options, cb) {
Expand All @@ -250,7 +261,7 @@ ElasticSearchClient.prototype.bulk = function(commandArray, options, cb) {
options = null;
}

var path = '/_bulk'
var path = '/_bulk';
var qs = '';
if (options) {
if (options._index) {
Expand All @@ -262,19 +273,19 @@ ElasticSearchClient.prototype.bulk = function(commandArray, options, cb) {
}
delete options._index;
}
qs = querystring.stringify(options)
qs = querystring.stringify(options);
if (qs) {
path += "?" + qs;
}
}
var commandBuffer=''
var commandBuffer='';


for(var i =0; i<commandArray.length;i++){
commandBuffer+=JSON.stringify(commandArray[i])+'\n'
commandBuffer+=JSON.stringify(commandArray[i])+'\n';
}
return this.createCall({path: path, method: 'POST',data:commandBuffer}, this.clientOptions, cb);
}
};


ElasticSearchClient.prototype.deleteByQuery = function(indexName, typeName, queryObj, options, cb) {
Expand All @@ -287,13 +298,13 @@ ElasticSearchClient.prototype.deleteByQuery = function(indexName, typeName, quer
var path = '/' + indexName + '/' + typeName + '/_query';
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',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.
Expand All @@ -305,13 +316,13 @@ ElasticSearchClient.prototype.moreLikeThis = function(indexName, typeName, docum
var path = '/' + indexName + '/' + typeName + '/'+documentId+'/_mlt';
var qs = '';
if (options) {
qs = querystring.stringify(options)
qs = querystring.stringify(options);
}
if (qs.length > 0) {
path += "?" + qs;
}
return this.createCall({path: path, method: 'GET'}, this.clientOptions, cb);
}
};

ElasticSearchClient.prototype.update = function(indexName, typeName, documentId, document, options, cb) {
//Pull the callback and set it false to not clobber id.
Expand All @@ -325,10 +336,10 @@ ElasticSearchClient.prototype.update = function(indexName, typeName, documentId,
var path = '/' + indexName + '/' + typeName + '/'+documentId+'/_update';
var qs = '';
if (options) {
qs = querystring.stringify(options)
qs = querystring.stringify(options);
}
if (qs.length > 0) {
path += "?" + qs;
}
return this.createCall({data: JSON.stringify(document), path: path, method: 'POST'}, this.clientOptions, cb);
}
};
Loading