Skip to content

Build your personal knowledge base with Trilium Notes

fix(search): carry the boundary tie group into the path-scored pass

sxhdf111d33bf06
diff --git a/packages/trilium-core/src/services/search/services/search.spec.ts b/packages/trilium-core/src/services/search/services/search.spec.ts
index c3b83f6cb1..c4339efc71 100644
--- a/packages/trilium-core/src/services/search/services/search.spec.ts
+++ b/packages/trilium-core/src/services/search/services/search.spec.ts
@@ -1026,14 +1026,15 @@ describe("Search", () => {
}
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
+ // 505 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.
+ // that ranking, so it has to apply the same rule. The plateau is deliberately larger
+ // than the tie group the cut will carry, so this measures the depth rule itself.
const outer = create("Boxes", "root");
const inner = create("Sub", outer.noteId);
- for (let index = 0; index < 200; index++) {
+ for (let index = 0; index < 500; index++) {
create("Widget", inner.noteId);
}
@@ -1044,10 +1045,31 @@ describe("Search", () => {
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);
+ // The premise: far more matches than the shortlist holds, so the cut is what is measured.
+ expect(results.length).toBeGreaterThan(200);
+ expect(results.length).toBeLessThan(505);
expect(kept.length).toEqual(5);
});
+
+ it("carries a deep note into the pass that scores its path", () => {
+ // Same title and body throughout, so the first pass cannot separate any of them and the
+ // leaf's advantage exists only in the path score the shortlist unlocks. Ranking the cut
+ // by depth alone would drop that leaf as the deepest of the tie, which is the one note
+ // the deferred score would have placed first.
+ const matched = create("Widget", "root");
+ const nested = create("Widget", matched.noteId);
+ const leaf = create("Widget", nested.noteId);
+
+ for (let index = 0; index < 200; index++) {
+ create("Widget", "root");
+ }
+
+ const searchContext = new SearchContext({ rankInTwoPasses: true });
+ const results = searchService.findResultsWithQuery("widget", searchContext);
+
+ expect(results.map((result) => result.noteId)).toContain(leaf.noteId);
+ expect(results[0].noteId).toEqual(leaf.noteId);
+ });
});
describe("fuzzy content match highlighting", () => {
diff --git a/packages/trilium-core/src/services/search/services/search.ts b/packages/trilium-core/src/services/search/services/search.ts
index 312a5f5ab7..73079237aa 100644
--- a/packages/trilium-core/src/services/search/services/search.ts
+++ b/packages/trilium-core/src/services/search/services/search.ts
@@ -378,13 +378,26 @@ function performSearch(expression: Expression, searchContext: SearchContext, ena
if (twoPass) {
// 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) => {
+ const sorted = 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);
+ });
+
+ // Depth stands in here for the path score the cut has not paid for yet, so it must never be
+ // the only reason a note is dropped: a deep note under query-matching ancestors ties at this
+ // score and would lose the bonus that ranks it above the shallow notes kept in its place.
+ // Carry the whole tie group straddling the boundary instead, capped so a plateau far larger
+ // than the shortlist still costs one pass.
+ const boundaryScore = sorted[RANK_SHORTLIST - 1].score;
+ let cutTo = RANK_SHORTLIST;
+ while (cutTo < RANK_SHORTLIST * 2 && cutTo < sorted.length && sorted[cutTo].score === boundaryScore) {
+ cutTo++;
+ }
+
+ ranked = sorted.slice(0, cutTo);
for (const res of ranked) {
res.computeScore(searchContext.fulltextQuery, searchContext.highlightedTokens, enableFuzzyMatching, searchContext.contentMatches.get(res.noteId), scoringTerms);