Skip to content

Refactor/clean up ES query on GET /challenges #315

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 1, 2020
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
44 changes: 19 additions & 25 deletions src/services/ChallengeService.js
Original file line number Diff line number Diff line change
Expand Up @@ -275,31 +275,23 @@ async function searchChallenges (currentUser, criteria) {

const mustQuery = []

const shouldQuery = []
const groupsQuery = []

// logger.debug(`Tags: ${criteria.tags}`)
if (criteria.tags) {
if (criteria.includeAllTags) {
for (const tag of criteria.tags) {
boolQuery.push({ match_phrase: { tags: tag } })
}
} else {
for (const tag of criteria.tags) {
shouldQuery.push({ match: { tags: tag } })
boolQuery.push({
bool: {
[criteria.includeAllTags ? 'must' : 'should']: _.map(criteria.tags, t => ({ match_phrase: { tags: t } }))
}
}
})
}

if (criteria.events) {
if (criteria.includeAllEvents) {
for (const e of criteria.events) {
boolQuery.push({ match_phrase: { 'events.key': e } })
}
} else {
for (const e of criteria.events) {
shouldQuery.push({ match: { 'events.key': e } })
boolQuery.push({
bool: {
[criteria.includeAllEvents ? 'must' : 'should']: _.map(criteria.events, e => ({ match_phrase: { 'events.key': e } }))
}
}
})
}

const mustNotQuery = []
Expand Down Expand Up @@ -353,21 +345,23 @@ async function searchChallenges (currentUser, criteria) {
} else if (!currentUser.isMachine && !helper.hasAdminRole(currentUser)) {
// If the user is not M2M and is not an admin, return public + challenges from groups the user can access
_.each(accessibleGroups, (g) => {
shouldQuery.push({ match_phrase: { groups: g } })
groupsQuery.push({ match_phrase: { groups: g } })
})
// include public challenges
shouldQuery.push({ bool: { must_not: { exists: { field: 'groups' } } } })
groupsQuery.push({ bool: { must_not: { exists: { field: 'groups' } } } })
}
} else {
_.each(groupsToFilter, (g) => {
shouldQuery.push({ match_phrase: { groups: g } })
groupsQuery.push({ match_phrase: { groups: g } })
})
}

if (criteria.ids) {
for (const id of criteria.ids) {
shouldQuery.push({ match_phrase: { _id: id } })
}
boolQuery.push({
bool: {
should: _.map(criteria.ids, id => ({ match_phrase: { _id: id } }))
}
})
}

const accessQuery = []
Expand Down Expand Up @@ -446,10 +440,10 @@ async function searchChallenges (currentUser, criteria) {
})
}

if (shouldQuery.length > 0) {
if (groupsQuery.length > 0) {
mustQuery.push({
bool: {
should: shouldQuery
should: groupsQuery
}
})
}
Expand Down