flora-solr
Advanced tools
Comparing version 0.3.1 to 0.6.0
112
index.js
@@ -5,2 +5,3 @@ 'use strict'; | ||
var url = require('url'); | ||
var querystring = require('querystring'); | ||
@@ -12,2 +13,4 @@ var _ = require('lodash'); | ||
var SUPPORTED_FILTERS = ['equal', 'notEqual', 'lessOrEqual', 'greaterOrEqual', 'range']; | ||
/** | ||
@@ -20,7 +23,2 @@ * @constructor | ||
this.options = config; | ||
// create config object for http.request method | ||
this.options.servers = _.mapValues(config.servers, function (server) { | ||
return _.assign(url.parse(server.url), { method: 'POST', headers: {} }); | ||
}); | ||
}; | ||
@@ -38,21 +36,18 @@ | ||
DataSource.prototype.process = function (request, callback) { | ||
var options, | ||
requestOpts, | ||
var requestUrl, | ||
server = request.server || 'default', | ||
params = ['wt=json'], | ||
queryString = '*:*', | ||
filters = []; | ||
params = { wt: 'json' }, | ||
queryParts = []; | ||
if (!this.options.servers[server]) return callback(new Error('Server "' + server + '" not defined')); | ||
options = this.options.servers[server]; | ||
requestOpts = _.assign({}, options, { path: options.pathname + request.collection + '/select' }); | ||
requestUrl = this.options.servers[server].url + request.collection + '/select'; | ||
if (request.attributes) params.push('fl=' + request.attributes.join(',')); | ||
if (request.search) filters.push(escapeSpecialChars(request.search)); | ||
if (request.order) params.push('sort=' + encodeURIComponent(buildSolrOrderString(request.order))); | ||
if (request.attributes) params.fl = request.attributes.join(','); | ||
if (request.order) params.sort= buildSolrOrderString(request.order); | ||
if (request.search) queryParts.push(escapeValueForSolr(request.search)); | ||
if (request.filter) { | ||
try { | ||
filters.push(buildSolrFilterString(request.filter)); | ||
queryParts.push(buildSolrFilterString(request.filter)); | ||
} catch (e) { | ||
@@ -62,17 +57,27 @@ return callback(e); | ||
} | ||
if (request.queryAddition) queryParts.push(prepareQueryAddition(request.queryAddition)); | ||
if (queryParts.length === 0) queryParts.push('*:*'); | ||
if (filters.length) queryString = encodeURIComponent(filters.join(' AND ')); | ||
if (!request.limit) request.limit = 1000000; // overwrite SOLR default limit for sub-resource processing | ||
if (request.page) params.push('start=' + (request.page - 1) * request.limit); | ||
params.push('rows=' + request.limit); | ||
if (request.page) params.start = (request.page - 1) * request.limit; | ||
params.push('q=' + queryString); | ||
if (!request.limitPer) params.rows = request.limit; | ||
else { | ||
params = _.assign(params, { | ||
group: 'true', | ||
'group.format': 'simple', | ||
'group.main': 'true', | ||
'group.field': request.limitPer, | ||
'group.limit': request.limit | ||
}); | ||
} | ||
params.q = queryParts.join(' AND '); | ||
if (request._explain) { | ||
request._explain.href = requestOpts.href; | ||
request._explain.url = requestUrl; | ||
request._explain.params = params; | ||
} | ||
querySolr(requestOpts, params, callback); | ||
querySolr(requestUrl, params, callback); | ||
}; | ||
@@ -88,2 +93,8 @@ | ||
/** | ||
* @param {string} value | ||
*/ | ||
DataSource.prototype.escape = escapeValueForSolr; | ||
function buildSolrFilterString(floraFilters) { | ||
@@ -99,3 +110,5 @@ var orConditions; | ||
return orConditions.join(' OR '); | ||
if (orConditions.length > 1) return '(' + orConditions.join(' OR ') + ')'; | ||
return orConditions.join(''); | ||
} | ||
@@ -174,8 +187,8 @@ | ||
if (filter.operator === 'less' || filter.operator === 'greater') { | ||
if (SUPPORTED_FILTERS.indexOf(filter.operator) === -1) { | ||
throw new ImplementationError('DataSource "flora-solr" does not support "' + filter.operator + '" filters'); | ||
} | ||
if (! Array.isArray(filter.attribute)) { | ||
value = prepareValueForSolr(value); | ||
if (!Array.isArray(filter.attribute)) { | ||
value = escapeValueForSolr(value); | ||
if (Array.isArray(value)) { | ||
@@ -186,6 +199,9 @@ if (operator === 'range') value = '[' + value[0] + ' TO ' + value[1] + ']'; | ||
if (operator === 'greaterOrEqual') value = '[' + value + ' TO *]'; | ||
if (operator === 'lessOrEqual') value = '[* TO ' + value + ']'; | ||
return filter.attribute + ':' + value; | ||
if (operator !== 'notEqual') { | ||
if (operator === 'greaterOrEqual') value = '[' + value + ' TO *]'; | ||
if (operator === 'lessOrEqual') value = '[* TO ' + value + ']'; | ||
return filter.attribute + ':' + value; | ||
} else { | ||
return '-' + filter.attribute + ':' + value; | ||
} | ||
} else { // convert composite keys to SOLR syntax | ||
@@ -195,3 +211,3 @@ return value | ||
var conditions = values.map(function (val, index) { | ||
return filter.attribute[index] + ':' + prepareValueForSolr(val); | ||
return filter.attribute[index] + ':' + escapeValueForSolr(val); | ||
}); | ||
@@ -211,3 +227,3 @@ return '(' + conditions.join(' AND ') + ')'; | ||
*/ | ||
function prepareValueForSolr(value) { | ||
function escapeValueForSolr(value) { | ||
if (typeof value === 'string') value = escapeSpecialChars(value); | ||
@@ -249,19 +265,28 @@ else if (typeof value === 'boolean') value = value === false ? 0 : 1; | ||
function prepareQueryAddition(queryAdditions) { | ||
return queryAdditions | ||
.replace(/[\r\n]+/g, ' ') | ||
.replace(/\s{2,}/g, ' ') | ||
.trim(); | ||
} | ||
/** | ||
* | ||
* @param {Object} options | ||
* @param {Array.<string>} params | ||
* @param {Object} params | ||
* @param {Function} callback | ||
* @private | ||
*/ | ||
function querySolr(options, params, callback) { | ||
var req; | ||
function querySolr(requestUrl, params, callback) { | ||
var options = url.parse(requestUrl); | ||
options.method = 'POST'; | ||
options.headers = { | ||
'Content-Type': 'application/x-www-form-urlencoded' | ||
}; | ||
options.headers['Content-Type'] = 'application/x-www-form-urlencoded'; | ||
var req = http.request(options, function processSolrReponse(res) { | ||
var chunks = []; | ||
req = http.request(options, function processSolrReponse(res) { | ||
var str = ''; | ||
res.on('data', function (chunk) { | ||
str += chunk; | ||
chunks.push(chunk); | ||
}); | ||
@@ -272,3 +297,3 @@ | ||
data = parseData(str); | ||
data = parseData(Buffer.concat(chunks).toString('utf8')); | ||
if (res.statusCode >= 400 || data instanceof Error) { | ||
@@ -291,5 +316,6 @@ if (!(data instanceof Error)) { | ||
req.write(querystring.stringify(params)); // add params to POST body | ||
req.on('error', callback); | ||
req.write(params.join('&')); | ||
req.end(); | ||
} |
{ | ||
"name": "flora-solr", | ||
"version": "0.3.1", | ||
"version": "0.6.0", | ||
"description": "Solr connection for Flora", | ||
@@ -34,7 +34,6 @@ "main": "index.js", | ||
"engines": { | ||
"node": ">=0.10.0", | ||
"iojs": ">=2.0" | ||
"node": ">=0.10.0" | ||
}, | ||
"dependencies": { | ||
"flora-errors": "^0.2.0", | ||
"flora-errors": "^0.6.0", | ||
"lodash": "^4.0.0" | ||
@@ -44,7 +43,7 @@ }, | ||
"chai": "^3.4.1", | ||
"eslint": "^1.10.3", | ||
"eslint": "^2.3.0", | ||
"grunt": "^0.4.5", | ||
"grunt-cli": "^0.1.13", | ||
"grunt-contrib-clean": "^0.7.0", | ||
"grunt-eslint": "^17.3.1", | ||
"grunt-contrib-clean": "^1.0.0", | ||
"grunt-eslint": "^18.0.0", | ||
"grunt-mocha-istanbul": "^3.0.1", | ||
@@ -56,5 +55,5 @@ "grunt-mocha-test": "^0.12.7", | ||
"mocha-bamboo-reporter": "^1.1.0", | ||
"nock": "5.2.x", | ||
"nock": "7.2.x", | ||
"when": "^3.7.7" | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
13900
300
2
+ Addedflora-errors@0.6.0(transitive)
- Removedflora-errors@0.2.0(transitive)
Updatedflora-errors@^0.6.0