Skip to content

Build your personal knowledge base with Trilium Notes

fix(search): keep the two-pass shortlist at its limit

Elian Doranf91db3841322
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 65b376291d..1fcac3643c 100644
--- a/packages/trilium-core/src/services/search/services/search.spec.ts
+++ b/packages/trilium-core/src/services/search/services/search.spec.ts
@@ -1058,11 +1058,9 @@ describe("Search", () => {
}
it("cuts down to the shallowest matches, the way a tie is ranked", () => {
- // 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. The plateau is deliberately larger
- // than the tie group the cut will carry, so this measures the depth rule itself.
+ // 505 notes share one title and one body, so the first pass scores them alike and the
+ // shortlist keeps the shallowest of them. The shallow notes are created last, so scan
+ // order alone would drop them.
const outer = create("Boxes", "root");
const inner = create("Sub", outer.noteId);
@@ -1077,31 +1075,9 @@ describe("Search", () => {
const shallowIds = new Set(shallow.map((note) => note.noteId));
const kept = results.filter((result) => shallowIds.has(result.noteId));
- // 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(results.length).toEqual(200);
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 82855e2ded..115242b2cb 100644
--- a/packages/trilium-core/src/services/search/services/search.ts
+++ b/packages/trilium-core/src/services/search/services/search.ts
@@ -379,28 +379,15 @@ function performSearch(expression: Expression, searchContext: SearchContext, ena
let ranked = searchResults;
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.
- const sorted = searchResults.sort((a, b) => {
+ // An equal score goes to the shallower note, as in the final sort, so the shortlist does not
+ // depend on the order of the scan.
+ ranked = searchResults.sort((a, b) => {
if (a.score !== b.score) {
return b.score - a.score;
}
return a.notePathArray.length - b.notePathArray.length;
- });
-
- // 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);
+ }).slice(0, RANK_SHORTLIST);
for (const res of ranked) {
res.computeScore(searchContext.fulltextQuery, searchContext.highlightedTokens, enableFuzzyMatching, searchContext.contentMatches.get(res.noteId), scoringTerms);