@@ -354,6 +354,71 @@ public function testQueryWithEmptyResults()
354354 $ this ->assertCount (0 , $ documents );
355355 }
356356
357+ public function testQueryReturnsDistancesAsScore ()
358+ {
359+ $ queryVector = new Vector ([0.15 , 0.25 , 0.35 ]);
360+ $ queryResponse = new QueryItemsResponse (
361+ ids: [['01234567-89ab-cdef-0123-456789abcdef ' , 'fedcba98-7654-3210-fedc-ba9876543210 ' ]],
362+ embeddings: [[[0.1 , 0.2 , 0.3 ], [0.4 , 0.5 , 0.6 ]]],
363+ metadatas: [[['title ' => 'Doc 1 ' ], ['title ' => 'Doc 2 ' ]]],
364+ documents: null ,
365+ data: null ,
366+ uris: null ,
367+ distances: [[0.123 , 0.456 ]]
368+ );
369+
370+ $ collection = $ this ->createMock (CollectionResource::class);
371+ $ client = $ this ->createMock (Client::class);
372+
373+ $ client ->expects ($ this ->once ())
374+ ->method ('getOrCreateCollection ' )
375+ ->with ('test-collection ' )
376+ ->willReturn ($ collection );
377+
378+ $ collection ->expects ($ this ->once ())
379+ ->method ('query ' )
380+ ->willReturn ($ queryResponse );
381+
382+ $ store = new Store ($ client , 'test-collection ' );
383+ $ documents = iterator_to_array ($ store ->query ($ queryVector ));
384+
385+ $ this ->assertCount (2 , $ documents );
386+ $ this ->assertSame (0.123 , $ documents [0 ]->score );
387+ $ this ->assertSame (0.456 , $ documents [1 ]->score );
388+ }
389+
390+ public function testQueryReturnsNullScoreWhenDistancesNotAvailable ()
391+ {
392+ $ queryVector = new Vector ([0.15 , 0.25 , 0.35 ]);
393+ $ queryResponse = new QueryItemsResponse (
394+ ids: [['01234567-89ab-cdef-0123-456789abcdef ' ]],
395+ embeddings: [[[0.1 , 0.2 , 0.3 ]]],
396+ metadatas: [[['title ' => 'Doc 1 ' ]]],
397+ documents: null ,
398+ data: null ,
399+ uris: null ,
400+ distances: null
401+ );
402+
403+ $ collection = $ this ->createMock (CollectionResource::class);
404+ $ client = $ this ->createMock (Client::class);
405+
406+ $ client ->expects ($ this ->once ())
407+ ->method ('getOrCreateCollection ' )
408+ ->with ('test-collection ' )
409+ ->willReturn ($ collection );
410+
411+ $ collection ->expects ($ this ->once ())
412+ ->method ('query ' )
413+ ->willReturn ($ queryResponse );
414+
415+ $ store = new Store ($ client , 'test-collection ' );
416+ $ documents = iterator_to_array ($ store ->query ($ queryVector ));
417+
418+ $ this ->assertCount (1 , $ documents );
419+ $ this ->assertNull ($ documents [0 ]->score );
420+ }
421+
357422 /**
358423 * @param array{where?: array<string, string>, whereDocument?: array<string, mixed>} $options
359424 * @param array<string, mixed>|null $expectedWhere
0 commit comments