Skip to content

Commit 757fc22

Browse files
Improve searching
1 parent 45b5f3f commit 757fc22

File tree

2 files changed

+54
-7
lines changed

2 files changed

+54
-7
lines changed

src/init-es.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ const initES = async () => {
3434
id: { type: 'keyword' },
3535
name: {
3636
type: 'keyword',
37+
fields: {
38+
text: {
39+
type: 'text'
40+
}
41+
},
3742
normalizer: 'custom_sort_normalizer'
3843
},
3944
prizeSets: {

src/services/ChallengeService.js

Lines changed: 49 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -217,14 +217,48 @@ async function searchChallenges (currentUser, criteria) {
217217
})
218218
}
219219

220+
const multiMatchQuery = []
220221
if (criteria.search) {
221-
boolQuery.push({
222-
bool: {
223-
should: [
224-
{ match_phrase_prefix: { 'name': criteria.search } },
225-
{ match_phrase_prefix: { 'description': criteria.search } },
226-
{ match_phrase_prefix: { 'tags': criteria.search } }
227-
]
222+
multiMatchQuery.push({
223+
// exact match
224+
multi_match: {
225+
query: criteria.search,
226+
fields: [
227+
'name.text^7',
228+
'tags^3',
229+
'description^2'
230+
],
231+
type: 'phrase_prefix',
232+
boost: 5
233+
}
234+
})
235+
multiMatchQuery.push({
236+
// match 100% words
237+
multi_match: {
238+
query: criteria.search,
239+
fields: [
240+
'name.text^3.0',
241+
'tags^2.5',
242+
'description^1.0'
243+
],
244+
type: 'most_fields',
245+
minimum_should_match: '100%',
246+
boost: 2.5
247+
}
248+
})
249+
multiMatchQuery.push({
250+
// fuzzy match
251+
multi_match: {
252+
query: criteria.search,
253+
fields: [
254+
'name.text^2.5',
255+
'tags^1.5',
256+
'description^1.0'
257+
],
258+
type: 'most_fields',
259+
minimum_should_match: '50%',
260+
fuzziness: 'AUTO',
261+
boost: 1
228262
}
229263
})
230264
} else {
@@ -518,6 +552,14 @@ async function searchChallenges (currentUser, criteria) {
518552
})
519553
}
520554

555+
if (multiMatchQuery) {
556+
mustQuery.push({
557+
bool: {
558+
should: multiMatchQuery
559+
}
560+
})
561+
}
562+
521563
if (boolQuery.length > 0) {
522564
mustQuery.push({
523565
bool: {

0 commit comments

Comments
 (0)