Skip to content
This repository was archived by the owner on Mar 13, 2025. It is now read-only.
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions src/common/es-helper.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const config = require('config')
const _ = require('lodash')
const querystring = require('querystring')
const logger = require('../common/logger')
const groupApi = require('./group-api')
const appConst = require('../consts')
Expand Down Expand Up @@ -705,6 +706,15 @@ function buildEsQueryFromFilter (filter) {
return setFilterValueToEsQuery(esQuery, matchField, filter.value, filter.queryField)
}

/**
* Returns if char is one of the reserved regex characters
* @param {*} char the char to check
*/
function isRegexReserved (char) {
const reserved = '[^$.|?*+(){}\\'
return reserved.indexOf(char) !== -1
}

/**
* Build ES Query to get attribute values by attributeId
* @param attributeId the attribute whose values to fetch
Expand Down Expand Up @@ -735,7 +745,7 @@ function buildEsQueryToGetAttributeValues (attributeId, attributeValue, size) {
values: {
terms: {
field: USER_ATTRIBUTE.esDocumentValueQuery,
include: `.*${attributeValue.replace(/[A-Za-z]/g, c => `[${c.toLowerCase()}${c.toUpperCase()}]`)}.*`,
include: `.*${attributeValue.replace(/[^a-zA-Z]/g, c => `[${!isRegexReserved(c) ? c : '\\' + c}]`).replace(/[A-Za-z]/g, c => `[${c.toLowerCase()}${c.toUpperCase()}]`)}.*`,
order: {
_key: 'asc'
},
Expand Down Expand Up @@ -1175,6 +1185,7 @@ async function searchUsers (authUser, filter, params) {

const authUserOrganizationId = filter.organizationId
const filterKey = Object.keys(userFilters)

for (const key of filterKey) {
const resolved = await resolveUserFilterFromDb(userFilters[key], authUser, authUserOrganizationId)
resolvedUserFilters.push(resolved)
Expand Down Expand Up @@ -1257,11 +1268,11 @@ async function searchUsers (authUser, filter, params) {
* @param {Object} param0 The attribute id and the attribute value properties
*/
async function searchAttributeValues ({ attributeId, attributeValue }) {
const esQuery = buildEsQueryToGetAttributeValues(attributeId, attributeValue, 5)
const esQuery = buildEsQueryToGetAttributeValues(attributeId, querystring.unescape(attributeValue), 5)
logger.debug(`ES query for searching attribute values: ${JSON.stringify(esQuery, null, 2)}`)

const esResult = await esClient.search(esQuery)

logger.debug(`ES Result: ${JSON.stringify(esResult, null, 2)}`)
const result = []
const attributes = esResult.aggregations.attributes.ids.buckets

Expand Down