Skip to content

Commit 0c48580

Browse files
committed
fix(search): classify whole words before phrase bonuses
Carry whole-word matching through the field scorer so a multiword prefix phrase cannot gain the same ranking priority from its phrase bonus. Extend the existing regression with adjacent and separated whole-word queries.
1 parent cb4098f commit 0c48580

2 files changed

Lines changed: 24 additions & 12 deletions

File tree

‎packages/shared/src/paletteSearch.test.ts‎

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,22 @@ describe("scoreThreadSearch", () => {
4040
thread("Share auth across worktrees"),
4141
]),
4242
).toEqual(["Share auth across worktrees", "Authentication cleanup", "Fix oauth redirect"]);
43-
// Use never lifts a prefix above a whole word, so a busy series cannot bury an old thread.
44-
expect(
45-
rankTitles("auth", [
46-
thread("Authentication cleanup", {
47-
activityAt: NOW,
48-
opens: { openedAt: NOW, score: 12, picks: [] },
49-
}),
50-
thread("Share auth across worktrees", { activityAt: NOW - 90 * DAY }),
51-
]),
52-
).toEqual(["Share auth across worktrees", "Authentication cleanup"]);
43+
for (const [query, prefix, wholeWords] of [
44+
["auth", "Authentication cleanup", "Share auth across worktrees"],
45+
["share auth", "Share authentication cleanup", "Share auth across worktrees"],
46+
["share auth", "Share authentication cleanup", "Auth across worktrees to share"],
47+
] as const) {
48+
expect(
49+
rankTitles(query, [
50+
thread(prefix, {
51+
activityAt: NOW,
52+
opens: { openedAt: NOW, score: 12, picks: [] },
53+
}),
54+
thread(wholeWords, { activityAt: NOW - 90 * DAY }),
55+
]),
56+
`${query}: ${wholeWords}`,
57+
).toEqual([wholeWords, prefix]);
58+
}
5359
});
5460

5561
it("treats camelCase, kebab, path, and digit boundaries as word starts", () => {

‎packages/shared/src/paletteSearch.ts‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,8 @@ interface WeightedSearchField {
344344
interface SearchFieldsMatch {
345345
/** Roughly 0.3 (inside a word) to 1.5 (exact title). */
346346
readonly quality: number;
347+
/** Every counted token matched a whole word at full weight, before phrase bonuses. */
348+
readonly wholeWords: boolean;
347349
/** Every token matched at a word start, without a typo. */
348350
readonly strong: boolean;
349351
/** Source ranges matched in the first field, for highlighting. */
@@ -441,6 +443,7 @@ function matchSearchFields(
441443
}
442444
return {
443445
quality,
446+
wholeWords: total >= counted * WHOLE_WORD,
444447
strong,
445448
primaryRanges: mergeRanges(primaryRanges),
446449
};
@@ -851,7 +854,7 @@ function matchReference(
851854
};
852855
return (thread.id && matches(thread.id)) ||
853856
thread.pullRequests?.some((pullRequest) => pullRequest.references?.some(matches))
854-
? { quality: 0.6, strong: true, primaryRanges: [] }
857+
? { quality: 0.6, wholeWords: false, strong: true, primaryRanges: [] }
855858
: null;
856859
}
857860

@@ -888,6 +891,7 @@ export interface ThreadSearchMatch {
888891
readonly score: number;
889892
/** Match quality alone; see `rankSearchGroups`. */
890893
readonly quality: number;
894+
readonly wholeWords: boolean;
891895
/** Every token matched at a word start; see `rankSearchGroups`. */
892896
readonly strong: boolean;
893897
/** Matched ranges in the title. */
@@ -1043,6 +1047,7 @@ export function scoreThreadSearch(
10431047
return {
10441048
score: match.quality * threadUsageBoost(thread, now),
10451049
quality: match.quality,
1050+
wholeWords: match.wholeWords,
10461051
// A thread's whole title never lifts its group over a command or project
10471052
// of that name: every new thread is titled "New thread" for a while.
10481053
groupQuality: Math.min(match.quality, WHOLE_NAME - 0.01),
@@ -1146,6 +1151,7 @@ export function rankSearchGroups<
11461151
readonly pickedAt?: number;
11471152
readonly pickedExactly?: boolean;
11481153
readonly groupQuality?: number;
1154+
readonly wholeWords?: boolean;
11491155
},
11501156
>(
11511157
groups: ReadonlyArray<{ readonly group: G; readonly items: ReadonlyArray<T> }>,
@@ -1174,7 +1180,7 @@ export function rankSearchGroups<
11741180
const wordMatch = (match: M) => Math.min(matchClass(match.quality), 2);
11751181
const exactPickAt = (match: M) => (match.pickedExactly ? (match.pickedAt ?? 0) : 0);
11761182
// Use reorders whole words or prefixes among themselves, never a prefix above a whole word.
1177-
const wholeWord = (match: M) => Number(match.quality >= WHOLE_WORD);
1183+
const wholeWord = (match: M) => Number(match.wholeWords ?? match.quality >= WHOLE_WORD);
11781184
const sorted = [...items].sort(
11791185
(left, right) =>
11801186
wordMatch(right.match) - wordMatch(left.match) ||

0 commit comments

Comments
 (0)