Skip to content

Commit 28980c1

Browse files
kalvinnchauclaude
andcommitted
fix(search): rank each person by their single best profile match
min(tier), max(rank) and max(created_at) were aggregated independently, so a person could take their tier from one source and their rank from the other. pick each person's best match with distinct on before ordering and paging. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Kalvin Chau <[email protected]>
1 parent 81cd422 commit 28980c1

2 files changed

Lines changed: 39 additions & 2 deletions

File tree

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -542,8 +542,10 @@ pub async fn search_profile_names(
542542
qb.push(" AND e.channel_id IS NULL AND e.deleted_at IS NULL)");
543543
}
544544
qb.push(
545-
"), people AS (SELECT person, min(tier) AS tier, max(rank) AS rank, \
546-
max(created_at) AS created_at FROM matches GROUP BY person \
545+
// Each person ranks by their single best match, not by mixing sources.
546+
"), people AS (SELECT * FROM (SELECT DISTINCT ON (person) person, tier, \
547+
rank, created_at FROM matches \
548+
ORDER BY person, tier, rank DESC, created_at DESC) AS best \
547549
ORDER BY tier, rank DESC, created_at DESC, person LIMIT ",
548550
);
549551
qb.push_bind(per_page as i64);

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2300,6 +2300,41 @@ async fn profile_names_match_verified_names() {
23002300
.await
23012301
.is_empty());
23022302

2303+
// A person ranks by their single best match. Kim's exact kind:0 name
2304+
// outranks Ken's, and Ken's better-ranked verified match has a worse tier.
2305+
let ken = rand_bytes32();
2306+
let ken_profile = put_profile_for(&pool, c, ken, r#"{"name":"Kname"}"#, 1_700_000_100).await;
2307+
put_verified(
2308+
&pool,
2309+
c,
2310+
relay,
2311+
ken,
2312+
&[
2313+
("display_name", "Doctor Kname"),
2314+
("username", "doctor-kname"),
2315+
("full_name", "Doctor Kname"),
2316+
],
2317+
1_700_000_110,
2318+
)
2319+
.await;
2320+
let kim = rand_bytes32();
2321+
let kim_profile = put_profile_for(
2322+
&pool,
2323+
c,
2324+
kim,
2325+
r#"{"name":"Kname","display_name":"Kname"}"#,
2326+
1_700_000_000,
2327+
)
2328+
.await;
2329+
assert_eq!(
2330+
people(&pool, c, "kname", Some(relay), 1, 1).await,
2331+
vec![kim_profile]
2332+
);
2333+
assert_eq!(
2334+
people(&pool, c, "kname", Some(relay), 2, 1).await.first(),
2335+
Some(&ken_profile)
2336+
);
2337+
23032338
teardown(pool, &schema).await;
23042339
}
23052340

0 commit comments

Comments
 (0)