Skip to content
This repository was archived by the owner on Mar 13, 2025. It is now read-only.

Commit 2853a7a

Browse files
committed
Escape reserved regex characters. Fixes #295
1 parent 1b8174d commit 2853a7a

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

src/common/es-helper.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -706,6 +706,15 @@ function buildEsQueryFromFilter (filter) {
706706
return setFilterValueToEsQuery(esQuery, matchField, filter.value, filter.queryField)
707707
}
708708

709+
/**
710+
* Returns if char is one of the reserved regex characters
711+
* @param {*} char the char to check
712+
*/
713+
function isRegexReserved (char) {
714+
const reserved = '[^$.|?*+(){}\\'
715+
return reserved.indexOf(char) !== -1
716+
}
717+
709718
/**
710719
* Build ES Query to get attribute values by attributeId
711720
* @param attributeId the attribute whose values to fetch
@@ -736,7 +745,7 @@ function buildEsQueryToGetAttributeValues (attributeId, attributeValue, size) {
736745
values: {
737746
terms: {
738747
field: USER_ATTRIBUTE.esDocumentValueQuery,
739-
include: `.*${attributeValue.replace(/[^a-zA-Z]+/gi, c => `[${c}]`).replace(/[A-Za-z]/g, c => `[${c.toLowerCase()}${c.toUpperCase()}]`)}.*`,
748+
include: `.*${attributeValue.replace(/[^a-zA-Z]/g, c => `[${!isRegexReserved(c) ? c : '\\' + c}]`).replace(/[A-Za-z]/g, c => `[${c.toLowerCase()}${c.toUpperCase()}]`)}.*`,
740749
order: {
741750
_key: 'asc'
742751
},
@@ -1263,6 +1272,7 @@ async function searchAttributeValues ({ attributeId, attributeValue }) {
12631272
logger.debug(`ES query for searching attribute values: ${JSON.stringify(esQuery, null, 2)}`)
12641273

12651274
const esResult = await esClient.search(esQuery)
1275+
logger.debug(`ES Result: ${JSON.stringify(esResult, null, 2)}`)
12661276
const result = []
12671277
const attributes = esResult.aggregations.attributes.ids.buckets
12681278

0 commit comments

Comments
 (0)