@@ -60,7 +60,14 @@ const SUPPORTED_FILTERS = [
60
60
'directProjectId' ,
61
61
] ;
62
62
63
- const escapeEsKeyword = keyword => keyword . replace ( / [ + - = > < ! | ( ) { } [ & \] ^ " ~ * ? : \\ / ] / g, '\\\\$&' ) ;
63
+ /**
64
+ * ES need to skip special chars else it is considered as RegEx or other ES query string syntax,
65
+ * see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-query-string-query.html
66
+ *
67
+ * @param {String } keyword keyword being searched for
68
+ * @return {String } result after parsing
69
+ */
70
+ const escapeEsKeyword = keyword => keyword . replace ( / [ + - = > < ! | ( ) { } [ & \] ^ " ~ * ? : \\ / ] / g, '\\$&' ) ;
64
71
65
72
const buildEsFullTextQuery = ( keyword , matchType , singleFieldName ) => {
66
73
let should = [
@@ -268,22 +275,6 @@ const setFilter = (value, keyword, fieldName) => {
268
275
return buildEsQueryWithFilter ( value , keyword , MATCH_TYPE_EXACT_PHRASE , fieldName ) ;
269
276
} ;
270
277
271
- /**
272
- * ES need to skip special chars else it is considered as RegEx
273
- *
274
- * @param {String } query query being searched for
275
- * @return {String } result after parsing
276
- */
277
- function escapeElasticsearchQuery ( query ) {
278
- const chars = [ '\\' , '+' , '-' , '&&' , '||' , '!' , '(' , ')' , '{' , '}' , '[' , ']' ,
279
- '^' , '"' , '~' , '*' , '?' , ':' , '/' , '<' , '>' ] ;
280
- let result = query ;
281
- _ . forEach ( chars , ( item ) => {
282
- result = result . replace ( item , `\\${ item } ` ) ;
283
- } ) ;
284
- return result ;
285
- }
286
-
287
278
/**
288
279
* Parse the ES search criteria and prepare search request body
289
280
*
@@ -442,7 +433,7 @@ const parseElasticSearchCriteria = (criteria, fields, order) => {
442
433
443
434
if ( ! keyword ) {
444
435
// Not a specific field search nor an exact phrase search, do a wildcard match
445
- keyword = escapeElasticsearchQuery ( keywordCriterion ) ;
436
+ keyword = escapeEsKeyword ( keywordCriterion ) ;
446
437
matchType = MATCH_TYPE_WILDCARD ;
447
438
}
448
439
0 commit comments