Skip to content
Open
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
74 changes: 72 additions & 2 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ Client.prototype._callSolrServer = function(requestFullPath, fetchOptions, final
};

/**
* Request get
* Request data (using http get method)
*
* @param {String} path - target path
* @param {Object|String} query - query
Expand Down Expand Up @@ -143,6 +143,60 @@ Client.prototype._requestGet = function(path, query, finalCallback) {
return this._callSolrServer(requestFullPath, options, finalCallback);
};

/**
* Request data (using http post method)
*
* @param {String} path - target path
* @param {Object|String} query - query
* @param {Function} finalCallback - (err, result)
*
* @returns {undefined|Promise} - When there's no callback function it returns a PromiseŰ
*/
Client.prototype._requestGetUsingHttpPost = function(path, query, finalCallback) {
var params, options, requestUrl;

if (query instanceof Query) {
params = query.toString();
} else if (_.isString(query)) {
params = query;
} else {
params = 'q=*:*';
}
requestUrl = this._makeHostUrl(this.options.protocol, this.options.host, this.options.port);
requestUrl += '/' + [this.options.rootPath, this.options.core, path].join('/');

logger.debug('[_requestGetUsingHttpPost] requestUrl: ', requestUrl, ' params: ', params);

options = {
method: 'POST',
body: params,
headers: {
'accept' : 'application/json; charset=utf-8',
'content-type' : 'application/x-www-form-urlencoded; charset=utf-8'
}
};
var result = fetch(requestUrl, options)
.then(function(res) {
if (res.status !== 200) {
logger.error(res);
throw new Error('Solr server error: ' + res.status);
} else {
return res.json();
}
});
if (typeof finalCallback === 'function') {
result
.then(function(json) {
finalCallback(null,json);
})
.catch(function (err) {
finalCallback(err.message);
});
} else {
return result;
}
};

/**
* Request post
*
Expand Down Expand Up @@ -194,7 +248,7 @@ Client.prototype.query = function() {
};

/**
* Search
* Search (using HTTP get method)
*
* @param {Object|String} query
* @param {Function} finalCallback - (err, result)
Expand All @@ -204,6 +258,22 @@ Client.prototype.query = function() {
Client.prototype.search = function(query, finalCallback) {
return this._requestGet(this.SEARCH_PATH, query, finalCallback);
};
/**
* Search (using HTTP post method)
*
* @param {Object|String} query
* @param {Function} finalCallback - (err, result)
*
* @returns {Undefined|Promise}
*/
Client.prototype.searchUsingHttpPost = function(query, finalCallback) {
if (typeof finalCallback === 'function') {
this._requestGetUsingHttpPost(this.SEARCH_PATH, query, finalCallback);
} else {
return this._requestGetUsingHttpPost(this.SEARCH_PATH, query);
}
};

/**
* Terms
*
Expand Down