11/**
2- * The keyword leg of a search with no signed-in reader behind it, against real PostgreSQL: ranking
3- * every base in one statement answers what ranking each base (or all of them) in the old two-step
4- * pass did, tie groups compared as sets, and admits nothing the reader may not see.
2+ * The keyword leg of a search with no signed-in reader behind it, against real PostgreSQL: it
3+ * answers what ranking each base (or all of them) in the two-step pass does, tie groups compared
4+ * as sets, and admits nothing the reader may not see.
55 *
66 * Every chunk's vector points away from the query vector, so the vector leg of a hybrid search
77 * finds nothing under its distance threshold and the fused list is the keyword ranking alone.
@@ -11,7 +11,7 @@ import { db } from '@sim/db'
1111import { document , embedding , knowledgeBase , organization , user , workspace } from '@sim/db/schema'
1212import { deleteWorkspaceFixture } from '@sim/db/testing/workspace-fixtures'
1313import { generateId } from '@sim/utils/id'
14- import { and , desc , eq , inArray , type SQL , sql } from 'drizzle-orm'
14+ import { and , eq , inArray , type SQL , sql } from 'drizzle-orm'
1515import { afterAll , beforeAll , describe , expect , it } from 'vitest'
1616import {
1717 createKnowledgeAclFixtureIds ,
@@ -34,7 +34,7 @@ const queryVector = {
3434} as const
3535const AWAY_FROM_QUERY = [ - 1 , ...Array < number > ( DIMENSIONS - 1 ) . fill ( 0 ) ]
3636
37- /** More bases than the old pass ranked in one statement, so it took its per-base branch. */
37+ /** More bases than the pass ranks in one statement, so it takes its per-base branch. */
3838const EXTRA_BASE_COUNT = 5
3939const DOCUMENTS_PER_BASE = 3
4040const CHUNKS_PER_DOCUMENT = 2
@@ -78,11 +78,8 @@ interface RankedKeywordRow {
7878 keywordRank : number
7979}
8080
81- /**
82- * The unauthenticated keyword ranking as it stood before it moved into one statement: the oracle
83- * each case is compared against.
84- */
85- async function rankBeforeSingleStatement ( input : {
81+ /** The two-step unauthenticated keyword ranking: the oracle each case is compared against. */
82+ async function rankTwoStep ( input : {
8683 knowledgeBaseIds : string [ ]
8784 topK : number
8885 query : string
@@ -152,7 +149,7 @@ function expectSameRanking(actual: string[], reference: RankedKeywordRow[], topK
152149 expect ( cursor ) . toBe ( actual . length )
153150}
154151
155- describe ( 'unauthenticated keyword ranking in one statement ' , ( ) => {
152+ describe ( 'unauthenticated keyword ranking' , ( ) => {
156153 const ids = createKnowledgeAclFixtureIds ( )
157154 const extraBaseIds = Array . from ( { length : EXTRA_BASE_COUNT } , ( ) => generateId ( ) )
158155 const unsearchedBaseId = generateId ( )
@@ -307,6 +304,23 @@ describe('unauthenticated keyword ranking in one statement', () => {
307304
308305 await db . insert ( document ) . values ( [ ...documents , ...hiddenDocuments ] )
309306 await db . insert ( embedding ) . values ( chunks )
307+
308+ // The comparisons below test tie-breaking only if scores tie across bases at several levels.
309+ const scores = await db
310+ . select ( {
311+ rank : sql < number > `ts_rank_cd(${ embedding . contentTsv } , websearch_to_tsquery(${ FTS_CONFIG } , ${ QUERY } ))` ,
312+ bases : sql < number > `count(DISTINCT ${ embedding . knowledgeBaseId } )::int` ,
313+ } )
314+ . from ( embedding )
315+ . where (
316+ and (
317+ inArray ( embedding . knowledgeBaseId , allBaseIds ) ,
318+ sql `${ embedding . contentTsv } @@ websearch_to_tsquery(${ FTS_CONFIG } , ${ QUERY } )`
319+ )
320+ )
321+ . groupBy ( sql `1` )
322+ expect ( scores . length ) . toBeGreaterThan ( 2 )
323+ expect ( scores . filter ( ( score ) => score . bases > 1 ) . length ) . toBeGreaterThan ( 1 )
310324 } )
311325
312326 afterAll ( async ( ) => {
@@ -334,7 +348,7 @@ describe('unauthenticated keyword ranking in one statement', () => {
334348 queryVector,
335349 } )
336350 expect ( retrieval ) . toEqual ( { status : 'complete' , timedOutLegs : [ ] } )
337- const reference = await rankBeforeSingleStatement ( {
351+ const reference = await rankTwoStep ( {
338352 knowledgeBaseIds : input . knowledgeBaseIds ,
339353 topK : hybridLegTopK ( input . topK ) ,
340354 query : QUERY ,
@@ -352,28 +366,9 @@ describe('unauthenticated keyword ranking in one statement', () => {
352366 return { actual, reference }
353367 }
354368
355- it ( 'seeds scores tied across bases at several distinct levels' , async ( ) => {
356- const scores = await db
357- . select ( {
358- rank : sql < number > `ts_rank_cd(${ embedding . contentTsv } , websearch_to_tsquery(${ FTS_CONFIG } , ${ QUERY } ))` ,
359- bases : sql < number > `count(DISTINCT ${ embedding . knowledgeBaseId } )::int` ,
360- } )
361- . from ( embedding )
362- . where (
363- and (
364- inArray ( embedding . knowledgeBaseId , allBaseIds ) ,
365- sql `${ embedding . contentTsv } @@ websearch_to_tsquery(${ FTS_CONFIG } , ${ QUERY } )`
366- )
367- )
368- . groupBy ( sql `1` )
369- . orderBy ( desc ( sql `1` ) )
370- expect ( scores . length ) . toBeGreaterThan ( 2 )
371- expect ( scores . filter ( ( score ) => score . bases > 1 ) . length ) . toBeGreaterThan ( 1 )
372- } )
373-
374369 describe . each ( [
375- { label : 'every base (old per-base branch)' , baseCount : allBaseIds . length } ,
376- { label : 'two bases (old single-statement branch)' , baseCount : 2 } ,
370+ { label : 'every base (per-base branch)' , baseCount : allBaseIds . length } ,
371+ { label : 'two bases (single-statement branch)' , baseCount : 2 } ,
377372 ] ) ( '$label' , ( { baseCount } ) => {
378373 const knowledgeBaseIds = allBaseIds . slice ( 0 , baseCount )
379374
0 commit comments