Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

Correct the candidate limit and n-gram comments

Rafi3a9293df8c0c
diff --git a/apps/web/lib/api/partners/search/providers/shared.ts b/apps/web/lib/api/partners/search/providers/shared.ts
index 88bb72f592..a99d89c4dd 100644
--- a/apps/web/lib/api/partners/search/providers/shared.ts
+++ b/apps/web/lib/api/partners/search/providers/shared.ts
@@ -4,9 +4,11 @@ const NGRAM_SIZE = 3;
/**
* Three-character tokens, which is what lets a partial email match without a
- * leading-wildcard query. Providers package these differently — Redis Search
- * requires each one as a separate `$eq`, Turbopuffer takes them as a single
- * query string — so this returns the tokens and leaves the packaging to them.
+ * leading-wildcard query.
+ *
+ * Returns the tokens rather than a joined string because the two callers below
+ * want them differently: an indexed address is stored joined, while a query's
+ * trigrams are checked for emptiness before the caller joins them.
*/
function toNgrams(value: string): string[] {
return Array.from(
@@ -18,7 +20,7 @@ function toNgrams(value: string): string[] {
);
}
-/** N-grams of an indexed email, as the space-separated field both providers store. */
+/** N-grams of an indexed email, as the space-separated field the document stores. */
export function getEmailNgrams(email: string | null): string {
if (!email) {
return "";
diff --git a/apps/web/lib/api/partners/search/types.ts b/apps/web/lib/api/partners/search/types.ts
index d31a611870..98dd9c9f19 100644
--- a/apps/web/lib/api/partners/search/types.ts
+++ b/apps/web/lib/api/partners/search/types.ts
@@ -3,20 +3,29 @@ import { PlatformType, ProgramEnrollmentStatus } from "@prisma/client";
/**
* How many ranked enrollment IDs a provider may return for one query.
*
- * 999 because Upstash Redis Search rejects anything from 1000 up
- * (`ERR limit should be a positive number less than 1000`).
+ * 999 is inherited rather than derived: the original Redis provider rejected
+ * anything from 1000 up. Nothing enforces it now, so it is simply the ceiling
+ * this was tuned and measured against.
*
- * KNOWN LIMITATION: filters run after this cut, not before.
+ * Status, group, country, and tags are applied by the provider *before* this
+ * cut, because they are indexed as filterable attributes. The filters the
+ * document does not carry are applied by the database *after* it, over an
+ * already truncated list:
*
- * 1. the provider ranks matches and keeps the top 999, ignoring all filters
- * 2. the database applies status/group/country/tag/metric to those 999
+ * - tenant ID and explicit partner IDs
+ * - the referral filter
+ * - every metric range, deliberately absent from the document because
+ * metrics move on each click and would churn it continuously
*
- * A query matching fewer than 999 is unaffected. Beyond that, both the rows and
- * the count are a floor, and nothing reports that — neither response has a
- * field for it that could be added without breaking its shape.
- *
- * The fix is indexing the filterable fields so the provider filters before it
- * ranks, which means keeping them in sync on every write — out of scope here.
+ * A query still matching more than 999 documents after the provider's filters
+ * reports a floor rather than a total, for both the rows and the count, and
+ * neither response has a field that could surface it without changing shape.
+ * That is an accepted trade: a search matching that many partners is one being
+ * refined rather than read, while raising the ceiling is paid on every query,
+ * in a larger `top_k` across each ranked branch, a longer `IN` list for the
+ * database, and more rows ordered in memory for a relevance sort. Narrowing by
+ * a database-only filter lowers the floor further, since it removes rows the
+ * cut had already kept.
*/
export const PARTNER_SEARCH_CANDIDATE_LIMIT = 999;