Skip to content

Commit 4e99221

Browse files
Try to fix the query
1 parent 7aee559 commit 4e99221

File tree

1 file changed

+17
-14
lines changed

1 file changed

+17
-14
lines changed

src/services/ChallengeService.js

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,6 @@ async function searchChallenges (currentUser, criteria) {
6969
boolQuery.push(filter)
7070
})
7171

72-
if (criteria.memberId) {
73-
const ids = await helper.listChallengesByMember(criteria.memberId)
74-
boolQuery.push({ terms: { _id: ids } })
75-
}
7672
if (criteria.tag) {
7773
boolQuery.push({ match_phrase: { tags: criteria.tag } })
7874
}
@@ -95,19 +91,26 @@ async function searchChallenges (currentUser, criteria) {
9591
boolQuery.push({ range: { updated: { lte: criteria.updatedDateEnd } } })
9692
}
9793

98-
const shouldQuery = []
94+
const mustQuery = []
95+
96+
const accessQuery = []
9997

10098
if (currentUser.handle) {
101-
shouldQuery.push({
102-
bool: {
103-
filter: [
104-
{ match_phrase: { createdBy: currentUser.handle } }
105-
]
106-
}
107-
})
99+
accessQuery.push({ match_phrase: { createdBy: currentUser.handle } })
100+
}
101+
102+
if (criteria.memberId) {
103+
const ids = await helper.listChallengesByMember(criteria.memberId)
104+
accessQuery.push({ terms: { _id: ids } })
108105
}
109106

110-
shouldQuery.push({
107+
mustQuery.push({
108+
bool: {
109+
should: accessQuery
110+
}
111+
})
112+
113+
mustQuery.push({
111114
bool: {
112115
filter: boolQuery
113116
}
@@ -121,7 +124,7 @@ async function searchChallenges (currentUser, criteria) {
121124
body: {
122125
query: {
123126
bool: {
124-
should: shouldQuery
127+
must: mustQuery
125128
}
126129
},
127130
sort: [{ 'created': { 'order': 'asc' } }]

0 commit comments

Comments
 (0)