Skip to content

Commit

Permalink
escaping chars in suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
schatzopoulos committed Nov 4, 2016
1 parent 2e74740 commit 01444c4
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
2 changes: 1 addition & 1 deletion application/solr/solrClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ module.exports = {
uri: requestUri,
method: 'GET'
}, (err, response, body) => {

console.log(JSON.stringify(response));
let solrResponse = {};

if(err){
Expand Down
22 changes: 17 additions & 5 deletions application/solr/suggestions.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
'use strict';

const solrClient = require('./solrClient');

function escapeSpecialChars(s){
return s.replace(/([\+\-!\(\)\{\}\[\]\^"~\*\?:\\])/g, (match) => {
return '\\' + match;
})
.replace(/&&/g, '\\&\\&')
.replace(/\|\|/g, '\\|\\|')
.replace(/'/g, '');
}

function mergeAndSortFacets(facetResults, limit){

Expand All @@ -21,6 +28,10 @@ function mergeAndSortFacets(facetResults, limit){
// sort based on frequency
let sortedResults = [];
for(let key in mergedResults){
// bypass terms with zero score
if(mergedResults[key] === 0)
continue;

sortedResults.push({
key: key,
value: mergedResults[key]
Expand All @@ -36,8 +47,9 @@ module.exports = {
// finds users
findUsers: function(q){
let promise = new Promise( (resolve, reject) => {
console.log(q);
let queryString = 'defType=edismax' +
'&q=' + q + '*' +
'&q=' + escapeSpecialChars(q) + '*' +
'&fq=kind:user' +
'&qf=username^10 surname forename email organization' + //boost username match
'&fl=_id username' +
Expand All @@ -58,19 +70,20 @@ module.exports = {
findKeywords: function(q){
let promise = new Promise( (resolve, reject) => {

let allKeywords = q.split(' ');
let allKeywords = decodeURIComponent(q).replace(/ +/g, ' ').split(' '); //trim multiple spaces and split
let curKeyword = allKeywords[allKeywords.length - 1];

let allExceptCurrent = [];
let prefix = '';
let paramQ = '*:*';
if(allKeywords.length > 1){
allExceptCurrent = allKeywords.slice(0, allKeywords.length-1);
prefix = allExceptCurrent.join(' ') + ' ';
let fieldFilter = '(' + allExceptCurrent.join(' AND ') + ')';
paramQ = 'title:' + fieldFilter +
' OR description:' + fieldFilter +
' OR content:' + fieldFilter;
let index = q.lastIndexOf(' ');
prefix = q.substring(0, index) + ' ';
}

let queryString =
Expand All @@ -93,7 +106,6 @@ module.exports = {

resolve({
numFound: docs.length,
start: 0,
docs: docs
});
}).catch( (err) => {
Expand Down

0 comments on commit 01444c4

Please sign in to comment.