@@ -275,31 +275,23 @@ async function searchChallenges (currentUser, criteria) {
275275
276276 const mustQuery = [ ]
277277
278- const shouldQuery = [ ]
278+ const groupsQuery = [ ]
279279
280280 // logger.debug(`Tags: ${criteria.tags}`)
281281 if ( criteria . tags ) {
282- if ( criteria . includeAllTags ) {
283- for ( const tag of criteria . tags ) {
284- boolQuery . push ( { match_phrase : { tags : tag } } )
285- }
286- } else {
287- for ( const tag of criteria . tags ) {
288- shouldQuery . push ( { match : { tags : tag } } )
282+ boolQuery . push ( {
283+ bool : {
284+ [ criteria . includeAllTags ? 'must' : 'should' ] : _ . map ( criteria . tags , t => ( { match_phrase : { tags : t } } ) )
289285 }
290- }
286+ } )
291287 }
292288
293289 if ( criteria . events ) {
294- if ( criteria . includeAllEvents ) {
295- for ( const e of criteria . events ) {
296- boolQuery . push ( { match_phrase : { 'events.key' : e } } )
297- }
298- } else {
299- for ( const e of criteria . events ) {
300- shouldQuery . push ( { match : { 'events.key' : e } } )
290+ boolQuery . push ( {
291+ bool : {
292+ [ criteria . includeAllEvents ? 'must' : 'should' ] : _ . map ( criteria . events , e => ( { match_phrase : { 'events.key' : e } } ) )
301293 }
302- }
294+ } )
303295 }
304296
305297 const mustNotQuery = [ ]
@@ -353,21 +345,23 @@ async function searchChallenges (currentUser, criteria) {
353345 } else if ( ! currentUser . isMachine && ! helper . hasAdminRole ( currentUser ) ) {
354346 // If the user is not M2M and is not an admin, return public + challenges from groups the user can access
355347 _ . each ( accessibleGroups , ( g ) => {
356- shouldQuery . push ( { match_phrase : { groups : g } } )
348+ groupsQuery . push ( { match_phrase : { groups : g } } )
357349 } )
358350 // include public challenges
359- shouldQuery . push ( { bool : { must_not : { exists : { field : 'groups' } } } } )
351+ groupsQuery . push ( { bool : { must_not : { exists : { field : 'groups' } } } } )
360352 }
361353 } else {
362354 _ . each ( groupsToFilter , ( g ) => {
363- shouldQuery . push ( { match_phrase : { groups : g } } )
355+ groupsQuery . push ( { match_phrase : { groups : g } } )
364356 } )
365357 }
366358
367359 if ( criteria . ids ) {
368- for ( const id of criteria . ids ) {
369- shouldQuery . push ( { match_phrase : { _id : id } } )
370- }
360+ boolQuery . push ( {
361+ bool : {
362+ should : _ . map ( criteria . ids , id => ( { match_phrase : { _id : id } } ) )
363+ }
364+ } )
371365 }
372366
373367 const accessQuery = [ ]
@@ -446,10 +440,10 @@ async function searchChallenges (currentUser, criteria) {
446440 } )
447441 }
448442
449- if ( shouldQuery . length > 0 ) {
443+ if ( groupsQuery . length > 0 ) {
450444 mustQuery . push ( {
451445 bool : {
452- should : shouldQuery
446+ should : groupsQuery
453447 }
454448 } )
455449 }
0 commit comments