@@ -275,31 +275,23 @@ async function searchChallenges (currentUser, criteria) {
275
275
276
276
const mustQuery = [ ]
277
277
278
- const shouldQuery = [ ]
278
+ const groupsQuery = [ ]
279
279
280
280
// logger.debug(`Tags: ${criteria.tags}`)
281
281
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 } } ) )
289
285
}
290
- }
286
+ } )
291
287
}
292
288
293
289
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 } } ) )
301
293
}
302
- }
294
+ } )
303
295
}
304
296
305
297
const mustNotQuery = [ ]
@@ -353,21 +345,23 @@ async function searchChallenges (currentUser, criteria) {
353
345
} else if ( ! currentUser . isMachine && ! helper . hasAdminRole ( currentUser ) ) {
354
346
// If the user is not M2M and is not an admin, return public + challenges from groups the user can access
355
347
_ . each ( accessibleGroups , ( g ) => {
356
- shouldQuery . push ( { match_phrase : { groups : g } } )
348
+ groupsQuery . push ( { match_phrase : { groups : g } } )
357
349
} )
358
350
// include public challenges
359
- shouldQuery . push ( { bool : { must_not : { exists : { field : 'groups' } } } } )
351
+ groupsQuery . push ( { bool : { must_not : { exists : { field : 'groups' } } } } )
360
352
}
361
353
} else {
362
354
_ . each ( groupsToFilter , ( g ) => {
363
- shouldQuery . push ( { match_phrase : { groups : g } } )
355
+ groupsQuery . push ( { match_phrase : { groups : g } } )
364
356
} )
365
357
}
366
358
367
359
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
+ } )
371
365
}
372
366
373
367
const accessQuery = [ ]
@@ -446,10 +440,10 @@ async function searchChallenges (currentUser, criteria) {
446
440
} )
447
441
}
448
442
449
- if ( shouldQuery . length > 0 ) {
443
+ if ( groupsQuery . length > 0 ) {
450
444
mustQuery . push ( {
451
445
bool : {
452
- should : shouldQuery
446
+ should : groupsQuery
453
447
}
454
448
} )
455
449
}
0 commit comments