Skip to content

Commit f8ecc6b

Browse files
author
tcchhabra
committed
fix for issue #3740
1 parent 12612e0 commit f8ecc6b

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

src/routes/projects/list.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,21 @@ const escapeEsKeyword = keyword => keyword.replace(/[+-=><!|(){}[&\]^"~*?:\\/]/g
6969
* @return {String} result after parsing
7070
*/
7171
function escapeElasticsearchQuery(query) {
72-
return query.replace(/(\+|\-|\=|&&|\|\||\>|\<|\!|\(|\)|\{|\}|\[|\]|\^|"|~|\*|\?|\:|\\|\/)/g, '\\$&');
72+
const chars = ['\\', '+', '-', '&&', '||', '!', '(', ')', '{', '}', '[', ']',
73+
'^', '"', '~', '*', '?', ':', '/', '<', '>'];
74+
let result = query;
75+
_.forEach(chars, (item) => {
76+
result = result.replace(item, `\\${item}`);
77+
});
78+
return result;
7379
}
7480

7581
const buildEsFullTextQuery = (keyword, matchType, singleFieldName) => {
76-
keyword = escapeElasticsearchQuery(keyword);
82+
const escapedKeyword = escapeElasticsearchQuery(keyword);
7783
let should = [
7884
{
7985
query_string: {
80-
query: (matchType === MATCH_TYPE_EXACT_PHRASE) ? keyword : `*${keyword}*`,
86+
query: (matchType === MATCH_TYPE_EXACT_PHRASE) ? escapedKeyword : `*${escapedKeyword}*`,
8187
analyze_wildcard: (matchType === MATCH_TYPE_WILDCARD),
8288
fields: ['name^5', 'description^3', 'type^2'],
8389
},
@@ -90,7 +96,7 @@ const buildEsFullTextQuery = (keyword, matchType, singleFieldName) => {
9096
path: 'details.utm',
9197
query: {
9298
query_string: {
93-
query: (matchType === MATCH_TYPE_EXACT_PHRASE) ? keyword : `*${keyword}*`,
99+
query: (matchType === MATCH_TYPE_EXACT_PHRASE) ? escapedKeyword : `*${escapedKeyword}*`,
94100
analyze_wildcard: (matchType === MATCH_TYPE_WILDCARD || matchType === MATCH_TYPE_SINGLE_FIELD),
95101
fields: ['details.utm.code^4'],
96102
},
@@ -104,7 +110,7 @@ const buildEsFullTextQuery = (keyword, matchType, singleFieldName) => {
104110
path: 'members',
105111
query: {
106112
query_string: {
107-
query: (matchType === MATCH_TYPE_EXACT_PHRASE) ? keyword : `*${keyword}*`,
113+
query: (matchType === MATCH_TYPE_EXACT_PHRASE) ? escapedKeyword : `*${escapedKeyword}*`,
108114
analyze_wildcard: (matchType === MATCH_TYPE_WILDCARD),
109115
fields: ['members.email', 'members.handle', 'members.firstName', 'members.lastName'],
110116
},

0 commit comments

Comments
 (0)