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 1/4] 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() From 22712326b25b6328f7cbca8782df112e1a4557c1 Mon Sep 17 00:00:00 2001 From: KatieFrogs <23621460+KatieFrogs@users.noreply.github.com> Date: Mon, 28 Feb 2022 02:45:27 +0300 Subject: [PATCH 2/4] Workaround for an edge case --- public/src/js/songselect.js | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/public/src/js/songselect.js b/public/src/js/songselect.js index aad1341..3a2427e 100644 --- a/public/src/js/songselect.js +++ b/public/src/js/songselect.js @@ -3059,8 +3059,10 @@ class SongSelect{ 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 + if(a[0].target.indexOf(query) === -1){ + score0 = -Infinity + a[0].ranges = null + } }else if(a[0].ranges.length !== 1){ score0 -= 9000 } @@ -3071,8 +3073,10 @@ class SongSelect{ 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 + if(a[1].target.indexOf(query) === -1){ + score1 = -Infinity + a[1].ranges = null + } }else if(a[1].ranges.length !== 1){ score1 -= 9000 } From 8c3429b67cf1be6e329ee2638131c4218f288dfa Mon Sep 17 00:00:00 2001 From: KatieFrogs <23621460+KatieFrogs@users.noreply.github.com> Date: Mon, 28 Feb 2022 02:51:51 +0300 Subject: [PATCH 3/4] Move the highlighted range --- public/src/js/songselect.js | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/public/src/js/songselect.js b/public/src/js/songselect.js index 3a2427e..f12d68b 100644 --- a/public/src/js/songselect.js +++ b/public/src/js/songselect.js @@ -3059,9 +3059,12 @@ class SongSelect{ a[0].ranges = this.indexesToRanges(a[0].indexes) if(a[0].indexes.length > 1){ if(a[0].ranges.length > a[0].indexes.length / 2){ - if(a[0].target.indexOf(query) === -1){ + var index = a[0].target.indexOf(query) + if(index === -1){ score0 = -Infinity a[0].ranges = null + }else{ + a[0].ranges = [[index, index + query.length - 1]] } }else if(a[0].ranges.length !== 1){ score0 -= 9000 @@ -3073,9 +3076,12 @@ class SongSelect{ a[1].ranges = this.indexesToRanges(a[1].indexes) if(a[1].indexes.length > 1){ if(a[1].ranges.length > a[1].indexes.length / 2){ - if(a[1].target.indexOf(query) === -1){ + var index = a[1].target.indexOf(query) + if(index === -1){ score1 = -Infinity a[1].ranges = null + }else{ + a[1].ranges = [[index, index + query.length - 1]] } }else if(a[1].ranges.length !== 1){ score1 -= 9000 From 03428aa50e789f60827f58f57eee5c3a688918d9 Mon Sep 17 00:00:00 2001 From: KatieFrogs <23621460+KatieFrogs@users.noreply.github.com> Date: Mon, 28 Feb 2022 03:24:14 +0300 Subject: [PATCH 4/4] Do not exclude 1 char gap --- public/src/js/songselect.js | 46 ++++++++++++++++++++++++------------- 1 file changed, 30 insertions(+), 16 deletions(-) diff --git a/public/src/js/songselect.js b/public/src/js/songselect.js index f12d68b..7e0c1be 100644 --- a/public/src/js/songselect.js +++ b/public/src/js/songselect.js @@ -3058,15 +3058,22 @@ class SongSelect{ 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){ - var index = a[0].target.indexOf(query) - if(index === -1){ - score0 = -Infinity - a[0].ranges = null - }else{ - a[0].ranges = [[index, index + query.length - 1]] + var rangeAmount = a[0].ranges.length + var lastIdx = -3 + a[0].ranges.forEach(range => { + if(range[0] - lastIdx <= 2){ + rangeAmount-- + score0 -= 1000 } - }else if(a[0].ranges.length !== 1){ + lastIdx = range[1] + }) + var index = a[0].target.indexOf(query) + if(index !== -1){ + a[0].ranges = [[index, index + query.length - 1]] + }else if(rangeAmount > a[0].indexes.length / 2){ + score0 = -Infinity + a[0].ranges = null + }else if(rangeAmount !== 1){ score0 -= 9000 } } @@ -3075,15 +3082,22 @@ class SongSelect{ 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){ - var index = a[1].target.indexOf(query) - if(index === -1){ - score1 = -Infinity - a[1].ranges = null - }else{ - a[1].ranges = [[index, index + query.length - 1]] + var rangeAmount = a[1].ranges.length + var lastIdx = -3 + a[1].ranges.forEach(range => { + if(range[0] - lastIdx <= 2){ + rangeAmount-- + score1 -= 1000 } - }else if(a[1].ranges.length !== 1){ + lastIdx = range[1] + }) + var index = a[1].target.indexOf(query) + if(index !== -1){ + a[1].ranges = [[index, index + query.length - 1]] + }else if(rangeAmount > a[1].indexes.length / 2){ + score1 = -Infinity + a[1].ranges = null + }else if(rangeAmount !== 1){ score1 -= 9000 } }