@@ -429,6 +429,77 @@ describe('Firestore class', () => {
429429 expect ( explainResults . snapshot ! . data ( ) . count ) . to . equal ( 3 ) ;
430430 } ) ;
431431
432+ it ( 'can plan a vector query' , async ( ) => {
433+ const indexTestHelper = new IndexTestHelper ( firestore ) ;
434+
435+ const collectionReference = await indexTestHelper . createTestDocs ( [
436+ { foo : 'bar' } ,
437+ { foo : 'xxx' , embedding : FieldValue . vector ( [ 10 , 10 ] ) } ,
438+ { foo : 'bar' , embedding : FieldValue . vector ( [ 1 , 1 ] ) } ,
439+ { foo : 'bar' , embedding : FieldValue . vector ( [ 10 , 0 ] ) } ,
440+ { foo : 'bar' , embedding : FieldValue . vector ( [ 20 , 0 ] ) } ,
441+ { foo : 'bar' , embedding : FieldValue . vector ( [ 100 , 100 ] ) } ,
442+ ] ) ;
443+
444+ const explainResults = await indexTestHelper
445+ . query ( collectionReference )
446+ . findNearest ( 'embedding' , FieldValue . vector ( [ 1 , 3 ] ) , {
447+ limit : 10 ,
448+ distanceMeasure : 'COSINE' ,
449+ } )
450+ . explain ( { analyze : false } ) ;
451+
452+ const metrics = explainResults . metrics ;
453+
454+ const plan = metrics . planSummary ;
455+ expect ( plan ) . to . not . be . null ;
456+ expect ( Object . keys ( plan . indexesUsed ) . length ) . to . be . greaterThan ( 0 ) ;
457+
458+ expect ( metrics . executionStats ) . to . be . null ;
459+ expect ( explainResults . snapshot ) . to . be . null ;
460+ } ) ;
461+
462+ it ( 'can profile a vector query' , async ( ) => {
463+ const indexTestHelper = new IndexTestHelper ( firestore ) ;
464+
465+ const collectionReference = await indexTestHelper . createTestDocs ( [
466+ { foo : 'bar' } ,
467+ { foo : 'xxx' , embedding : FieldValue . vector ( [ 10 , 10 ] ) } ,
468+ { foo : 'bar' , embedding : FieldValue . vector ( [ 1 , 1 ] ) } ,
469+ { foo : 'bar' , embedding : FieldValue . vector ( [ 10 , 0 ] ) } ,
470+ { foo : 'bar' , embedding : FieldValue . vector ( [ 20 , 0 ] ) } ,
471+ { foo : 'bar' , embedding : FieldValue . vector ( [ 100 , 100 ] ) } ,
472+ ] ) ;
473+
474+ const explainResults = await indexTestHelper
475+ . query ( collectionReference )
476+ . findNearest ( 'embedding' , FieldValue . vector ( [ 1 , 3 ] ) , {
477+ limit : 10 ,
478+ distanceMeasure : 'COSINE' ,
479+ } )
480+ . explain ( { analyze : true } ) ;
481+
482+ const metrics = explainResults . metrics ;
483+ expect ( metrics . planSummary ) . to . not . be . null ;
484+ expect (
485+ Object . keys ( metrics . planSummary . indexesUsed ) . length
486+ ) . to . be . greaterThan ( 0 ) ;
487+
488+ expect ( metrics . executionStats ) . to . not . be . null ;
489+ const stats = metrics . executionStats ! ;
490+
491+ expect ( stats . readOperations ) . to . be . greaterThan ( 0 ) ;
492+ expect ( stats . resultsReturned ) . to . be . equal ( 5 ) ;
493+ expect (
494+ stats . executionDuration . nanoseconds > 0 ||
495+ stats . executionDuration . seconds > 0
496+ ) . to . be . true ;
497+ expect ( Object . keys ( stats . debugStats ) . length ) . to . be . greaterThan ( 0 ) ;
498+
499+ expect ( explainResults . snapshot ) . to . not . be . null ;
500+ expect ( explainResults . snapshot ! . docs . length ) . to . equal ( 5 ) ;
501+ } ) ;
502+
432503 it ( 'getAll() supports array destructuring' , ( ) => {
433504 const ref1 = randomCol . doc ( 'doc1' ) ;
434505 const ref2 = randomCol . doc ( 'doc2' ) ;
0 commit comments