@@ -1508,4 +1508,57 @@ class PipelineIntegrationTests: FSTIntegrationTestCase {
1508
1508
1509
1509
TestHelper . compare ( pipelineSnapshot: snapshot, expected: expectedResults, enforceOrder: false )
1510
1510
}
1511
+
1512
+ func testFindNearest( ) async throws {
1513
+ let collRef = collectionRef ( withDocuments: bookDocs)
1514
+ let db = collRef. firestore
1515
+
1516
+ let measures : [ DistanceMeasure ] = [ . euclidean, . dotProduct, . cosine]
1517
+ let expectedResults : [ [ String : Sendable ] ] = [
1518
+ [ " title " : " The Hitchhiker's Guide to the Galaxy " ] ,
1519
+ [ " title " : " One Hundred Years of Solitude " ] ,
1520
+ [ " title " : " The Handmaid's Tale " ] ,
1521
+ ]
1522
+
1523
+ for measure in measures {
1524
+ let pipeline = db. pipeline ( )
1525
+ . collection ( collRef. path)
1526
+ . findNearest (
1527
+ field: Field ( " embedding " ) ,
1528
+ vectorValue: [ 10 , 1 , 3 , 1 , 2 , 1 , 1 , 1 , 1 , 1 ] ,
1529
+ distanceMeasure: measure, limit: 3
1530
+ )
1531
+ . select ( " title " )
1532
+ let snapshot = try await pipeline. execute ( )
1533
+ TestHelper . compare ( pipelineSnapshot: snapshot, expected: expectedResults, enforceOrder: true )
1534
+ }
1535
+ }
1536
+
1537
+ func testFindNearestWithDistance( ) async throws {
1538
+ let collRef = collectionRef ( withDocuments: bookDocs)
1539
+ let db = collRef. firestore
1540
+
1541
+ let expectedResults : [ [ String : Sendable ] ] = [
1542
+ [
1543
+ " title " : " The Hitchhiker's Guide to the Galaxy " ,
1544
+ " computedDistance " : 1.0 ,
1545
+ ] ,
1546
+ [
1547
+ " title " : " One Hundred Years of Solitude " ,
1548
+ " computedDistance " : 12.041594578792296 ,
1549
+ ] ,
1550
+ ]
1551
+
1552
+ let pipeline = db. pipeline ( )
1553
+ . collection ( collRef. path)
1554
+ . findNearest (
1555
+ field: Field ( " embedding " ) ,
1556
+ vectorValue: [ 10 , 1 , 2 , 1 , 1 , 1 , 1 , 1 , 1 , 1 ] ,
1557
+ distanceMeasure: . euclidean, limit: 2 ,
1558
+ distanceField: " computedDistance "
1559
+ )
1560
+ . select ( " title " , " computedDistance " )
1561
+ let snapshot = try await pipeline. execute ( )
1562
+ TestHelper . compare ( pipelineSnapshot: snapshot, expected: expectedResults, enforceOrder: false )
1563
+ }
1511
1564
}
0 commit comments