@@ -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 - z A - Z ] + / gi , c => `[${ c } ]` ) . replace ( / [ A - Z a - z ] / g, c => `[${ c . toLowerCase ( ) } ${ c . toUpperCase ( ) } ]` ) } .*` ,
748+ include : `.*${ attributeValue . replace ( / [ ^ a - z A - Z ] / g , c => `[${ ! isRegexReserved ( c ) ? c : '\\' + c } ]` ) . replace ( / [ A - Z a - 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