Skip to content

Commit

Permalink
Merge pull request #359 from hotwax/358_facility_filter_enhancement
Browse files Browse the repository at this point in the history
Improved: Escaped special characters while building facility filter in Solr search query (358).
  • Loading branch information
ravilodhi authored Nov 23, 2023
2 parents 8c4598e + 4aac409 commit 91bad3c
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/utils/solrHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,10 @@ const prepareOrderQuery = (params: any) => {

if (Array.isArray(filterValue)) {
const filterOperator = params.filters[key].op ? params.filters[key].op : 'OR' ;
payload.json.filter += ` AND ${key}: (${filterValue.join(' ' + filterOperator + ' ')})`
const escapedFilterValues = filterValue.map(value => escapeSolrSpecialChars(value));
payload.json.filter += ` AND ${key}: (${escapedFilterValues.join(' ' + filterOperator + ' ')})`
} else {
payload.json.filter += ` AND ${key}: ${filterValue}`
payload.json.filter += ` AND ${key}: ${escapeSolrSpecialChars(filterValue)}`
}
})
}
Expand All @@ -47,4 +48,12 @@ const prepareOrderQuery = (params: any) => {
return payload
}

export { prepareOrderQuery }
const escapeSolrSpecialChars = (input: any) => {
const specialChars = ['\\', '+', '-', '&&', '||', '!', '(', ')', '{', '}', '[', ']', '^', '"', '~', '*', '?', ':'];

// Escape each special character in the input
const escapedInput = String(input).replace(new RegExp(`[${specialChars.join('\\')}]`, 'g'), '\\$&');
return escapedInput;
}

export { escapeSolrSpecialChars, prepareOrderQuery }

0 comments on commit 91bad3c

Please sign in to comment.