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
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ var client = new SolrNode({
protocol: 'http'
});

// or

var client = new SolrNode('http://127.0.0.1:8983/solr/test');

// Set logger level (can be set to DEBUG, INFO, WARN, ERROR, FATAL or OFF)
require('log4js').getLogger('solr-node').level = 'DEBUG';
```
Expand Down
2 changes: 1 addition & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
declare module 'solr-node' {
class Client {
constructor(options: Client.ClientOptions);
constructor(options: Client.ClientOptions | string);

query(): Client.Query;

Expand Down
42 changes: 20 additions & 22 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,25 @@ var _ = require('underscore'),
*
* @constructor
*
* @param {Object} options
* @param {String|Object} options - an URL to Solr server core - example: 'http://127.0.0.1:8983/solr/test'
* @param {String} [options.host] - host address of Solr server
* @param {Number|String} [options.port] - port number of Solr server
* @param {String} [options.core] - client core name
* @param {String} [options.rootPath] - solr root path
* @param {String} [options.protocol] - request protocol ('http'|'https')
*/
function Client(options) {
this.options = {
host: options.host || '127.0.0.1',
port: options.port || '8983',
core: options.core || '',
rootPath: options.rootPath || 'solr',
protocol: options.protocol || 'http'
};
if (_.isObject(options)) {
var host = options.host || '127.0.0.1';
var port = options.port || '8983';
var core = options.core || '';
var rootPath = options.rootPath || 'solr';
var protocol = options.protocol || 'http';

this.solrEndpointUrl = this._makeHostUrl(protocol, host, port) + '/' + rootPath+ '/' + core;
} else {
this.solrEndpointUrl = options || '';
}

// Path Constants List
this.SEARCH_PATH = 'select';
Expand Down Expand Up @@ -103,7 +107,7 @@ Client.prototype._callSolrServer = function(requestFullPath, fetchOptions, final
* @returns {undefined|Promise} - When there's no callback function it returns a Promise
*/
Client.prototype._requestGet = function(path, query, finalCallback) {
var params, options, requestPrefixUrl, requestFullPath;
var params, options, requestUrl;

if (query instanceof Query) {
params = query.toString();
Expand All @@ -112,12 +116,9 @@ Client.prototype._requestGet = function(path, query, finalCallback) {
} else {
params = 'q=*:*';
}
requestPrefixUrl = this._makeHostUrl(this.options.protocol, this.options.host, this.options.port);
requestPrefixUrl += '/' + [this.options.rootPath, this.options.core, path].join('/');
requestUrl = this.solrEndpointUrl + '/' + path + '?' + params;

requestFullPath = requestPrefixUrl + '?' + params;

logger.debug('[_requestGet] requestFullPath: ', requestFullPath);
logger.debug('[_requestGet] requestUrl: ', requestUrl);

options = {
method: 'GET',
Expand All @@ -126,7 +127,7 @@ Client.prototype._requestGet = function(path, query, finalCallback) {
}
};

return this._callSolrServer(requestFullPath, options, finalCallback);
return this._callSolrServer(requestUrl, options, finalCallback);
};

/**
Expand All @@ -140,7 +141,7 @@ Client.prototype._requestGet = function(path, query, finalCallback) {
* @returns {undefined|Promise} - When there's no callback function it returns a Promise
*/
Client.prototype._requestPost = function(path, data, urlOptions, finalCallback) {
var params, options, requestPrefixUrl, requestFullPath;
var params, options, requestUrl;

if (_.isString(urlOptions)) {
params = urlOptions;
Expand All @@ -150,12 +151,9 @@ Client.prototype._requestPost = function(path, data, urlOptions, finalCallback)
params = '';
}

requestPrefixUrl = this._makeHostUrl(this.options.protocol, this.options.host, this.options.port);
requestPrefixUrl += '/' + [this.options.rootPath, this.options.core, path].join('/');

requestFullPath = requestPrefixUrl + '?' + params;
requestUrl = this.solrEndpointUrl + '/' + path + '?' + params;

logger.debug('[_requestPost] requestFullPath: ', requestFullPath);
logger.debug('[_requestPost] requestUrl: ', requestUrl);
logger.debug('[_requestPost] data: ', data);

options = {
Expand All @@ -167,7 +165,7 @@ Client.prototype._requestPost = function(path, data, urlOptions, finalCallback)
}
};

return this._callSolrServer(requestFullPath, options, finalCallback);
return this._callSolrServer(requestUrl, options, finalCallback);
};

/**
Expand Down
25 changes: 11 additions & 14 deletions test/client-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,13 +166,7 @@ describe('Client', function() {
//when
var client = new Client(options);
//then
expect(client.options).to.eql({
host: '127.0.0.1',
port: '8983',
core: '',
rootPath: 'solr',
protocol: 'http'
});
expect(client.solrEndpointUrl).to.eql('http://127.0.0.1:8983/solr/');
});

it('should create client when core:"test".', function() {
Expand All @@ -183,13 +177,16 @@ describe('Client', function() {
//when
var client = new Client(options);
//then
expect(client.options).to.eql({
host: '127.0.0.1',
port: '8983',
core: 'test',
rootPath: 'solr',
protocol: 'http'
});
expect(client.solrEndpointUrl).to.eql('http://127.0.0.1:8983/solr/test');
});

it('should create client when solr endpoint given as a string', function() {
//given
var options = 'http://test.com/solr/test';
//when
var client = new Client(options);
//then
expect(client.solrEndpointUrl).to.eql('http://test.com/solr/test');
});
});

Expand Down