Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
search hit with exact note title match gets a strong score boost, closes #3470
zadam797ddf6205a9
diff --git a/src/routes/api/attributes.js b/src/routes/api/attributes.jsindex cb59239a9a..8c0299b7ae 100644--- a/src/routes/api/attributes.js+++ b/src/routes/api/attributes.js@@ -68,8 +68,6 @@ function updateNoteAttribute(req) {attribute.markAsDeleted();}- console.log(attribute);-attribute.save();return {diff --git a/src/services/search/search_context.js b/src/services/search/search_context.jsindex c71addb17c..6c427cf5fc 100644--- a/src/services/search/search_context.js+++ b/src/services/search/search_context.js@@ -24,6 +24,7 @@ class SearchContext {this.fuzzyAttributeSearch = !!params.fuzzyAttributeSearch;this.highlightedTokens = [];this.originalQuery = "";+ this.fulltextQuery = ""; // complete fulltext part// if true, becca does not have (up-to-date) information needed to process the query// and some extra data needs to be loaded before executingthis.dbLoadNeeded = false;diff --git a/src/services/search/search_result.js b/src/services/search/search_result.jsindex 15b036e9a1..4c678661de 100644--- a/src/services/search/search_result.js+++ b/src/services/search/search_result.js@@ -17,18 +17,22 @@ class SearchResult {return this.notePathArray[this.notePathArray.length - 1];}- computeScore(tokens) {+ computeScore(fulltextQuery, tokens) {this.score = 0;- // matches in attributes don't get extra points and thus are implicitly valued less than note path matches+ const note = becca.notes[this.noteId];- this.addScoreForStrings(tokens, this.notePathTitle, 1);+ if (note.title.toLowerCase() === fulltextQuery) {+ this.score += 100; // high reward for exact match #3470+ }- // add one more time for note title alone (already contained in the notePathTitle),- // thus preferring notes with matches on its own note title as opposed to ancestors or descendants- const note = becca.notes[this.noteId];+ // notes with matches on its own note title as opposed to ancestors or descendantsthis.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);+if (note.isInHiddenSubtree()) {this.score = this.score / 2;}diff --git a/src/services/search/services/lex.js b/src/services/search/services/lex.jsindex d7a3fb83e6..c3c2273eb6 100644--- a/src/services/search/services/lex.js+++ b/src/services/search/services/lex.js@@ -1,6 +1,7 @@function lex(str) {str = str.toLowerCase();+ let fulltextQuery = "";const fulltextTokens = [];const expressionTokens = [];@@ -37,6 +38,8 @@ function lex(str) {expressionTokens.push(rec);} else {fulltextTokens.push(rec);++ fulltextQuery = str.substr(0, endIndex + 1);}currentWord = '';@@ -129,7 +132,10 @@ function lex(str) {finishWord(str.length - 1);+ fulltextQuery = fulltextQuery.trim();+return {+ fulltextQuery,fulltextTokens,expressionTokens}diff --git a/src/services/search/services/search.js b/src/services/search/services/search.jsindex 206ec63b47..3e2f5af40a 100644--- a/src/services/search/services/search.js+++ b/src/services/search/services/search.js@@ -170,7 +170,7 @@ function findResultsWithExpression(expression, searchContext) {.filter(note => !!note);for (const res of searchResults) {- res.computeScore(searchContext.highlightedTokens);+ res.computeScore(searchContext.fulltextQuery, searchContext.highlightedTokens);}if (!noteSet.sorted) {@@ -195,7 +195,9 @@ function findResultsWithExpression(expression, searchContext) {}function parseQueryToExpression(query, searchContext) {- const {fulltextTokens, expressionTokens} = lex(query);+ const {fulltextQuery, fulltextTokens, expressionTokens} = lex(query);+ searchContext.fulltextQuery = fulltextQuery;+let structuredExpressionTokens;try {