search hit with exact note title match gets a strong score boost, closes #3470

This commit is contained in:
zadam 2022-12-30 23:14:48 +01:00
parent ca4e1c19a7
commit 797ddf6205
5 changed files with 21 additions and 10 deletions

View File

@ -68,8 +68,6 @@ function updateNoteAttribute(req) {
attribute.markAsDeleted(); attribute.markAsDeleted();
} }
console.log(attribute);
attribute.save(); attribute.save();
return { return {

View File

@ -24,6 +24,7 @@ class SearchContext {
this.fuzzyAttributeSearch = !!params.fuzzyAttributeSearch; this.fuzzyAttributeSearch = !!params.fuzzyAttributeSearch;
this.highlightedTokens = []; this.highlightedTokens = [];
this.originalQuery = ""; this.originalQuery = "";
this.fulltextQuery = ""; // complete fulltext part
// if true, becca does not have (up-to-date) information needed to process the query // if true, becca does not have (up-to-date) information needed to process the query
// and some extra data needs to be loaded before executing // and some extra data needs to be loaded before executing
this.dbLoadNeeded = false; this.dbLoadNeeded = false;

View File

@ -17,18 +17,22 @@ class SearchResult {
return this.notePathArray[this.notePathArray.length - 1]; return this.notePathArray[this.notePathArray.length - 1];
} }
computeScore(tokens) { computeScore(fulltextQuery, tokens) {
this.score = 0; this.score = 0;
const note = becca.notes[this.noteId];
if (note.title.toLowerCase() === fulltextQuery) {
this.score += 100; // high reward for exact match #3470
}
// notes with matches on its own note title as opposed to ancestors or descendants
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 // 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, 1);
// 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];
this.addScoreForStrings(tokens, note.title, 1.5);
if (note.isInHiddenSubtree()) { if (note.isInHiddenSubtree()) {
this.score = this.score / 2; this.score = this.score / 2;
} }

View File

@ -1,6 +1,7 @@
function lex(str) { function lex(str) {
str = str.toLowerCase(); str = str.toLowerCase();
let fulltextQuery = "";
const fulltextTokens = []; const fulltextTokens = [];
const expressionTokens = []; const expressionTokens = [];
@ -37,6 +38,8 @@ function lex(str) {
expressionTokens.push(rec); expressionTokens.push(rec);
} else { } else {
fulltextTokens.push(rec); fulltextTokens.push(rec);
fulltextQuery = str.substr(0, endIndex + 1);
} }
currentWord = ''; currentWord = '';
@ -129,7 +132,10 @@ function lex(str) {
finishWord(str.length - 1); finishWord(str.length - 1);
fulltextQuery = fulltextQuery.trim();
return { return {
fulltextQuery,
fulltextTokens, fulltextTokens,
expressionTokens expressionTokens
} }

View File

@ -170,7 +170,7 @@ function findResultsWithExpression(expression, searchContext) {
.filter(note => !!note); .filter(note => !!note);
for (const res of searchResults) { for (const res of searchResults) {
res.computeScore(searchContext.highlightedTokens); res.computeScore(searchContext.fulltextQuery, searchContext.highlightedTokens);
} }
if (!noteSet.sorted) { if (!noteSet.sorted) {
@ -195,7 +195,9 @@ function findResultsWithExpression(expression, searchContext) {
} }
function parseQueryToExpression(query, searchContext) { function parseQueryToExpression(query, searchContext) {
const {fulltextTokens, expressionTokens} = lex(query); const {fulltextQuery, fulltextTokens, expressionTokens} = lex(query);
searchContext.fulltextQuery = fulltextQuery;
let structuredExpressionTokens; let structuredExpressionTokens;
try { try {