Skip to content

Commit 91b6142

Browse files
committed
fix: searching by "ref" and reuse existent method
reuse existent method "escapeEsKeyword" instead of new "escapeElasticsearchQuery"
1 parent 6c0f788 commit 91b6142

File tree

1 file changed

+9
-18
lines changed

1 file changed

+9
-18
lines changed

src/routes/projects/list.js

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,14 @@ const SUPPORTED_FILTERS = [
6060
'directProjectId',
6161
];
6262

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, '\\$&');
6471

6572
const buildEsFullTextQuery = (keyword, matchType, singleFieldName) => {
6673
let should = [
@@ -268,22 +275,6 @@ const setFilter = (value, keyword, fieldName) => {
268275
return buildEsQueryWithFilter(value, keyword, MATCH_TYPE_EXACT_PHRASE, fieldName);
269276
};
270277

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-
287278
/**
288279
* Parse the ES search criteria and prepare search request body
289280
*
@@ -442,7 +433,7 @@ const parseElasticSearchCriteria = (criteria, fields, order) => {
442433

443434
if (!keyword) {
444435
// Not a specific field search nor an exact phrase search, do a wildcard match
445-
keyword = escapeElasticsearchQuery(keywordCriterion);
436+
keyword = escapeEsKeyword(keywordCriterion);
446437
matchType = MATCH_TYPE_WILDCARD;
447438
}
448439

0 commit comments

Comments
 (0)