Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

feat(search): rank every command k result type with one scorer (#127831)

Shy Altera9ef2e97806e
diff --git a/frontend/src/lib/components/CommandKSearch/COMMAND_K_SEARCH_UX.md b/frontend/src/lib/components/CommandKSearch/COMMAND_K_SEARCH_UX.md
index c3e258410b08..c892d7201674 100644
--- a/frontend/src/lib/components/CommandKSearch/COMMAND_K_SEARCH_UX.md
+++ b/frontend/src/lib/components/CommandKSearch/COMMAND_K_SEARCH_UX.md
@@ -111,6 +111,15 @@ The input is always in exactly one mode, decided from the token under the cursor
Results for a search with words follow ordered tie-breaking rules: words matched, typos, word proximity, name before folder, exact words, word position, exactness, the person's recent views, then recency. Products rank with the same rules through their name and search keywords, so "recordings" finds Session replay. When a name match and a search keyword match tie, the name match ranks first, so "metrics" puts Metrics above Data catalog.
- Accounts rank with the results when account search is enabled. The Accounts API matches the full search text, so an account can suppress the partial-match or spelling notice even when its display name does not contain the text. Distinct accounts with the same name stay separate. Accounts get no file-system **See all** filter.
+- In the test group of the `command-k-unified-ranking` experiment, one scorer ranks every result type with the same rules.
+ The sources only find candidates. The scorer then orders them by words matched, typos, match tier, the person's recent views, word proximity, word position, a type preference, then recency.
+ Match tiers, best first: exact name, exact name in singular, exact search keyword, name starts with the search, the same in singular, every word starts a name word, every word starts a name or keyword word, a match on an identity field (account external ID, website domain or email domain, person distinct ID), a match inside a word, then a match in the folder or type only.
+ Names compare as words, split on hyphens and underscores, so `checkout flow` matches the flags `checkout-flow-redesign` and `checkout_flow_redesign` as names that start with the search.
+ When every source answers and nothing matches, Cmd+K tries the search once more with underscores as spaces and says so ("No matches for **posthog_personal_api_key**. Showing results for **posthog personal api key**."), so a pasted variable name finds Personal API keys.
+ An account whose display name does not contain the search ranks as an identity match or a match inside a word, not as an exact match.
+ Products rank three match tiers higher, so a product whose name starts with the search ranks with an exact saved object name, and "replay" puts Replay vision above a notebook named Replay.
+ The type preference breaks ties: a search that looks like a domain or an email address prefers accounts and persons, and any other search prefers products, then saved objects and settings, then accounts and persons.
+ An account with no Salesforce, HubSpot deal or billing ID, no tag and no notebook ranks three match tiers lower, so a generic account name such as "Studio" does not lead an equally good saved object. A domain or email search still finds its account.
- When no result has every word, results use the words that matched. A callout above the results names the dropped word ("No matches for **xyzzy**. Showing results for **report**."). When every match needs a typo, it says "No exact matches for **dashbord**. Showing close spellings." The callout is not selectable.
- **Best matches** comes first: the three best results of different types.
- The other results follow in groups by type, each with its total count and up to three rows. A group ends with a **See all N <types>** row that adds the `is:` chip and keeps the words. A one-word search that starts a type name (`dash`, `flag`) moves that type's group up.
@@ -216,4 +225,4 @@ Every step emits an event so we can see adoption and drop-off:
- `command k search opened` (source: shortcut, button)
- `command k filter suggested` / `command k filter committed` (key, value kind, via: tab, enter, space, paste, see-all)
- `command k result opened` (section, position, has_filters, filter_keys, query_length, ranked_section: `best-matches`, `group-<type>`, or the local section)
-- `command k search abandoned` (closed with no result opened; last mode, had_filters)
+- `command k search abandoned` (closed with no result opened; last mode, had_filters, had_results: whether any result row was on screen)
diff --git a/frontend/src/lib/components/CommandKSearch/CommandKSearchNotice.tsx b/frontend/src/lib/components/CommandKSearch/CommandKSearchNotice.tsx
index a392dcfdf0fd..a50c4eb95919 100644
--- a/frontend/src/lib/components/CommandKSearch/CommandKSearchNotice.tsx
+++ b/frontend/src/lib/components/CommandKSearch/CommandKSearchNotice.tsx
@@ -22,7 +22,7 @@ export function CommandKSearchNotice({ row }: { row: NoticeRow }): JSX.Element {
<IconInfo className="size-3.5" />
</ItemMedia>
<ItemContent className="text-xs text-muted-foreground">
- {row.reason === 'partial' ? (
+ {row.reason === 'partial' || row.reason === 'corrected' ? (
<span>
No matches for <Words words={row.missing} />. Showing results for <Words words={row.shown} />.
</span>
diff --git a/frontend/src/lib/components/CommandKSearch/commandKCorrection.test.ts b/frontend/src/lib/components/CommandKSearch/commandKCorrection.test.ts
new file mode 100644
index 000000000000..e4c0a2bbe955
--- /dev/null
+++ b/frontend/src/lib/components/CommandKSearch/commandKCorrection.test.ts
@@ -0,0 +1,11 @@
+import { correctedSearch } from './commandKCorrection'
+
+describe('correctedSearch', () => {
+ test.each([
+ ['posthog_personal_api_key', 'posthog personal api key'],
+ ['api', null],
+ ['_', null],
+ ])('corrects %s to %s', (text, expected) => {
+ expect(correctedSearch(text)).toBe(expected)
+ })
+})
diff --git a/frontend/src/lib/components/CommandKSearch/commandKCorrection.ts b/frontend/src/lib/components/CommandKSearch/commandKCorrection.ts
new file mode 100644
index 000000000000..9c6044dd2355
--- /dev/null
+++ b/frontend/src/lib/components/CommandKSearch/commandKCorrection.ts
@@ -0,0 +1,10 @@
+export interface SearchCorrection {
+ from: string
+ to: string
+}
+
+/** The search to try when the typed text finds nothing: a pasted snake_case name split into words. */
+export function correctedSearch(text: string): string | null {
+ const split = text.replace(/_/g, ' ').replace(/\s+/g, ' ').trim()
+ return split !== text && split ? split : null
+}
diff --git a/frontend/src/lib/components/CommandKSearch/commandKRankedResults.test.ts b/frontend/src/lib/components/CommandKSearch/commandKRankedResults.test.ts
index c29a00cadd61..dfb488a8c7fe 100644
--- a/frontend/src/lib/components/CommandKSearch/commandKRankedResults.test.ts
+++ b/frontend/src/lib/components/CommandKSearch/commandKRankedResults.test.ts
@@ -34,7 +34,14 @@ const product = (name: string, searchKeywords: string[] = []): SearchItem => ({
const exactWordRank = (created: number): number[] => [-1, 0, 0, 0, -1, 0, 2, 0, -created]
-const account = (id: string, name: string): SearchItem => ({ id, name, category: 'accounts', itemType: 'account' })
+const account = (id: string, name: string, identityFields: string[] = [], isCurated = false): SearchItem => ({
+ id,
+ name,
+ category: 'accounts',
+ itemType: 'account',
+ identityFields,
+ isCurated,
+})
const summary = (sections: CommandKSection[]): string[] =>
sections.map((section) => {
@@ -223,7 +230,129 @@ describe('buildRankedResults', () => {
],
},
- ])('$name', ({ query, results, products, persons = [], accounts = [], expected }) => {
+ {
+ name: 'with unified ranking, an account that matches only inside a word ranks below a name match',
+ query: 'stripe',
+ results: [result('1', 'Stripe revenue', 'insight', [-1, 0, 0, 0, -1, 0, 1, 0, -1])],
+ products: [],
+ accounts: [account('account-1', 'Pinstripe Example Co')],
+ unifiedRanking: true,
+ expected: ['Results: Stripe revenue, Pinstripe Example Co'],
+ },
+ {
+ name: 'with unified ranking, a domain search puts the account that owns the domain first',
+ query: 'example.org',
+ results: [result('1', 'Example org signups', 'insight', [-2, 0, 0, 0, -2, 0, 1, 0, -1])],
+ products: [],
+ accounts: [account('account-1', 'Acme', ['example.org'])],
+ unifiedRanking: true,
+ expected: ['Results: Acme, Example org signups'],
+ },
+ {
+ name: 'with unified ranking, a hyphenated flag key that starts with the search words ranks first',
+ query: 'checkout flow',
+ results: [
+ result('1', 'Flow of checkout events', 'insight', [-2, 0, 0, 0, -2, 0, 2, 0, -2]),
+ result('2', 'checkout-flow-redesign', 'feature_flag', [-2, 0, 0, 0, -2, 0, 5, 0, -1]),
+ ],
+ products: [],
+ unifiedRanking: true,
+ expected: ['Results: checkout-flow-redesign, Flow of checkout events'],
+ },
+ {
+ name: 'with unified ranking, an account with a generic name and no CRM record ranks below a saved object',
+ query: 'studio',
+ results: [result('1', 'Studio review', 'notebook', [-1, 0, 0, 0, -1, 0, 1, 0, -1])],
+ products: [],
+ accounts: [account('account-1', 'Studio')],
+ unifiedRanking: true,
+ expected: ['Results: Studio review, Studio'],
+ },
+ {
+ name: 'with unified ranking, a curated account with the same name still ranks first',
+ query: 'studio',
+ results: [result('1', 'Studio review', 'notebook', [-1, 0, 0, 0, -1, 0, 1, 0, -1])],
+ products: [],
+ accounts: [account('account-1', 'Studio', [], true)],
+ unifiedRanking: true,
+ expected: ['Results: Studio, Studio review'],
+ },
+ {
+ name: 'with unified ranking, a snake_case name splits into words like a hyphenated one',
+ query: 'checkout flow',
+ results: [
+ result('1', 'Flow of checkout redesign', 'insight', [-2, 0, 0, 0, -2, 0, 2, 0, -2]),
+ result('2', 'checkout_flow_redesign', 'feature_flag', [-2, 0, 0, 0, -2, 0, 5, 0, -1]),
+ ],
+ products: [],
+ unifiedRanking: true,
+ expected: ['Results: checkout_flow_redesign, Flow of checkout redesign'],
+ },
+ {
+ name: 'with unified ranking, a product that starts with the search ranks above an exact saved object name',
+ query: 'replay',
+ results: [result('1', 'Replay', 'notebook', [-1, 0, 0, 0, -1, 0, 0, 0, -1])],
+ products: [product('Session replay'), product('Replay vision')],
+ unifiedRanking: true,
+ expected: ['Results: Replay vision, Replay, Session replay'],
+ },
+ {
+ name: 'with unified ranking, an exact folder match beats a name that needs a typo',
+ query: 'revenue',
+ results: [
+ result('1', 'Revenue board', 'dashboard', [-1, 0, 0, 0, -1, 0, 1, 0, -2]),
+ {
+ ...result('2', 'Revenge', 'insight', [-1, 0, 0, 1, -1, 0, 5, 0, -1]),
+ record: { type: 'insight', ref: '2', path: 'Revenue/Revenge' },
+ },
+ ],
+ products: [],
+ unifiedRanking: true,
+ expected: ['Results: Revenue board, Revenge'],
+ },
+ {
+ name: 'with unified ranking, a match inside a word beats a word that needs a typo',
+ query: 'stripe',
+ results: [result('1', 'Stripe revenue', 'insight', [-1, 0, 0, 0, -1, 0, 1, 0, -1])],
+ products: [],
+ accounts: [account('account-1', 'Pinstripe Strive')],
+ unifiedRanking: true,
+ expected: ['Results: Stripe revenue, Pinstripe Strive'],
+ },
+ {
+ name: 'with unified ranking, a keyword with fewer typos wins over an earlier keyword',
+ query: 'lajehouse',
+ results: [result('1', 'Warehouse costs', 'insight', [-1, 1, 0, 0, -1, 0, 5, 0, -1])],
+ products: [product('Data ops', ['managed warehouse', 'lakehouse'])],
+ unifiedRanking: true,
+ expected: ['Results: Data ops'],
+ },
+ {
+ name: 'with unified ranking, an account the API matched keeps zero typos over a fuzzy name',
+ query: 'stripe',
+ results: [result('1', 'Ada stripe com signups', 'insight', [-1, 0, 0, 0, -1, 1, 2, 0, -1])],
+ products: [],
+ accounts: [account('account-1', 'Strive')],
+ unifiedRanking: true,
+ expected: ['Results: Ada stripe com signups, Strive'],
+ },
+ {
+ name: 'with unified ranking, a search keyword still matches with a typo',
+ query: 'recordigns',
+ results: [],
+ products: [product('Session replay', ['recordings']), product('Surveys')],
+ unifiedRanking: true,
+ expected: ['Results: Session replay'],
+ },
+ {
+ name: 'with unified ranking, an exact name ranks above an exact search keyword',
+ query: 'metrics',
+ results: [],
+ products: [product('Data catalog', ['metrics']), product('Metrics')],
+ unifiedRanking: true,
+ expected: ['Results: Metrics, Data catalog'],
+ },
+ ])('$name', ({ query, results, products, persons = [], accounts = [], unifiedRanking = false, expected }) => {
expect(
summary(
buildRankedResults({
@@ -235,6 +364,7 @@ describe('buildRankedResults', () => {
accounts,
state: 'ready',
scoped: false,
+ unifiedRanking,
})
)
).toEqual(expected)
diff --git a/frontend/src/lib/components/CommandKSearch/commandKRankedResults.ts b/frontend/src/lib/components/CommandKSearch/commandKRankedResults.ts
index 7e220e90b1a1..dc1d459d7cd6 100644
--- a/frontend/src/lib/components/CommandKSearch/commandKRankedResults.ts
+++ b/frontend/src/lib/components/CommandKSearch/commandKRankedResults.ts
@@ -3,7 +3,18 @@ import { SearchItem } from 'lib/components/Search/searchItems'
import { capitalizeFirstLetter } from 'lib/utils/strings'
import { pluralTypeName } from './commandKFilterOptions'
-import { RankKey, compareRanks, rankLocalItem, searchWords, tokenTypos } from './commandKRanking'
+import {
+ RankCandidate,
+ RankKey,
+ SERVER_CREATED_INDEX,
+ SERVER_FRECENCY_INDEX,
+ compareRanks,
+ isIdentityQuery,
+ rankCandidate,
+ rankLocalItem,
+ searchWords,
+ tokenTypos,
+} from './commandKRanking'
import type { CommandKRow, CommandKSection } from './commandKSections'
const BEST_MATCHES_LIMIT = 3
@@ -86,6 +97,67 @@ function rankAccounts(accounts: SearchItem[], query: string): RankedItem[] {
})
}
+const IDENTITY_GROUPS = ['accounts', 'persons']
+const PRODUCT_TIER_BOOST = 3
+const UNCURATED_ACCOUNT_TIER_PENALTY = 3
+// Ties go to products, then saved objects and settings, then accounts and persons.
+const TYPE_ORDER: Record<string, number> = { products: 0, accounts: 2, persons: 2 }
+
+const displayName = (item: SearchItem): string => String(item.displayName || item.name)
+
+const typePriorFor = (groupKey: string, query: string): number =>
+ isIdentityQuery(query) ? Number(!IDENTITY_GROUPS.includes(groupKey)) : (TYPE_ORDER[groupKey] ?? 1)
+
+function tierBoostFor(item: SearchItem, groupKey: string, query: string): number {
+ if (groupKey === 'products') {
+ return PRODUCT_TIER_BOOST
+ }
+ const uncuratedAccount = groupKey === 'accounts' && !item.isCurated
+ return uncuratedAccount && !isIdentityQuery(query) ? -UNCURATED_ACCOUNT_TIER_PENALTY : 0
+}
+
+function candidateFor(item: SearchItem, groupKey: string, query: string): RankCandidate {
+ const common = {
+ name: displayName(item),
+ typePrior: typePriorFor(groupKey, query),
+ identity: item.identityFields,
+ }
+ if (item.category !== 'results') {
+ return {
+ ...common,
+ aliases: item.searchKeywords,
+ context: item.parentName ? [item.parentName] : [],
+ indirect: !!item.parentName,
+ tierBoost: tierBoostFor(item, groupKey, query),
+ matchesQuery: IDENTITY_GROUPS.includes(groupKey),
+ }
+ }
+ const path = typeof item.record?.path === 'string' ? item.record.path.split('/').slice(0, -1) : []
+ return {
+ ...common,
+ context: [...path, groupKey.replace(/[_/]/g, ' ')],
+ frecency: -(item.rankKey?.[SERVER_FRECENCY_INDEX] ?? 0) / 1000,
+ createdAt: -(item.rankKey?.[SERVER_CREATED_INDEX] ?? 0),
+ }
+}
+
+function rankUnified(items: SearchItem[], query: string, groupKey: (item: SearchItem) => string): RankedItem[] {
+ return items.flatMap((item) => {
+ const key = groupKey(item)
+ const ranked = rankCandidate(query, candidateFor(item, key, query))
+ if (!ranked) {
+ return item.rankKey ? [{ item, rank: item.rankKey, groupKey: key }] : []
+ }
+ return [
+ {
+ item: ranked.matchedAlias ? { ...item, matchedSearchKeyword: ranked.matchedAlias } : item,
+ rank: ranked.rank,
+ groupKey: key,
+ },
+ ]
+ })
+}
+
const itemRow = (
section: string,
item: SearchItem,
@@ -189,6 +261,7 @@ interface RankedResultsInput {
accounts?: SearchItem[]
state: CommandKSection['state']
scoped: boolean
+ unifiedRanking?: boolean
}
export function buildRankedResults({
@@ -201,14 +274,25 @@ export function buildRankedResults({
accounts = [],
state,
scoped,
+ unifiedRanking = false,
}: RankedResultsInput): CommandKSection[] {
- const candidates: RankedItem[] = [
- ...results.map((item) => ({ item, rank: item.rankKey ?? [], groupKey: groupKeyFor(item) })),
- ...rankLocalItems(products, query, 'products'),
- ...rankLocalItems(persons, query, 'persons'),
- ...rankLocalItems(settings, query, 'settings'),
- ...rankAccounts(accounts, query),
- ].sort((a, b) => compareRanks(a.rank, b.rank))
+ const candidates: RankedItem[] = (
+ unifiedRanking
+ ? [
+ ...rankUnified(results, query, groupKeyFor),
+ ...rankUnified(products, query, () => 'products'),
+ ...rankUnified(persons, query, () => 'persons'),
+ ...rankUnified(settings, query, () => 'settings'),
+ ...rankUnified(accounts, query, () => 'accounts'),
+ ]
+ : [
+ ...results.map((item) => ({ item, rank: item.rankKey ?? [], groupKey: groupKeyFor(item) })),
+ ...rankLocalItems(products, query, 'products'),
+ ...rankLocalItems(persons, query, 'persons'),
+ ...rankLocalItems(settings, query, 'settings'),
+ ...rankAccounts(accounts, query),
+ ]
+ ).sort((a, b) => compareRanks(a.rank, b.rank))
if (!candidates.length) {
return []
}
diff --git a/frontend/src/lib/components/CommandKSearch/commandKRanking.ts b/frontend/src/lib/components/CommandKSearch/commandKRanking.ts
index e9835b33038b..3680d9b0738f 100644
--- a/frontend/src/lib/components/CommandKSearch/commandKRanking.ts
+++ b/frontend/src/lib/components/CommandKSearch/commandKRanking.ts
@@ -14,7 +14,11 @@ export const FILTER_TOKEN = new RegExp(`(^|\\s)([-!](?:${FILTER_KEY}:"[^"]*"|\\S
export const freeWords = (query: string): string[] => searchWords(query.replace(FILTER_TOKEN, ' '))
-function osaDistance(a: string, b: string): number {
+// Stops once every path is longer than limit, because a row's smallest value never shrinks.
+function osaDistance(a: string, b: string, limit = Infinity): number {
+ if (Math.abs(a.length - b.length) > limit) {
+ return limit + 1
+ }
if (!a.length) {
return b.length
}
@@ -32,6 +36,9 @@ function osaDistance(a: string, b: string): number {
}
current.push(value)
}
+ if (Math.min(...current) > limit) {
+ return limit + 1
+ }
previousPrevious = previous
previous = current
}
@@ -54,9 +61,9 @@ export function tokenTypos(token: string, word: string): number | null {
return null
}
const distance = Math.min(
- osaDistance(token, word.slice(0, token.length)),
- osaDistance(token, word),
- osaDistance(token, singular(word))
+ osaDistance(token, word.slice(0, token.length), budget),
+ osaDistance(token, word, budget),
+ osaDistance(token, singular(word), budget)
)
return distance <= budget ? distance : null
}
@@ -227,3 +234,203 @@ export function strictLocalMatches<T extends { name: string; displayName?: strin
.sort((a, b) => compareRanks(a.ranked!.rank, b.ranked!.rank))
.map(({ item }) => item)
}
+
+const UNIFIED_WORD = /[\p{L}\p{N}]+/gu
+
+const unifiedWords = (text: string): string[] => text.toLowerCase().match(UNIFIED_WORD) ?? []
+
+const unifiedSingularText = (text: string): string => unifiedWords(text).map(singular).join(' ')
+
+export const SERVER_FRECENCY_INDEX = 7
+export const SERVER_CREATED_INDEX = 8
+
+export interface RankCandidate {
+ name: string
+ aliases?: string[]
+ identity?: string[]
+ context?: string[]
+ frecency?: number
+ createdAt?: number
+ typePrior?: number
+ indirect?: boolean
+ matchesQuery?: boolean
+ tierBoost?: number
+}
+
+interface CandidateRank {
+ rank: RankKey
+ matchedAlias: string | null
+}
+
+const MatchTier = {
+ Exact: 0,
+ ExactSingular: 1,
+ ExactAlias: 2,
+ Prefix: 3,
+ PrefixSingular: 4,
+ NameWords: 5,
+ AliasWords: 6,
+ Identity: 7,
+ Partial: 8,
+ Weak: 9,
+} as const
+
+type MatchTier = (typeof MatchTier)[keyof typeof MatchTier]
+
+type Field = 'name' | 'alias' | 'identity' | 'context'
+
+interface TokenHit {
+ typos: number
+ position: number
+ field: Field
+ wordStart: boolean
+ alias?: string
+}
+
+const MAX_POSITION_PENALTY = 3
+const MIN_PARTIAL_TOKEN_LENGTH = 3
+
+export const isIdentityQuery = (query: string): boolean => /^\S*[.@]\S+$/.test(query.trim())
+
+function hitInWords(token: string, words: string[]): Omit<TokenHit, 'field'> | null {
+ let best: Omit<TokenHit, 'field'> | null = null
+ for (let position = 0; position < words.length; position++) {
+ const typos = tokenTypos(token, words[position])
+ if (typos === 0) {
+ return { typos, position, wordStart: true }
+ }
+ if (typos !== null && (!best || typos < best.typos)) {
+ best = { typos, position, wordStart: true }
+ }
+ }
+ // A literal match inside a word needs no typo, so it beats a fuzzy word match.
+ const inside = token.length < MIN_PARTIAL_TOKEN_LENGTH ? -1 : words.findIndex((word) => word.includes(token))
+ return inside >= 0 ? { typos: 0, position: inside, wordStart: false } : best
+}
+
+const isClean = (hit: Omit<TokenHit, 'field'> | null): boolean => !!hit && hit.typos === 0 && hit.wordStart
+
+function cleanHit(token: string, candidate: RankCandidate, name: Omit<TokenHit, 'field'> | null): TokenHit | null {
+ if (name && isClean(name)) {
+ return { ...name, field: 'name' }
+ }
+ for (const alias of candidate.aliases ?? []) {
+ const hit = hitInWords(token, unifiedWords(alias))
+ if (hit && isClean(hit)) {
+ return { ...hit, position: MAX_POSITION_PENALTY, field: 'alias', alias }
+ }
+ }
+ const inIdentity = (candidate.identity ?? []).some((value) => value.toLowerCase().includes(token))
+ return inIdentity ? { typos: 0, position: MAX_POSITION_PENALTY, field: 'identity', wordStart: true } : null
+}
+
+function tokenHit(token: string, candidate: RankCandidate, nameWords: string[]): TokenHit | null {
+ const name = hitInWords(token, nameWords)
+ const clean = cleanHit(token, candidate, name)
+ if (clean) {
+ return clean
+ }
+ const context = hitInWords(token, (candidate.context ?? []).flatMap(unifiedWords))
+ const hits: TokenHit[] = [
+ ...(name ? [{ ...name, field: 'name' as const }] : []),
+ ...(candidate.aliases ?? []).flatMap((alias) => {
+ const hit = hitInWords(token, unifiedWords(alias))
+ return hit ? [{ ...hit, position: MAX_POSITION_PENALTY, field: 'alias' as const, alias }] : []
+ }),
+ ...(context ? [{ ...context, position: MAX_POSITION_PENALTY, field: 'context' as const }] : []),
+ // The source already matched the whole search on fields the browser cannot see.
+ ...(candidate.matchesQuery
+ ? [{ typos: 0, position: MAX_POSITION_PENALTY, field: 'identity' as const, wordStart: true }]
+ : []),
+ ]
+ return hits.length ? hits.reduce((best, hit) => (hit.typos < best.typos ? hit : best)) : null
+}
+
+function wholeQueryTier(query: string, candidate: RankCandidate): MatchTier | null {
+ const exactQuery = unifiedWords(query).join(' ')
+ const exactName = unifiedWords(candidate.name).join(' ')
+ const singularQuery = unifiedSingularText(query)
+ const singularName = unifiedSingularText(candidate.name)
+ const rawQuery = query.trim().toLowerCase()
+ const identity = isIdentityQuery(query) ? (candidate.identity ?? []).map((value) => value.toLowerCase()) : []
+ const tiers: [MatchTier, () => boolean][] = [
+ [MatchTier.Exact, () => exactName === exactQuery || identity.includes(rawQuery)],
+ [MatchTier.ExactSingular, () => singularName === singularQuery],
+ [
+ MatchTier.ExactAlias,
+ () => (candidate.aliases ?? []).some((alias) => unifiedSingularText(alias) === singularQuery),
+ ],
+ [
+ MatchTier.Prefix,
+ () => exactName.startsWith(exactQuery) || identity.some((value) => value.startsWith(rawQuery)),
+ ],
+ [MatchTier.PrefixSingular, () => singularName.startsWith(singularQuery)],
+ ]
+ return tiers.find(([, matches]) => matches())?.[0] ?? null
+}
+
+const WORD_TIERS: [MatchTier, (hits: TokenHit[]) => boolean][] = [
+ [MatchTier.NameWords, (hits) => hits.every((hit) => hit.field === 'name' && isClean(hit))],
+ [
+ MatchTier.AliasWords,
+ (hits) => hits.every((hit) => (hit.field === 'name' || hit.field === 'alias') && isClean(hit)),
+ ],
+ [
+ MatchTier.Identity,
+ (hits) => hits.some((hit) => hit.field === 'identity') && hits.every((hit) => hit.field !== 'context'),
+ ],
+ [MatchTier.Partial, (hits) => hits.every((hit) => hit.field === 'name')],
+]
+
+function matchTier(query: string, hits: (TokenHit | null)[], candidate: RankCandidate): MatchTier {
+ const whole = wholeQueryTier(query, candidate)
+ if (whole !== null) {
+ return whole
+ }
+ if (hits.some((hit) => hit === null)) {
+ return MatchTier.Weak
+ }
+ return WORD_TIERS.find(([, matches]) => matches(hits as TokenHit[]))?.[0] ?? MatchTier.Weak
+}
+
+function proximityOf(positions: number[]): number {
+ let proximity = 0
+ for (let i = 1; i < positions.length; i++) {
+ proximity +=
+ Math.max(Math.abs(positions[i] - positions[i - 1]) - 1, 0) + (positions[i] < positions[i - 1] ? 1 : 0)
+ }
+ return Math.min(proximity, MAX_POSITION_PENALTY)
+}
+
+const usageTier = (frecency: number): number => (frecency >= 1 ? 0 : frecency > 0 ? 1 : 2)
+
+export function rankCandidate(query: string, candidate: RankCandidate): CandidateRank | null {
+ const normalizedQuery = query.trim().toLowerCase().replace(/\s+/g, ' ').slice(0, MAX_LOCAL_QUERY_LENGTH)
+ const tokens = [...new Set(unifiedWords(normalizedQuery))]
+ if (!tokens.length) {
+ return null
+ }
+ const nameWords = unifiedWords(candidate.name)
+ const hits = tokens.map((token) => tokenHit(token, candidate, nameWords))
+ const found = hits.filter((hit): hit is TokenHit => hit !== null)
+ if (!found.length) {
+ return null
+ }
+ const positions = found.filter((hit) => hit.field === 'name').map((hit) => hit.position)
+ const frecency = candidate.frecency ?? 0
+ return {
+ rank: [
+ -found.length,
+ found.reduce((sum, hit) => sum + hit.typos, 0),
+ Math.max(matchTier(normalizedQuery, hits, candidate) - (candidate.tierBoost ?? 0), 0),
+ usageTier(frecency),
+ proximityOf(positions),
+ Math.min(...positions, MAX_POSITION_PENALTY),
+ candidate.typePrior ?? 1,
+ Number(!!candidate.indirect),
+ -Math.round(frecency * 1000),
+ -(candidate.createdAt ?? 0),
+ ],
+ matchedAlias: found.find((hit) => hit.field === 'alias')?.alias ?? null,
+ }
+}
diff --git a/frontend/src/lib/components/CommandKSearch/commandKSearchLogic.test.ts b/frontend/src/lib/components/CommandKSearch/commandKSearchLogic.test.ts
index 4291b4a6dc6a..a22103f840d4 100644
--- a/frontend/src/lib/components/CommandKSearch/commandKSearchLogic.test.ts
+++ b/frontend/src/lib/components/CommandKSearch/commandKSearchLogic.test.ts
@@ -1,6 +1,10 @@
+import { FEATURE_FLAGS } from 'lib/constants'
+import { featureFlagLogic } from 'lib/logic/featureFlagLogic'
+
import { initKeaTests } from '~/test/init'
import { commandKSearchLogic } from './commandKSearchLogic'
+import type { RemoteResults } from './commandKSources'
describe('commandKSearchLogic', () => {
let logic: ReturnType<typeof commandKSearchLogic.build>
@@ -54,6 +58,43 @@ describe('commandKSearchLogic', () => {
expect(logic.values.highlightedRow).toBe(rows[rows.length - 1])
})
+ test.each([
+ ['answered with nothing', false, { from: 'zzq_xyw', to: 'zzq xyw' }],
+ ['answered, but one failed', true, null],
+ ])('corrects a search only after every source %s', (_, oneFailed, expected) => {
+ featureFlagLogic.actions.setFeatureFlags([], { [FEATURE_FLAGS.COMMAND_K_UNIFIED_RANKING]: 'test' })
+ logic.actions.inputChanged('zzq_xyw', 7, false)
+ const sources = Object.keys(logic.values.remoteQueries)
+ expect(sources.length).toBeGreaterThan(1)
+
+ logic.actions.remoteLoaded('zzq_xyw', { results: [] })
+ expect(logic.values.searchCorrection).toBeNull()
+
+ sources.forEach((source, index) => {
+ logic.actions.remoteLoaded(
+ 'zzq_xyw',
+ { [source]: [] } as RemoteResults,
+ oneFailed && index === sources.length - 1
+ )
+ })
+ expect(logic.values.searchCorrection).toEqual(expected)
+ })
+
+ it('searches a correction until the text changes', () => {
+ logic.actions.inputChanged('zzq_xyw', 7, false)
+ logic.actions.applyCorrection({ from: 'zzq_xyw', to: 'zzq xyw' })
+
+ expect(logic.values.localQuery).toEqual('zzq xyw')
+ expect(logic.values.requestQueries.objects).toEqual('zzq xyw')
+
+ logic.actions.setChipsAndText([{ key: 'is', value: 'insight', label: 'Insight', negated: false }], 'zzq_xyw', 8)
+ expect(logic.values.requestQueries.objects).toEqual('type:insight zzq xyw')
+
+ logic.actions.setChipsAndText([], 'zzq_xywx', 8)
+
+ expect(logic.values.localQuery).toEqual('zzq_xywx')
+ })
+
it('a See all row narrows the search to its type and keeps the words', () => {
logic.actions.inputChanged('revenue', 7, false)
diff --git a/frontend/src/lib/components/CommandKSearch/commandKSearchLogic.tsx b/frontend/src/lib/components/CommandKSearch/commandKSearchLogic.tsx
index ea6355a1a528..9f46dc3bc1db 100644
--- a/frontend/src/lib/components/CommandKSearch/commandKSearchLogic.tsx
+++ b/frontend/src/lib/components/CommandKSearch/commandKSearchLogic.tsx
@@ -40,6 +40,7 @@ import type { GroupType, GroupTypeIndex, OrganizationMemberType, UserType } from
import type { FeatureFlagsSet } from '../../logic/featureFlagLogic'
import type { CommandOpenSource } from '../Command/commandLogic'
import { askAiQuestionFor } from './commandKAskAi'
+import { SearchCorrection, correctedSearch } from './commandKCorrection'
import { buildFilterOptions, viewAllFor } from './commandKFilterOptions'
import { ChipSelection, KeyIntent } from './commandKKeys'
import {
@@ -61,6 +62,7 @@ import {
resolveQuery,
tokenAtCursor,
} from './commandKQuery'
+import { freeWords } from './commandKRanking'
import {
CommandKMode,
CommandKRow,
@@ -76,6 +78,7 @@ import {
isSelectable,
pickDisplayedQuery,
rankableSettings,
+ settingsQuery,
resultsSettled,
themeSearchItem,
} from './commandKSections'
@@ -84,6 +87,7 @@ import { REMOTE_REQUESTS, RemoteContext, RemoteResults, RemoteSource, settleRequ
const REMOTE_DEBOUNCE_MS = 150
const OBJECTS_DEBOUNCE_MS = 50
const FAST_REQUESTS = ['objects', 'persons']
+
const INPUT_HIGHLIGHT_KEY = 'input'
export type CommitVia = 'tab' | 'enter' | 'space' | 'paste' | 'click' | 'typed' | 'see-all'
@@ -137,6 +141,7 @@ export interface commandKSearchLogicValues {
requestedQueries: Record<string, string>
resolvedQuery: ResolvedQuery
resultOpened: boolean
+ searchCorrection: SearchCorrection | null
sections: CommandKSection[]
selectableRows: CommandKRow[]
shownQuery: SectionsQuery | null
@@ -187,6 +192,9 @@ export interface commandKSearchLogicActions {
row: CommandKRow
via: CommitVia
}
+ applyCorrection: (correction: SearchCorrection) => {
+ correction: SearchCorrection
+ }
applyKeyIntent: (intent: KeyIntent) => {
intent: KeyIntent
}
@@ -290,7 +298,11 @@ export interface commandKSearchLogicMeta {
cursorContext: (text: string, cursor: number, chips: QueryChip[]) => CursorContext
mode: (cursorContext: CursorContext) => CommandKMode
resolvedQuery: (text: string, chips: QueryChip[], filterOptions: FilterOptions) => ResolvedQuery
- localQuery: (resolvedQuery: ResolvedQuery, mode: CommandKMode) => string
+ localQuery: (
+ resolvedQuery: ResolvedQuery,
+ mode: CommandKMode,
+ searchCorrection: SearchCorrection | null
+ ) => string
askAiQuestion: (text: string, resolvedQuery: ResolvedQuery) => string | null
remoteContext: (
currentTeamId: number | null,
@@ -302,7 +314,8 @@ export interface commandKSearchLogicMeta {
resolvedQuery: ResolvedQuery,
localQuery: string,
mode: CommandKMode,
- remoteContext: RemoteContext
+ remoteContext: RemoteContext,
+ searchCorrection: SearchCorrection | null
) => Record<string, string>
remoteQueries: (requestQueries: Record<string, string>) => Partial<Record<RemoteSource, string>>
suggestionsSection: (
@@ -356,7 +369,8 @@ export interface commandKSearchLogicMeta {
query: SectionsQuery
stale: boolean
},
- featureFlags: FeatureFlagsSet
+ featureFlags: FeatureFlagsSet,
+ searchCorrection: SearchCorrection | null
) => CommandKSection[]
selectableRows: (sections: CommandKSection[]) => CommandKRow[]
highlightedRow: (selectableRows: CommandKRow[], highlightedKey: string | null) => CommandKRow | null
@@ -448,6 +462,7 @@ export const commandKSearchLogic = kea<commandKSearchLogicType>([
loadFolders: true,
foldersLoaded: (folders: FileSystemEntry[]) => ({ folders }),
markResultOpened: true,
+ applyCorrection: (correction: SearchCorrection) => ({ correction }),
}),
reducers({
text: [
@@ -542,6 +557,13 @@ export const commandKSearchLogic = kea<commandKSearchLogicType>([
markResultOpened: () => true,
},
],
+ searchCorrection: [
+ null as SearchCorrection | null,
+ {
+ applyCorrection: (_, { correction }) => correction,
+ setChipsAndText: (state, { text }) => (state?.from === text.trim() ? state : null),
+ },
+ ],
}),
selectors({
filterOptions: [
@@ -567,9 +589,14 @@ export const commandKSearchLogic = kea<commandKSearchLogicType>([
],
/** The free text the non-object sections search for. Empty while a filter limits the palette to objects. */
localQuery: [
- (s) => [s.resolvedQuery, s.mode],
- (resolved: ResolvedQuery, mode: CommandKMode): string =>
- (mode === 'search' || mode === 'key') && resolved.filters.length === 0 ? resolved.freeText.trim() : '',
+ (s) => [s.resolvedQuery, s.mode, s.searchCorrection],
+ (resolved: ResolvedQuery, mode: CommandKMode, correction: SearchCorrection | null): string => {
+ const query =
+ (mode === 'search' || mode === 'key') && resolved.filters.length === 0
+ ? resolved.freeText.trim()
+ : ''
+ return query && correction?.from === query ? correction.to : query
+ },
],
askAiQuestion: [
(s) => [s.text, s.resolvedQuery],
@@ -591,21 +618,27 @@ export const commandKSearchLogic = kea<commandKSearchLogicType>([
],
/** The query each request runs for this input, for the requests that apply to it. */
requestQueries: [
- (s) => [s.resolvedQuery, s.localQuery, s.mode, s.remoteContext],
+ (s) => [s.resolvedQuery, s.localQuery, s.mode, s.remoteContext, s.searchCorrection],
(
resolved: ResolvedQuery,
localQuery: string,
mode: CommandKMode,
- context: RemoteContext
+ context: RemoteContext,
+ correction: SearchCorrection | null
): Record<string, string> => {
+ // Filters come first in backendSearch, so the corrected words replace its end.
+ const corrects =
+ correction?.from === resolved.freeText.trim() && resolved.backendSearch.endsWith(correction.from)
+ const objectsQuery = corrects
+ ? resolved.backendSearch.slice(0, -correction.from.length) + correction.to
+ : resolved.backendSearch
const queries: Record<string, string> = {}
// Every request searches the current project, so none runs before one is known.
if (context.currentTeamId === null) {
return queries
}
for (const request of REMOTE_REQUESTS) {
- const query =
- request.query === 'objects' ? (mode === 'empty' ? '' : resolved.backendSearch) : localQuery
+ const query = request.query === 'objects' ? (mode === 'empty' ? '' : objectsQuery) : localQuery
if (query && (request.enabled?.(query, context) ?? true)) {
queries[request.id] = query
}
@@ -719,6 +752,7 @@ export const commandKSearchLogic = kea<commandKSearchLogicType>([
s.remote,
s.displayedQuery,
s.featureFlags,
+ s.searchCorrection,
],
(
mode: CommandKMode,
@@ -727,9 +761,13 @@ export const commandKSearchLogic = kea<commandKSearchLogicType>([
local: LocalLists,
remote: RemoteSlots,
displayed: { query: SectionsQuery; stale: boolean },
- featureFlags: FeatureFlagsSet
- ): CommandKSection[] =>
- buildSections({
+ featureFlags: FeatureFlagsSet,
+ searchCorrection: SearchCorrection | null
+ ): CommandKSection[] => {
+ const unifiedRanking =
+ freeWords(displayed.query.remoteQueries.results ?? '').length > 0 &&
+ featureFlags[FEATURE_FLAGS.COMMAND_K_UNIFIED_RANKING] === 'test'
+ return buildSections({
mode,
context,
suggestions,
@@ -739,9 +777,18 @@ export const commandKSearchLogic = kea<commandKSearchLogicType>([
stale: displayed.stale,
personsFirst: !!displayed.query.remoteQueries.persons?.includes('@'),
settingsInResults:
- rankableSettings(local, displayed.query.localQuery).length > 0 &&
+ rankableSettings(local, settingsQuery(displayed.query.localQuery, unifiedRanking)).length > 0 &&
featureFlags[FEATURE_FLAGS.COMMAND_K_SETTINGS_IN_RESULTS] === 'test',
- }),
+ unifiedRanking,
+ searchCorrection:
+ searchCorrection &&
+ (displayed.query.remoteQueries.results ?? displayed.query.localQuery).endsWith(
+ searchCorrection.to
+ )
+ ? searchCorrection
+ : null,
+ })
+ },
],
selectableRows: [
(s) => [s.sections],
@@ -770,6 +817,30 @@ export const commandKSearchLogic = kea<commandKSearchLogicType>([
],
}),
listeners(({ actions, values, cache }) => {
+ // Retries a corrected search only once every source has answered and nothing matched.
+ const correctWhenEmpty = (): void => {
+ const query = values.localQuery
+ const corrected = !values.searchCorrection && query ? correctedSearch(query) : null
+ const settled = Object.entries(values.remoteQueries).every(
+ ([source, sourceQuery]) => !sourceQuery || sourceQuery in values.remote[source as RemoteSource].cache
+ )
+ const failed = Object.entries(values.remoteQueries).some(
+ ([source, sourceQuery]) =>
+ !!sourceQuery && !!values.remote[source as RemoteSource].failed?.[sourceQuery]
+ )
+ const empty = !values.sections.some((section) => section.rows.some((row) => row.kind === 'item'))
+ if (
+ corrected &&
+ settled &&
+ !failed &&
+ !values.displayedQuery.stale &&
+ empty &&
+ values.featureFlags[FEATURE_FLAGS.COMMAND_K_UNIFIED_RANKING] === 'test'
+ ) {
+ actions.applyCorrection({ from: query, to: corrected })
+ }
+ }
+
const showSettledQuery = (): void => {
if (values.shownQuery !== values.currentQuery && resultsSettled(values.currentQuery, values.remote)) {
actions.showQuery(values.currentQuery)
@@ -830,7 +901,15 @@ export const commandKSearchLogic = kea<commandKSearchLogicType>([
}
}
+ // Cached answers arrive inside this loop, so a correction waits until the loop is done.
const syncRemote = (): void => {
+ cache.syncingRemote = true
+ startRemoteRequests()
+ cache.syncingRemote = false
+ correctWhenEmpty()
+ }
+
+ const startRemoteRequests = (): void => {
const started: Record<string, string> = {}
for (const request of REMOTE_REQUESTS) {
const query = values.requestQueries[request.id] ?? ''
@@ -1044,7 +1123,13 @@ export const commandKSearchLogic = kea<commandKSearchLogicType>([
}
},
clearQuery: () => actions.setChipsAndText([], '', 0),
- remoteLoaded: showSettledQuery,
+ remoteLoaded: () => {
+ showSettledQuery()
+ if (!cache.syncingRemote) {
+ correctWhenEmpty()
+ }
+ },
+ applyCorrection: () => syncRemote(),
loadFolders: async () => {
if (cache.foldersRequested) {
return
@@ -1071,6 +1156,7 @@ export const commandKSearchLogic = kea<commandKSearchLogicType>([
posthog.capture('command k search abandoned', {
last_mode: values.mode,
had_filters: values.chips.length > 0,
+ had_results: values.sections.some((section) => section.rows.some((row) => row.kind === 'item')),
})
}
}),
diff --git a/frontend/src/lib/components/CommandKSearch/commandKSections.test.ts b/frontend/src/lib/components/CommandKSearch/commandKSections.test.ts
index 8f7e52d9cbc3..e90ac01bad3a 100644
--- a/frontend/src/lib/components/CommandKSearch/commandKSections.test.ts
+++ b/frontend/src/lib/components/CommandKSearch/commandKSections.test.ts
@@ -55,6 +55,8 @@ const input = (mode: CommandKMode, overrides: Partial<SectionsInput>): SectionsI
stale: false,
personsFirst: false,
settingsInResults: false,
+ unifiedRanking: false,
+ searchCorrection: null,
...overrides,
})
@@ -331,6 +333,56 @@ describe('buildSections', () => {
expect(sections[0].rows).toMatchObject([{ kind: 'item', section: 'accounts', item: account }])
})
+ test.each([
+ [
+ 'results',
+ [{ ...dashboard('a', '1'), name: 'API usage', rankKey: [-1, 0, 0, 0, -1, 0, 1, 0, 0] }],
+ ['notice', 'best-matches'],
+ ],
+ ['no results', [], []],
+ ])('a search correction with %s shows the notice only above results', (_, results, keys) => {
+ const remote = emptyRemoteSlots()
+ remote.results.cache['api keys'] = results
+ const sections = buildSections(
+ input('search', {
+ remote,
+ remoteQueries: { results: 'api keys' },
+ localQuery: 'api keys',
+ searchCorrection: { from: 'api_keys', to: 'api keys' },
+ })
+ )
+
+ expect(sections.map((section) => section.key)).toEqual(keys)
+ if (keys.length) {
+ expect(sections[0].rows).toEqual([
+ expect.objectContaining({ reason: 'corrected', missing: ['api_keys'], shown: ['api keys'] }),
+ ])
+ }
+ })
+
+ it('with unified ranking, a domain search through the sections puts the account that owns the domain first', () => {
+ const account: SearchItem = {
+ id: 'account-1',
+ name: 'Acme',
+ category: 'accounts',
+ itemType: 'account',
+ identityFields: ['example.org'],
+ }
+ const remote = emptyRemoteSlots()
+ remote.results.cache['example.org'] = [
+ { ...dashboard('a', '1'), name: 'Example org signups', rankKey: [-2, 0, 0, 0, -2, 0, 1, 0, -1] },
+ ]
+ remote.accounts.cache['example.org'] = [account]
+ const current = query({
+ remoteQueries: { results: 'example.org', accounts: 'example.org' },
+ localQuery: 'example.org',
+ })
+
+ const sections = buildSections(input('search', { remote, ...current, unifiedRanking: true }))
+
+ expect(sections[0].rows[0]).toMatchObject({ kind: 'item', item: account })
+ })
+
it('shows saved objects when the Accounts request settles empty', () => {
const remote = emptyRemoteSlots()
remote.results.cache.billing = [{ ...dashboard('a', '1'), rankKey: [-1, 0, 0, 0, -1, 0, 2, 0, 0] }]
@@ -377,6 +429,32 @@ describe('buildSections', () => {
expect(firstItem.kind === 'item' ? String(firstItem.item.displayName || firstItem.item.name) : null).toBe(first)
})
+ it('with unified ranking, a pasted snake_case name ranks its matching setting first', () => {
+ const setting: SearchItem = {
+ id: 'settings-api-keys',
+ name: 'User: Personal API keys (api keys)',
+ displayName: 'Personal API keys',
+ category: 'settings',
+ }
+ const products: SearchItem[] = [
+ { id: 'product-endpoints', name: 'Endpoints', category: 'products', searchKeywords: ['api'] },
+ ]
+ const remote = emptyRemoteSlots()
+ remote.results.cache.personal_api_keys = []
+ const sections = buildSections(
+ input('search', {
+ local: { ...emptyLocal(), settings: [setting], products },
+ remote,
+ remoteQueries: { results: 'personal_api_keys' },
+ localQuery: 'personal_api_keys',
+ settingsInResults: true,
+ unifiedRanking: true,
+ })
+ )
+
+ expect(sections[0].rows[0]).toMatchObject({ kind: 'item', item: setting })
+ })
+
it('says which words were dropped when a filter is active', () => {
const remote = emptyRemoteSlots()
remote.results = {
diff --git a/frontend/src/lib/components/CommandKSearch/commandKSections.ts b/frontend/src/lib/components/CommandKSearch/commandKSections.ts
index 9b60572e9d7d..dbc700c41768 100644
--- a/frontend/src/lib/components/CommandKSearch/commandKSections.ts
+++ b/frontend/src/lib/components/CommandKSearch/commandKSections.ts
@@ -1,6 +1,7 @@
import { SearchItem, filterNewItems } from 'lib/components/Search/searchItems'
import { SETTINGS_THEME_ITEM_ID, filterSearchItems } from 'lib/components/Search/utils'
+import type { SearchCorrection } from './commandKCorrection'
import { CursorContext, FilterDefinition, FilterOptions, ValueOption, matchValueOptions } from './commandKQuery'
import { buildRankedResults } from './commandKRankedResults'
import { FILTER_TOKEN, freeWords, strictLocalMatches } from './commandKRanking'
@@ -58,7 +59,7 @@ export type CommandKRow =
}
| { kind: 'ask-ai'; key: string; question: string }
| { kind: 'scope'; key: string; type: string; label: string }
- | { kind: 'notice'; key: string; reason: 'partial' | 'typos'; missing: string[]; shown: string[] }
+ | { kind: 'notice'; key: string; reason: 'partial' | 'typos' | 'corrected'; missing: string[]; shown: string[] }
| { kind: 'loading-more'; key: string }
export interface CommandKSection {
@@ -235,6 +236,8 @@ export interface SectionsInput extends SectionsQuery {
stale: boolean
personsFirst: boolean
settingsInResults: boolean
+ unifiedRanking: boolean
+ searchCorrection: SearchCorrection | null
}
const isRanked = (query: SectionsQuery): boolean =>
@@ -303,6 +306,10 @@ const plainName = (item: SearchItem): string =>
.replace(/\s*\([^)]*\)\s*$/, '')
.trim()
+// The unified scorer splits words on underscores, so the settings it ranks are matched the same way.
+export const settingsQuery = (localQuery: string, unifiedRanking: boolean): string =>
+ unifiedRanking ? localQuery.replace(/_/g, ' ') : localQuery
+
export function rankableSettings(local: LocalLists, localQuery: string): SearchItem[] {
const productNames = new Set(local.products.map(plainName))
return exactSettingsMatches(local.settings, localQuery).filter((item) => !productNames.has(plainName(item)))
@@ -316,19 +323,23 @@ function rankedResultsSections(
state: CommandKSection['state'],
persons: SearchItem[],
accounts: SearchItem[],
- settingsInResults: boolean
+ settingsInResults: boolean,
+ unifiedRanking: boolean
): CommandKSection[] {
const words = freeWords(query)
+ // The unified scorer reads punctuation, so a domain or an email address keeps it.
+ const freeText = query.replace(FILTER_TOKEN, ' ').replace(/\s+/g, ' ').trim()
return buildRankedResults({
persons,
accounts,
- settings: settingsInResults ? rankableSettings(local, localQuery) : [],
- query: words.join(' '),
+ settings: settingsInResults ? rankableSettings(local, settingsQuery(localQuery, unifiedRanking)) : [],
+ query: unifiedRanking ? freeText : words.join(' '),
highlightWords: words,
results,
products: localQuery ? local.products : [],
state,
scoped: (query.match(FILTER_TOKEN) ?? []).some((token) => token.trim().startsWith('type:')),
+ unifiedRanking,
})
}
@@ -395,7 +406,31 @@ function lowerSections(
: all
}
+function withCorrectionNotice(sections: CommandKSection[], correction: SearchCorrection | null): CommandKSection[] {
+ if (!correction || !sections.some((section) => section.rows.some((row) => row.kind === 'item'))) {
+ return sections
+ }
+ const row: CommandKRow = {
+ kind: 'notice',
+ key: 'notice/corrected',
+ reason: 'corrected',
+ missing: [correction.from],
+ shown: [correction.to],
+ }
+ const notice = sections.find((section) => section.key === 'notice')
+ if (notice) {
+ return sections.map((section) => (section === notice ? { ...section, rows: [row, ...section.rows] } : section))
+ }
+ const index = sections.findIndex((section) => section.key !== 'filters' && section.key !== 'suggestions')
+ const at = index < 0 ? sections.length : index
+ return [...sections.slice(0, at), readySection('notice', [row]), ...sections.slice(at)]
+}
+
export function buildSections(input: SectionsInput): CommandKSection[] {
+ return withCorrectionNotice(buildSectionsForQuery(input), input.searchCorrection)
+}
+
+function buildSectionsForQuery(input: SectionsInput): CommandKSection[] {
const { mode, context, local, remote, remoteQueries, localQuery } = input
if (mode === 'empty') {
return [
@@ -455,7 +490,8 @@ export function buildSections(input: SectionsInput): CommandKSection[] {
state,
leadingPersons,
accountsQuery ? remote.accounts.cache[accountsQuery] : [],
- input.settingsInResults
+ input.settingsInResults,
+ input.unifiedRanking
)
sections.push(
...(rankedSections.some((section) => section.key === 'results')
diff --git a/frontend/src/lib/components/Search/searchItems.tsx b/frontend/src/lib/components/Search/searchItems.tsx
index 63c012156b90..6fbab7bed74c 100644
--- a/frontend/src/lib/components/Search/searchItems.tsx
+++ b/frontend/src/lib/components/Search/searchItems.tsx
@@ -39,6 +39,8 @@ export interface SearchItem {
itemType?: string | null
searchKeywords?: string[]
hiddenSearchText?: string
+ identityFields?: string[]
+ isCurated?: boolean
matchedSearchKeyword?: string | null
parentName?: string
record?: Record<string, unknown>
@@ -217,6 +219,19 @@ export const accountToSearchItem = (account: AccountApi): SearchItem => {
href: urls.customerAnalyticsAccount(account.id),
icon: <IconBuilding />,
itemType: 'account',
+ identityFields: [
+ account.external_id,
+ account.properties?.website_domain,
+ ...(account.properties?.email_domains ?? []),
+ ...(account.properties?.known_emails ?? []),
+ ].filter((value): value is string => !!value),
+ isCurated: !!(
+ account.properties?.sfdc_id ||
+ account.properties?.hubspot_deal_id ||
+ account.properties?.billing_id ||
+ account.tags?.length ||
+ account.notebooks.length
+ ),
record: { type: 'account', id: account.id },
badges: getAccountStatusTags(account).map(({ label, type, dateLabel }) => ({
label,
@@ -274,6 +289,7 @@ export const personToSearchItem = (person: SearchablePerson): SearchItem => {
category: 'persons',
href: urls.personByUUID(person.uuid),
itemType: 'person',
+ identityFields: [...(person.distinct_ids ?? [])],
record: { type: 'person', ref: person.uuid, uuid: person.uuid, distinctIds: person.distinct_ids },
}
}
diff --git a/frontend/src/lib/constants.tsx b/frontend/src/lib/constants.tsx
index e788c627b4e0..2046755e5b3d 100644
--- a/frontend/src/lib/constants.tsx
+++ b/frontend/src/lib/constants.tsx
@@ -284,6 +284,7 @@ export const FEATURE_FLAGS = {
COHORT_INLINE_CALCULATION: 'inline-cohort-calculation', // owner: #team-analytics-platform, inlines fast dynamic cohort queries instead of using precomputed cohortpeople table
COHORTS_TAXONOMIC_BASIC_LIST: 'cohorts-taxonomic-basic-list', // owner: @adamleith, picker sends ?basic=true to the cohorts list endpoint (trimmed payload: no query/groups/last_error_message/experiment_set; filters is kept)
COMMAND_K_SETTINGS_IN_RESULTS: 'command-k-settings-in-results', // owner: @puemos #team-platform-ux multivariate=control,test, experiment: rank exact-match settings with the Command K results
+ COMMAND_K_UNIFIED_RANKING: 'command-k-unified-ranking', // owner: @puemos #team-platform-ux multivariate=control,test, experiment: rank every Command K result type with one scorer
CONDENSED_FILTER_BAR: 'condensed_filter_bar', // owner: @jordanm-posthog #team-web-analytics
CREATE_FORM_TOOL: 'phai-create-form-tool', // owner: @kappa90 #team-posthog-ai
CRM_ITERATION_ONE: 'crm-iteration-one', // owner: @arthurdedeus #team-customer-analytics
diff --git a/posthog/api/search.py b/posthog/api/search.py
index 0df02cc43964..f23eae527575 100644
--- a/posthog/api/search.py
+++ b/posthog/api/search.py
@@ -1,9 +1,11 @@
import re
+import operator
from collections import defaultdict
+from functools import reduce
from typing import Any, Literal, TypedDict, cast
-from django.db.models import BigIntegerField, CharField, F, Model, OuterRef, QuerySet, Subquery, Value
-from django.db.models.functions import Cast, JSONObject
+from django.db.models import BigIntegerField, CharField, F, Model, OuterRef, Q, QuerySet, Subquery, Value
+from django.db.models.functions import Cast, JSONObject, Lower
from django.http import HttpResponse
from drf_spectacular.utils import extend_schema
@@ -400,6 +402,15 @@ def class_queryset(
# full-text search rank
if query:
+ if klass in (EventDefinition, PropertyDefinition):
+ # ts_rank keeps a name only when at least one search word matches it, so a cheap substring
+ # check runs first and the costly rank only scores names that can match, not every definition.
+ # Postgres lower() folds case as the rank does, where UPPER() would miss names such as "İstanbul".
+ words = re.findall(r"[^\W_]+", query)
+ if words:
+ qs = qs.alias(_lower_name=Lower("name")).filter(
+ reduce(operator.or_, (Q(_lower_name__contains=Lower(Value(word))) for word in words))
+ )
qs = qs.annotate(rank=build_rank(search_fields, query, config="simple"))
qs = qs.filter(rank__gt=0.05)
values.append("rank")
diff --git a/posthog/api/test/test_search.py b/posthog/api/test/test_search.py
index 800db0e179cb..12a6286c47e1 100644
--- a/posthog/api/test/test_search.py
+++ b/posthog/api/test/test_search.py
@@ -5,6 +5,8 @@ from unittest.mock import Mock
from django.db import connection
from django.test.utils import CaptureQueriesContext
+from parameterized import parameterized
+
from posthog.api.search import ENTITY_MAP, SEARCHABLE_ENTITIES, class_queryset, search_entities
from posthog.helpers.full_text_search import build_search_vector, process_query
from posthog.models import OrganizationMembership, Team, User
@@ -223,14 +225,28 @@ class TestSearch(APIBaseTest):
response = self.client.get("/api/projects/@current/search?q=%21%3A%28%29%5B%5D%26%7C%3C%3E%20str1%20str2")
self.assertEqual(response.status_code, 200)
- def test_event_definitions(self):
- EventDefinition.objects.create(name="first event", team=self.team)
- EventDefinition.objects.create(name="second event", team=self.team)
- EventDefinition.objects.create(name="third event", team=self.team)
+ @parameterized.expand(
+ [
+ ("prefix", "sec", {"second event"}),
+ ("every word", "first event", {"first event"}),
+ ("some words, as the rank allows", "first event missing", {"first event"}),
+ ("shared word", "event", {"first event", "second event", "third event"}),
+ ("word inside a snake_case name", "view", {"page_view_v2"}),
+ ("dotted capital I folded like the rank", "istanbul", {"İstanbul office"}),
+ ("no match", "missing", set()),
+ ]
+ )
+ def test_event_definitions(self, _name: str, query: str, expected: set[str]) -> None:
+ for name in ("first event", "second event", "third event", "page_view_v2", "İstanbul office"):
+ EventDefinition.objects.create(name=name, team=self.team)
- response = self.client.get("/api/projects/@current/search?q=sec&entities=event_definition")
+ with CaptureQueriesContext(connection) as queries:
+ response = self.client.get(f"/api/projects/@current/search?q={query}&entities=event_definition")
self.assertEqual(response.status_code, 200)
+ self.assertEqual({result["extra_fields"]["name"] for result in response.json()["results"]}, expected)
+ definition_sql = next(q["sql"] for q in queries.captured_queries if "posthog_eventdefinition" in q["sql"])
+ self.assertIn("LIKE", definition_sql)
def test_early_access_features(self):
EarlyAccessFeature.objects.create(name="first feature", team=self.team, stage="beta")