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
fix(search): rank the two-pass cut by depth, not by scan order
sxh313d0fa1633327a
diff --git a/packages/trilium-core/src/services/search/services/search.spec.ts b/packages/trilium-core/src/services/search/services/search.spec.tsindex 6003fdf313..c3b83f6cb1 100644--- a/packages/trilium-core/src/services/search/services/search.spec.ts+++ b/packages/trilium-core/src/services/search/services/search.spec.ts@@ -1015,6 +1015,41 @@ describe("Search", () => {});});+ describe("the two-pass shortlist cut", () => {+ function create(title: string, parentNoteId: string) {+ return getContext().init(() => noteService.createNewNote({+ parentNoteId,+ title,+ content: "<p>widget details</p>",+ type: "text"+ }).note);+ }++ it("cuts down to the shallowest matches, the way a tie is ranked", () => {+ // 205 notes share one title and one body, so the first pass scores them alike and only+ // the order of the scan can decide who reaches the shortlist. The rule the final+ // comparator applies to an equal score is nearness to the root, and the cut is part of+ // that ranking, so it has to apply the same rule.+ const outer = create("Boxes", "root");+ const inner = create("Sub", outer.noteId);++ for (let index = 0; index < 200; index++) {+ create("Widget", inner.noteId);+ }++ const shallow = [ ...Array(5) ].map(() => create("Widget", "root"));++ const searchContext = new SearchContext({ rankInTwoPasses: true });+ const results = searchService.findResultsWithQuery("widget", searchContext);+ const shallowIds = new Set(shallow.map((note) => note.noteId));+ const kept = results.filter((result) => shallowIds.has(result.noteId));++ // The premise: more matches than the shortlist holds, so the cut is what is measured.+ expect(results.length).toEqual(200);+ expect(kept.length).toEqual(5);+ });+ });+describe("fuzzy content match highlighting", () => {function contentNote(title: string, content: string) {return getContext().init(() => noteService.createNewNote({diff --git a/packages/trilium-core/src/services/search/services/search.ts b/packages/trilium-core/src/services/search/services/search.tsindex 240222586b..312a5f5ab7 100644--- a/packages/trilium-core/src/services/search/services/search.ts+++ b/packages/trilium-core/src/services/search/services/search.ts@@ -376,7 +376,15 @@ function performSearch(expression: Expression, searchContext: SearchContext, enalet ranked = searchResults;if (twoPass) {- ranked = searchResults.sort((a, b) => b.score - a.score).slice(0, RANK_SHORTLIST);+ // The cut is part of the ranking, so it breaks an equal score the way the final comparator+ // does. Left to score alone, the surviving shortlist is whichever notes the scan met first.+ ranked = searchResults.sort((a, b) => {+ if (a.score !== b.score) {+ return b.score - a.score;+ }++ return a.notePathArray.length - b.notePathArray.length;+ }).slice(0, RANK_SHORTLIST);for (const res of ranked) {res.computeScore(searchContext.fulltextQuery, searchContext.highlightedTokens, enableFuzzyMatching, searchContext.contentMatches.get(res.noteId), scoringTerms);