|
1 | 1 | const config = require('config')
|
2 | 2 | const _ = require('lodash')
|
| 3 | +const querystring = require('querystring') |
3 | 4 | const logger = require('../common/logger')
|
4 | 5 | const groupApi = require('./group-api')
|
5 | 6 | const appConst = require('../consts')
|
@@ -705,6 +706,15 @@ function buildEsQueryFromFilter (filter) {
|
705 | 706 | return setFilterValueToEsQuery(esQuery, matchField, filter.value, filter.queryField)
|
706 | 707 | }
|
707 | 708 |
|
| 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 | + |
708 | 718 | /**
|
709 | 719 | * Build ES Query to get attribute values by attributeId
|
710 | 720 | * @param attributeId the attribute whose values to fetch
|
@@ -735,7 +745,7 @@ function buildEsQueryToGetAttributeValues (attributeId, attributeValue, size) {
|
735 | 745 | values: {
|
736 | 746 | terms: {
|
737 | 747 | field: USER_ATTRIBUTE.esDocumentValueQuery,
|
738 |
| - include: `.*${attributeValue.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()}]`)}.*`, |
739 | 749 | order: {
|
740 | 750 | _key: 'asc'
|
741 | 751 | },
|
@@ -1175,6 +1185,7 @@ async function searchUsers (authUser, filter, params) {
|
1175 | 1185 |
|
1176 | 1186 | const authUserOrganizationId = filter.organizationId
|
1177 | 1187 | const filterKey = Object.keys(userFilters)
|
| 1188 | + |
1178 | 1189 | for (const key of filterKey) {
|
1179 | 1190 | const resolved = await resolveUserFilterFromDb(userFilters[key], authUser, authUserOrganizationId)
|
1180 | 1191 | resolvedUserFilters.push(resolved)
|
@@ -1257,11 +1268,11 @@ async function searchUsers (authUser, filter, params) {
|
1257 | 1268 | * @param {Object} param0 The attribute id and the attribute value properties
|
1258 | 1269 | */
|
1259 | 1270 | async function searchAttributeValues ({ attributeId, attributeValue }) {
|
1260 |
| - const esQuery = buildEsQueryToGetAttributeValues(attributeId, attributeValue, 5) |
| 1271 | + const esQuery = buildEsQueryToGetAttributeValues(attributeId, querystring.unescape(attributeValue), 5) |
1261 | 1272 | logger.debug(`ES query for searching attribute values: ${JSON.stringify(esQuery, null, 2)}`)
|
1262 | 1273 |
|
1263 | 1274 | const esResult = await esClient.search(esQuery)
|
1264 |
| - |
| 1275 | + logger.debug(`ES Result: ${JSON.stringify(esResult, null, 2)}`) |
1265 | 1276 | const result = []
|
1266 | 1277 | const attributes = esResult.aggregations.attributes.ids.buckets
|
1267 | 1278 |
|
|
0 commit comments