From 0b433e5e722a2c50fe750d7fd46fdde19107d927 Mon Sep 17 00:00:00 2001 From: KatieFrogs <23621460+KatieFrogs@users.noreply.github.com> Date: Mon, 28 Feb 2022 02:19:25 +0300 Subject: [PATCH] Improve search relevancy - If the search string gets split too many times (more than twice the amount of characters), the result is excluded - Matches in the title are ranked higher than in the subtitle - Exact matches are ranked the highest --- public/src/js/songselect.js | 59 +++++++++++++++++++++++++++++-------- 1 file changed, 47 insertions(+), 12 deletions(-) diff --git a/public/src/js/songselect.js b/public/src/js/songselect.js index d2aa6e9..aad1341 100644 --- a/public/src/js/songselect.js +++ b/public/src/js/songselect.js @@ -2789,17 +2789,7 @@ class SongSelect{ highlightResult(text, result){ var fragment = document.createDocumentFragment() - var indexes = result ? result.indexes : [] - var ranges = [] - var range - indexes.forEach(idx => { - if(range && range[1] === idx - 1){ - range[1] = idx - }else{ - range = [idx, idx] - ranges.push(range) - } - }) + var ranges = (result ? result.ranges : null) || [] var lastIdx = 0 ranges.forEach(range => { if(lastIdx !== range[0]){ @@ -3062,7 +3052,38 @@ class SongSelect{ results = fuzzysort.go(query, results, { keys: ["titlePrepared", "subtitlePrepared"], allowTypo: true, - limit: maxResults + limit: maxResults, + scoreFn: a => { + if(a[0]){ + var score0 = a[0].score + a[0].ranges = this.indexesToRanges(a[0].indexes) + if(a[0].indexes.length > 1){ + if(a[0].ranges.length > a[0].indexes.length / 2){ + score0 = -Infinity + a[0].ranges = null + }else if(a[0].ranges.length !== 1){ + score0 -= 9000 + } + } + } + if(a[1]){ + var score1 = a[1].score - 1000 + a[1].ranges = this.indexesToRanges(a[1].indexes) + if(a[1].indexes.length > 1){ + if(a[1].ranges.length > a[1].indexes.length / 2){ + score1 = -Infinity + a[1].ranges = null + }else if(a[1].ranges.length !== 1){ + score1 -= 9000 + } + } + } + if(a[0]){ + return a[1] ? Math.max(score0, score1) : score0 + }else{ + return a[1] ? score1 : -Infinity + } + } }) }else{ results = results.map(result => { @@ -3072,6 +3093,20 @@ class SongSelect{ return results } + + indexesToRanges(indexes){ + var ranges = [] + var range + indexes.forEach(idx => { + if(range && range[1] === idx - 1){ + range[1] = idx + }else{ + range = [idx, idx] + ranges.push(range) + } + }) + return ranges + } searchInput(){ var text = this.search.input.value.toLowerCase()