Merge pull request #103 from lbryio/escape_query

added escape function to escape special characters
This commit is contained in:
Mark 2018-08-14 21:15:40 -04:00 committed by GitHub
commit af2e3e88b9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -42,7 +42,7 @@ function getResults (input) {
'must': { 'must': {
'query_string': { 'query_string': {
'fields': ['channel'], 'fields': ['channel'],
'query' : input.channel, 'query' : getEscapedQuery(input.channel),
}, },
}, },
}, },
@ -74,7 +74,7 @@ function getResults (input) {
}; };
const conTermName = { // Contains search term - Name const conTermName = { // Contains search term - Name
'query_string': { 'query_string': {
'query' : '*' + input.s.trim() + '*', 'query' : '*' + getEscapedQuery(input.s) + '*',
'fields': [ 'fields': [
'name', 'name',
], ],
@ -89,7 +89,7 @@ function getResults (input) {
'should': [ 'should': [
{ // Contains search term in Author, Title, Description { // Contains search term in Author, Title, Description
'query_string': { 'query_string': {
'query' : '*' + input.s.trim() + '*', 'query' : '*' + getEscapedQuery(input.s) + '*',
'fields': [ 'fields': [
'value.stream.metadata.author', 'value.stream.metadata.author',
'value.stream.metadata.title', 'value.stream.metadata.title',
@ -252,6 +252,28 @@ function getStatus () {
}); });
} }
function getEscapedQuery (query) {
let badCharacters = ['+', '-', '&&', '||', '!', '(', ')', '{', '}', '[', ']', '^', '"', '~', '*', '?', ':', '\\'];
let escapedQuery = '';
for (var i = 0; i < query.length; i++) {
let char1 = query.charAt(i);
if (badCharacters.includes(char1)) {
escapedQuery = escapedQuery + '\\' + char1;
} else if (i + 1 <= query.length) {
let char2 = query.charAt(i + 1);
if (badCharacters.includes(char1 + char2)) {
escapedQuery = escapedQuery + '\\' + char1 + char2;
i++;
} else {
escapedQuery = escapedQuery + char1;
}
} else {
escapedQuery = escapedQuery + char1;
}
}
return escapedQuery;
}
class LighthouseControllers { class LighthouseControllers {
/* eslint-disable no-param-reassign */ /* eslint-disable no-param-reassign */
// Start syncing blocks... // Start syncing blocks...