Skip to content

Commit 194ad32

Browse files
TheSentinel454Elrondclaude
committed
fix(search): measure the prefix minimum on the trailing token
Only the final whitespace token gets `:*`, so a completed word before a one- or two-character tail ("the p", "project pr") still ran the broad prefix lookup. Measure the trailing token instead of the whole text. Co-authored-by: Elrond <28d6302a099e5225b02c4155ac4236e4912603df2ab08dbfc2f4fef08ce598c8@buzz.block.builderlab.xyz> Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Luke Tornquist <[email protected]>
1 parent 8d85abb commit 194ad32

2 files changed

Lines changed: 26 additions & 9 deletions

File tree

‎crates/buzz-search/src/query.rs‎

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ pub enum SearchMode {
6363
///
6464
/// Intended for bounded typeahead surfaces such as the desktop topbar. The
6565
/// relay still refetches and re-authorizes every hit; this mode changes only
66-
/// the candidate tsquery, not the access boundary. Text shorter than three
67-
/// characters returns no hits unless the query is kind:0-only.
66+
/// the candidate tsquery, not the access boundary. A trailing token shorter
67+
/// than three characters returns no hits unless the query is kind:0-only.
6868
Prefix,
6969
}
7070

@@ -150,8 +150,9 @@ const SEARCH_TEXT_MAX_CHARS: usize = 4096;
150150
/// bridge uses page 1), but clamp here too so a future caller cannot accidentally
151151
/// wire untrusted input into a multi-trillion-row OFFSET.
152152
const PAGE_MAX: u32 = 1000;
153-
/// Shortest search text [`SearchMode::Prefix`] runs for anything but a
154-
/// kind:0-only (profile) query; shorter text returns no hits without SQL.
153+
/// Shortest trailing token (the one that gets `:*`) [`SearchMode::Prefix`]
154+
/// runs for anything but a kind:0-only (profile) query; a shorter one returns
155+
/// no hits without SQL, whatever completed words precede it.
155156
/// The GIN index cannot stop early or return rows in rank order, so a one- or
156157
/// two-character `:*` prefix makes Postgres fetch, recheck and rank a large
157158
/// share of every indexed message before it can apply `LIMIT`, and typeahead
@@ -239,7 +240,12 @@ fn normalized_search_text(q: &str) -> Option<String> {
239240
pub async fn search(pool: &PgPool, query: &SearchQuery) -> Result<SearchResult, SearchError> {
240241
let profile_only = query.kinds.as_deref() == Some(&[0][..]);
241242
let Some(search_text) = normalized_search_text(&query.q).filter(|text| {
242-
query.mode != SearchMode::Prefix || profile_only || text.chars().count() >= PREFIX_MIN_CHARS
243+
query.mode != SearchMode::Prefix
244+
|| profile_only
245+
|| text
246+
.split_whitespace()
247+
.last()
248+
.is_some_and(|token| token.chars().count() >= PREFIX_MIN_CHARS)
243249
}) else {
244250
return Ok(SearchResult {
245251
hits: Vec::new(),

‎crates/buzz-search/tests/postgres_fts_integration.rs‎

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -397,12 +397,23 @@ async fn prefix_mode_matches_final_token_prefix_without_changing_full_text() {
397397
assert!(prefix_ids.contains(&project_plan));
398398
assert!(prefix_ids.contains(&project_archive));
399399

400-
// Below three characters, non-profile prefix search returns no hits in
401-
// either sort, whatever the kinds; `pro` above proves the rows exist.
400+
// A trailing token below three characters returns no hits for non-profile
401+
// prefix search, in either sort, whatever the kinds and whatever completed
402+
// words precede it; `pro` above proves the rows exist.
402403
// kind:0-only stays one-character typeahead (see the `jm` test above).
403404
for (q, kinds, sort) in [
404405
("pr", Some(vec![9]), buzz_search::SearchSort::Relevance),
405406
(" p ", Some(vec![9]), buzz_search::SearchSort::Relevance),
407+
(
408+
"project pr",
409+
Some(vec![9]),
410+
buzz_search::SearchSort::Relevance,
411+
),
412+
(
413+
"project p ",
414+
Some(vec![9]),
415+
buzz_search::SearchSort::Relevance,
416+
),
406417
("pr", None, buzz_search::SearchSort::Relevance),
407418
("pr", Some(vec![0, 9]), buzz_search::SearchSort::Relevance),
408419
("pr", Some(vec![9]), buzz_search::SearchSort::Newest),
@@ -451,7 +462,7 @@ async fn prefix_mode_matches_final_token_prefix_without_changing_full_text() {
451462
let completed_token_is_exact = svc
452463
.search(&SearchQuery {
453464
community: c,
454-
q: "project pl".into(),
465+
q: "project plan".into(),
455466
channel_scope: ChannelScope::Any,
456467
kinds: Some(vec![9]),
457468
authors: None,
@@ -501,7 +512,7 @@ async fn prefix_mode_handles_tsquery_boundary_punctuation() {
501512
let result = svc
502513
.search(&SearchQuery {
503514
community: c,
504-
q: "operators ' : & | ( ) ! alpha be".into(),
515+
q: "operators ' : & | ( ) ! alpha bet".into(),
505516
channel_scope: ChannelScope::Any,
506517
kinds: Some(vec![9]),
507518
authors: None,

0 commit comments

Comments
 (0)