From bca7d9c4d29ff5d728184250d190160843fbf1e7 Mon Sep 17 00:00:00 2001 From: perf3ct Date: Mon, 25 Nov 2024 23:04:20 +0000 Subject: [PATCH 1/4] weigh exact title matches heavily --- src/services/search/search_result.ts | 35 ++++++++++++++++++---------- 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/src/services/search/search_result.ts b/src/services/search/search_result.ts index 385999053..c1da6b7df 100644 --- a/src/services/search/search_result.ts +++ b/src/services/search/search_result.ts @@ -27,21 +27,32 @@ class SearchResult { this.score = 0; const note = becca.notes[this.noteId]; + const normalizedQuery = fulltextQuery.toLowerCase(); + const normalizedTitle = note.title.toLowerCase(); + // Note ID exact match if (note.noteId.toLowerCase() === fulltextQuery) { this.score += 100; } - if (note.title.toLowerCase() === fulltextQuery) { - this.score += 100; // high reward for exact match #3470 + // Title matching scores - significantly increase the exact match score + if (normalizedTitle === normalizedQuery) { + this.score += 1000; // Much higher score for exact match + } + else if (normalizedTitle.startsWith(normalizedQuery)) { + this.score += 150; + } + else if (normalizedTitle.includes(` ${normalizedQuery} `) || + normalizedTitle.startsWith(`${normalizedQuery} `) || + normalizedTitle.endsWith(` ${normalizedQuery}`)) { + this.score += 120; } - // notes with matches on its own note title as opposed to ancestors or descendants + const beforeTokenScore = this.score; + // Add scores for partial matches with lower weights this.addScoreForStrings(tokens, note.title, 1.5); - - // matches in attributes don't get extra points and thus are implicitly valued less than note path matches - - this.addScoreForStrings(tokens, this.notePathTitle, 1); + this.addScoreForStrings(tokens, this.notePathTitle, 0.5); // Reduced weight for path matches + if (note.isInHiddenSubtree()) { this.score = this.score / 2; @@ -51,19 +62,19 @@ class SearchResult { addScoreForStrings(tokens: string[], str: string, factor: number) { const chunks = str.toLowerCase().split(" "); - this.score = 0; - + let tokenScore = 0; for (const chunk of chunks) { for (const token of tokens) { if (chunk === token) { - this.score += 4 * token.length * factor; + tokenScore += 4 * token.length * factor; } else if (chunk.startsWith(token)) { - this.score += 2 * token.length * factor; + tokenScore += 2 * token.length * factor; } else if (chunk.includes(token)) { - this.score += token.length * factor; + tokenScore += token.length * factor; } } } + this.score += tokenScore; } } From a97a0660ea261d383d7d8bc6a979c2a3c408ec6a Mon Sep 17 00:00:00 2001 From: perf3ct Date: Tue, 26 Nov 2024 03:41:01 +0000 Subject: [PATCH 2/4] add back comment --- src/services/search/search_result.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/services/search/search_result.ts b/src/services/search/search_result.ts index c1da6b7df..a9190674b 100644 --- a/src/services/search/search_result.ts +++ b/src/services/search/search_result.ts @@ -48,6 +48,7 @@ class SearchResult { this.score += 120; } + // notes with matches on its own note title as opposed to ancestors or descendants const beforeTokenScore = this.score; // Add scores for partial matches with lower weights this.addScoreForStrings(tokens, note.title, 1.5); From 7fdaedd468f250355397b906826303d3e8548616 Mon Sep 17 00:00:00 2001 From: perf3ct Date: Tue, 26 Nov 2024 03:52:20 +0000 Subject: [PATCH 3/4] also significantly increase noteId matches --- src/services/search/search_result.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/services/search/search_result.ts b/src/services/search/search_result.ts index a9190674b..9a0f71f9c 100644 --- a/src/services/search/search_result.ts +++ b/src/services/search/search_result.ts @@ -30,9 +30,9 @@ class SearchResult { const normalizedQuery = fulltextQuery.toLowerCase(); const normalizedTitle = note.title.toLowerCase(); - // Note ID exact match + // Note ID exact match, also significantly increase if (note.noteId.toLowerCase() === fulltextQuery) { - this.score += 100; + this.score += 1000; } // Title matching scores - significantly increase the exact match score From 84007a11038b3909fb3a24a143a9ce72bfa1165d Mon Sep 17 00:00:00 2001 From: perf3ct Date: Tue, 26 Nov 2024 19:15:45 +0000 Subject: [PATCH 4/4] tweak weights --- src/services/search/search_result.ts | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/src/services/search/search_result.ts b/src/services/search/search_result.ts index 9a0f71f9c..45a85c386 100644 --- a/src/services/search/search_result.ts +++ b/src/services/search/search_result.ts @@ -30,33 +30,30 @@ class SearchResult { const normalizedQuery = fulltextQuery.toLowerCase(); const normalizedTitle = note.title.toLowerCase(); - // Note ID exact match, also significantly increase + // Note ID exact match, much higher score if (note.noteId.toLowerCase() === fulltextQuery) { this.score += 1000; } - // Title matching scores - significantly increase the exact match score + // Title matching scores, make sure to always win if (normalizedTitle === normalizedQuery) { - this.score += 1000; // Much higher score for exact match + this.score += 2000; // Increased from 1000 to ensure exact matches always win } else if (normalizedTitle.startsWith(normalizedQuery)) { - this.score += 150; + this.score += 500; // Increased to give more weight to prefix matches } else if (normalizedTitle.includes(` ${normalizedQuery} `) || normalizedTitle.startsWith(`${normalizedQuery} `) || normalizedTitle.endsWith(` ${normalizedQuery}`)) { - this.score += 120; + this.score += 300; // Increased to better distinguish word matches } - // notes with matches on its own note title as opposed to ancestors or descendants - const beforeTokenScore = this.score; - // Add scores for partial matches with lower weights - this.addScoreForStrings(tokens, note.title, 1.5); - this.addScoreForStrings(tokens, this.notePathTitle, 0.5); // Reduced weight for path matches - + // Add scores for partial matches with adjusted weights + this.addScoreForStrings(tokens, note.title, 2.0); // Increased to give more weight to title matches + this.addScoreForStrings(tokens, this.notePathTitle, 0.3); // Reduced to further de-emphasize path matches if (note.isInHiddenSubtree()) { - this.score = this.score / 2; + this.score = this.score / 3; // Increased penalty for hidden notes } }