: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
refactor(search): use one scorer for every cmd+k section (#130819)
Shy Alter669434d91873
diff --git a/frontend/src/lib/components/CommandKSearch/COMMAND_K_SEARCH_UX.md b/frontend/src/lib/components/CommandKSearch/COMMAND_K_SEARCH_UX.mdindex d4287a3d0bcc..3fe91f115743 100644--- a/frontend/src/lib/components/CommandKSearch/COMMAND_K_SEARCH_UX.md+++ b/frontend/src/lib/components/CommandKSearch/COMMAND_K_SEARCH_UX.md@@ -94,7 +94,7 @@ The input is always in exactly one mode, decided from the token under the cursor- A search with words goes to the ranked search endpoint (`file_system/search/`). A search made only of filters goes to the list endpoint, because there are no words to rank.- Sections, in this fixed order, covering everything the current palette finds:1. **Results**: file system objects (insights, dashboards, feature flags, experiments, surveys, notebooks, cohorts, actions, early access features, replay playlists, data pipelines), and products. For a search with words, see [Ranked results](#ranked-results).- 2. In-memory lists, filtered on every keystroke: **Data management**, **People**, **Health**, **Actions** (Log out, and Toggle terminal when the terminal is on), **Shortcuts** (with their key bindings), **Settings** (with a Dark mode / Light mode row for "dark", "light", "theme", "appearance"), **Create new**. For a search with words they keep only items that match every word with no typo. Products rank with the results instead. Settings that match every word with no typo also rank with the results, unless a product has the same name and opens the same page. A setting can match a word in its name, a keyword, or its level and section (`rename project`, `project timezone`), as long as its name or a keyword holds one of the words. Matching settings rank with the results, and the **Settings** section lists only the ones that did not. When any setting ranks, the **Settings** section does not render, so a search shows one Settings group. The Dark mode / Light mode row ranks with the settings, and when the ranking leaves it out it still shows, at the top of the Settings group or in the **Settings** section. The Settings group count includes the settings held back for sharing a product's name. When the Settings group holds more settings than it shows, it ends with a **Show all N settings** row that expands the group in place and moves the highlight to the first new row. Any change to the query collapses the group again. A product tab with the same name (Workflows › Settings) does not hold a setting back, so "settings" finds the **Settings** page first.+ 2. In-memory lists, filtered on every keystroke: **Data management**, **People**, **Health**, **Actions** (Log out, and Toggle terminal when the terminal is on), **Shortcuts** (with their key bindings), **Settings** (with a Dark mode / Light mode row for "dark", "light", "theme", "appearance"), **Create new**. For a search with words they keep only items whose name or search keywords match every word at the start of a word with no typo, using the same scorer as the results, and list them in its order. Products rank with the results instead. Settings that match every word with no typo also rank with the results, unless a product has the same name and opens the same page. A setting can match a word in its name, a keyword, or its level and section (`rename project`, `project timezone`), as long as its name or a keyword holds one of the words. Matching settings rank with the results, and the **Settings** section lists only the ones that did not. When any setting ranks, the **Settings** section does not render, so a search shows one Settings group. The Dark mode / Light mode row ranks with the settings, and when the ranking leaves it out it still shows, at the top of the Settings group or in the **Settings** section. The Settings group count includes the settings held back for sharing a product's name. When the Settings group holds more settings than it shows, it ends with a **Show all N settings** row that expands the group in place and moves the highlight to the first new row. Any change to the query collapses the group again. A product tab with the same name (Workflows › Settings) does not hold a setting back, so "settings" finds the **Settings** page first.- The settings rows come from the settings scene's own map (`SETTINGS_MAP`) and the same gates the scene applies (feature flags, realm, team, admin-only, staff-only, billing access, integrations), so search never shows a setting the scene hides. Sections hidden from the settings navigation stay out of search too.- Rows: **Settings**, one landing row per level (**Project settings**, **Organization settings**, **User settings**), one row per section ("Billing" in Organization settings), and one row per setting with a readable title ("Date & time" in Project settings › Customization) that opens the section at that setting's anchor. A setting whose title matches its section's title, or whose section links to another page, folds into the section row.3. Remote lists that the file system does not hold: **Events**, **Properties**, **Workflows** (one unified search request), **Accounts** (behind the customer analytics flag, excluding accounts already shown in ranked results), **Support tickets** (3+ characters or a ticket number).diff --git a/frontend/src/lib/components/CommandKSearch/commandKRanking.test.ts b/frontend/src/lib/components/CommandKSearch/commandKRanking.test.tsindex df862a754b12..c8d3ea0c981d 100644--- a/frontend/src/lib/components/CommandKSearch/commandKRanking.test.ts+++ b/frontend/src/lib/components/CommandKSearch/commandKRanking.test.ts@@ -1,4 +1,4 @@-import { compareRanks, freeWords, highlightSegments, rankLocalItem, strictLocalMatches } from './commandKRanking'+import { compareRanks, freeWords, highlightSegments, rankCandidate, strictLocalMatches } from './commandKRanking'const marked = (name: string, words: string[]): string =>highlightSegments(name, words)@@ -16,7 +16,7 @@ describe('commandKRanking', () => {})it('ranks a pasted wall of text without failing', () => {- expect(rankLocalItem(`experiments ${'x'.repeat(35000)}`, 'Experiments')).not.toBeNull()+ expect(rankCandidate(`experiments ${'x'.repeat(35000)}`, { name: 'Experiments' })).not.toBeNull()})test.each([@@ -32,19 +32,12 @@ describe('commandKRanking', () => {expect(marked(name, words)).toEqual(expected)})- test.each([- { query: 'recordings', name: 'Session replay', aliases: ['recordings'], alias: 'recordings' },- { query: 'survey', name: 'Surveys', aliases: ['feedback'], alias: null },- ])('ranks $name for $query by its best name or alias', ({ query, name, aliases, alias }) => {- expect(rankLocalItem(query, name, aliases)?.matchedAlias).toEqual(alias)- })-test.each([{ query: 'metrics', other: 'Data catalog', aliases: ['semantic layer', 'metrics'] },{ query: 'mertics', other: 'Shared metrics', aliases: [] },])('ranks Metrics above $other for $query', ({ query, other, aliases }) => {- const metrics = rankLocalItem(query, 'Metrics', ['time series'])?.rank ?? []- expect(compareRanks(metrics, rankLocalItem(query, other, aliases)?.rank ?? [])).toBeLessThan(0)+ const metrics = rankCandidate(query, { name: 'Metrics', aliases: ['time series'] }) ?? []+ expect(compareRanks(metrics, rankCandidate(query, { name: other, aliases }) ?? [])).toBeLessThan(0)})it('matches a plural query to a singular name without a typo', () => {@@ -54,7 +47,7 @@ describe('commandKRanking', () => {})it('gives a repeated word no proximity bonus', () => {- const rank = (name: string): number[] => rankLocalItem('sign signup', name)?.rank ?? []+ const rank = (name: string): number[] => rankCandidate('sign signup', { name }) ?? []expect(compareRanks(rank('Sign signup'), rank('Signup'))).toBeLessThan(0)})})diff --git a/frontend/src/lib/components/CommandKSearch/commandKRanking.ts b/frontend/src/lib/components/CommandKSearch/commandKRanking.tsindex 398b44b205e2..aeb31ad9b29f 100644--- a/frontend/src/lib/components/CommandKSearch/commandKRanking.ts+++ b/frontend/src/lib/components/CommandKSearch/commandKRanking.ts@@ -2,9 +2,7 @@ const WORD = /[\p{L}\p{N}_]+/guexport type RankKey = number[]-const MIN_ALIAS_QUERY_LENGTH = 3export const MAX_LOCAL_QUERY_LENGTH = 200-const STARTS_WITH = 1export const searchWords = (text: string): string[] => text.toLowerCase().match(WORD) ?? []@@ -48,8 +46,6 @@ function osaDistance(a: string, b: string, limit = Infinity): number {const singular = (word: string): string =>word.length > 3 && word.endsWith('s') && !word.endsWith('ss') ? word.slice(0, -1) : word-const singularText = (text: string): string => searchWords(text).map(singular).join(' ')-const allowedTypos = (token: string): number => (token.length < 5 ? 0 : token.length < 9 ? 1 : 2)export function tokenTypos(token: string, word: string): number | null {@@ -68,89 +64,6 @@ export function tokenTypos(token: string, word: string): number | null {return distance <= budget ? distance : null}-function bestMatch(token: string, words: string[]): [number, number] | null {- let best: [number, number] | null = null- for (let position = 0; position < words.length; position++) {- const typos = tokenTypos(token, words[position])- if (typos !== null && (!best || typos < best[0])) {- best = [typos, position]- if (typos === 0) {- break- }- }- }- return best-}--const escapeRegExp = (text: string): string => text.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')--function exactness(query: string, name: string, nameWords: string[]): number {- if (name === query || singularText(name) === singularText(query)) {- return 0- }- if (name.startsWith(query)) {- return 1- }- if (nameWords.some((word) => word.startsWith(query)) || new RegExp(`(^|\\W)${escapeRegExp(query)}`).test(name)) {- return 2- }- if (name.includes(query)) {- return 3- }- if (- nameWords- .map((word) => word[0])- .join('')- .startsWith(query.replace(/ /g, ''))- ) {- return 4- }- return 5-}--function rankName(query: string, tokens: string[], name: string, indirect: number): RankKey | null {- const lowerName = name.toLowerCase()- const nameWords = searchWords(lowerName)- let matched = 0- let typos = 0- let exactWords = 0- const positions: number[] = []- for (const token of tokens) {- const hit = bestMatch(token, nameWords)- if (!hit) {- continue- }- matched++- typos += hit[0]- positions.push(hit[1])- if (tokens.length > 1 && nameWords.some((word) => singular(word) === singular(token))) {- exactWords++- }- }- if (!matched) {- return null- }- if (tokens.length === 1 && singularText(lowerName) === singularText(query)) {- exactWords = 1- }- 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 [- -matched,- typos,- proximity,- matched === tokens.length ? 0 : 1,- -exactWords,- Math.min(...positions),- Math.min(exactness(query, lowerName, nameWords), STARTS_WITH),- indirect,- -Number.MAX_SAFE_INTEGER,- ]-}-export const compareRanks = (a: RankKey, b: RankKey): number => {for (let i = 0; i < Math.min(a.length, b.length); i++) {if (a[i] !== b[i]) {@@ -160,31 +73,6 @@ export const compareRanks = (a: RankKey, b: RankKey): number => {return a.length - b.length}-interface LocalRank {- rank: RankKey- matchedAlias: string | null-}--export function rankLocalItem(query: string, name: string, aliases: string[] = []): LocalRank | null {- const normalized = query.trim().toLowerCase().replace(/\s+/g, ' ').slice(0, MAX_LOCAL_QUERY_LENGTH)- const tokens = searchWords(normalized)- if (!tokens.length) {- return null- }- let best: LocalRank | null = null- const byName = rankName(normalized, tokens, name, 0)- if (byName) {- best = { rank: byName, matchedAlias: null }- }- for (const alias of normalized.length >= MIN_ALIAS_QUERY_LENGTH ? aliases : []) {- const byAlias = rankName(normalized, tokens, alias, 1)- if (byAlias && (!best || compareRanks(byAlias, best.rank) < 0)) {- best = { rank: byAlias, matchedAlias: alias }- }- }- return best-}-interface HighlightSegment {text: stringmatch: boolean@@ -220,21 +108,6 @@ export function highlightSegments(name: string, words: string[]): HighlightSegmereturn segments}-export function strictLocalMatches<T extends { name: string; displayName?: string; searchKeywords?: string[] }>(- items: T[],- query: string-): T[] {- const words = searchWords(query).length- return items- .map((item) => ({- item,- ranked: rankLocalItem(query, String(item.displayName || item.name), item.searchKeywords),- }))- .filter(({ ranked }) => ranked !== null && -ranked.rank[0] === words && ranked.rank[1] === 0)- .sort((a, b) => compareRanks(a.ranked!.rank, b.ranked!.rank))- .map(({ item }) => item)-}-const UNIFIED_WORD = /[\p{L}\p{N}]+/guconst unifiedWords = (text: string): string[] => text.toLowerCase().match(UNIFIED_WORD) ?? []@@ -304,26 +177,27 @@ function hitInWords(token: string, words: string[]): Omit<TokenHit, 'field'> | nconst 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' }+const wordStartPosition = (token: string, words: string[]): number =>+ words.findIndex((word) => word.startsWith(token) || word === singular(token))++function cleanHit(token: string, candidate: RankCandidate, nameWords: string[]): TokenHit | null {+ const name = wordStartPosition(token, nameWords)+ if (name >= 0) {+ return { typos: 0, position: name, field: 'name', wordStart: true }}- for (const alias of candidate.aliases ?? []) {- const hit = hitInWords(token, unifiedWords(alias))- if (hit && isClean(hit)) {- return { ...hit, position: MAX_POSITION_PENALTY, field: 'alias' }- }+ if ((candidate.aliases ?? []).some((alias) => wordStartPosition(token, unifiedWords(alias)) >= 0)) {+ return { typos: 0, position: MAX_POSITION_PENALTY, field: 'alias', wordStart: true }}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) {+function tokenHit(token: string, candidate: RankCandidate, nameWords: string[], strict: boolean): TokenHit | null {+ const clean = cleanHit(token, candidate, nameWords)+ if (clean || strict) {return clean}+ const name = hitInWords(token, nameWords)const context = hitInWords(token, (candidate.context ?? []).flatMap(unifiedWords))const hits: TokenHit[] = [...(name ? [{ ...name, field: 'name' as const }] : []),@@ -398,16 +272,16 @@ function proximityOf(positions: number[]): number {const usageTier = (frecency: number): number => (frecency >= 1 ? 0 : frecency > 0 ? 1 : 2)-export function rankCandidate(query: string, candidate: RankCandidate): RankKey | null {+export function rankCandidate(query: string, candidate: RankCandidate, strict = false): RankKey | 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 hits = tokens.map((token) => tokenHit(token, candidate, nameWords, strict))const found = hits.filter((hit): hit is TokenHit => hit !== null)- if (!found.length) {+ if (!found.length || (strict && found.length < hits.length)) {return null}const positions = found.filter((hit) => hit.field === 'name').map((hit) => hit.position)@@ -442,3 +316,21 @@ export function serverOnlyRank(serverRank: RankKey, candidate: RankCandidate): R-(candidate.createdAt ?? 0),]}++export function strictLocalMatches<T extends { name: string; displayName?: string; searchKeywords?: string[] }>(+ items: T[],+ query: string+): T[] {+ return items+ .map((item) => ({+ item,+ rank: rankCandidate(+ query,+ { name: String(item.displayName || item.name), aliases: item.searchKeywords },+ true+ ),+ }))+ .filter((entry): entry is { item: T; rank: RankKey } => !!entry.rank)+ .sort((a, b) => compareRanks(a.rank, b.rank))+ .map(({ item }) => item)+}diff --git a/frontend/src/lib/components/CommandKSearch/commandKSections.test.ts b/frontend/src/lib/components/CommandKSearch/commandKSections.test.tsindex fa66825b0c6e..6350b011e20e 100644--- a/frontend/src/lib/components/CommandKSearch/commandKSections.test.ts+++ b/frontend/src/lib/components/CommandKSearch/commandKSections.test.ts@@ -717,6 +717,27 @@ describe('buildSections', () => {expect(sections[0].rows[0]).toMatchObject({ kind: 'item', item: setting })})+ it('a pasted snake_case name keeps its Health row next to a matching saved object', () => {+ const health: SearchItem = { id: 'health-pipeline-status', name: 'Pipeline status', category: 'health' }+ const remote = emptyRemoteSlots()+ remote.results.cache.pipe_stat = [+ { ...dashboard('a', '1'), name: 'Pipe stat board', rankKey: [-2, 0, 0, 0, -2, 0, 1, 0, 0] },+ ]+ const sections = buildSections(+ input('search', {+ local: { ...emptyLocal(), health: [health] },+ remote,+ remoteQueries: { results: 'pipe_stat' },+ localQuery: 'pipe_stat',+ })+ )++ const ids = sections.flatMap((section) =>+ section.rows.flatMap((row) => (row.kind === 'item' ? [row.item.id] : []))+ )+ expect(ids).toEqual(expect.arrayContaining(['a', 'health-pipeline-status']))+ })+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.tsindex fb4c35dcb768..f3c44d725dca 100644--- a/frontend/src/lib/components/CommandKSearch/commandKSections.ts+++ b/frontend/src/lib/components/CommandKSearch/commandKSections.ts@@ -309,14 +309,14 @@ function matchLocal(source: LocalSource, items: SearchItem[], query: string, ranif (cached) {return cached}- const fuzzy = (): SearchItem[] =>- source === 'create' ? filterNewItems(items, query) : filterSearchItems(items, query)const matches =strict && source === 'settings'? settingsMatches(items, query): strict- ? strictLocalMatches(fuzzy(), query)- : fuzzy()+ ? strictLocalMatches(items, query)+ : source === 'create'+ ? filterNewItems(items, query)+ : filterSearchItems(items, query)if (byQuery.size >= LOCAL_MATCH_CACHE_SIZE) {byQuery.delete(byQuery.keys().next().value!)}