Skip to content

Commit ee92dca

Browse files
committed
change findNearest and min and max
1 parent 4aaddee commit ee92dca

File tree

7 files changed

+22
-32
lines changed

7 files changed

+22
-32
lines changed

Firestore/Swift/Source/ExprImpl.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -384,31 +384,31 @@ public extension Expression {
384384
}
385385

386386
func minimum() -> AggregateFunction {
387-
return AggregateFunction("minimum", [self])
387+
return AggregateFunction("min", [self])
388388
}
389389

390390
func maximum() -> AggregateFunction {
391-
return AggregateFunction("maximum", [self])
391+
return AggregateFunction("max", [self])
392392
}
393393

394394
// MARK: Logical min/max
395395

396396
func logicalMaximum(_ expressions: [Expression]) -> FunctionExpression {
397-
return FunctionExpression("logical_maximum", [self] + expressions)
397+
return FunctionExpression("max", [self] + expressions)
398398
}
399399

400400
func logicalMaximum(_ values: [Sendable]) -> FunctionExpression {
401401
let exprs = [self] + values.map { Helper.sendableToExpr($0) }
402-
return FunctionExpression("logical_maximum", exprs)
402+
return FunctionExpression("max", exprs)
403403
}
404404

405405
func logicalMinimum(_ expressions: [Expression]) -> FunctionExpression {
406-
return FunctionExpression("logical_minimum", [self] + expressions)
406+
return FunctionExpression("min", [self] + expressions)
407407
}
408408

409409
func logicalMinimum(_ values: [Sendable]) -> FunctionExpression {
410410
let exprs = [self] + values.map { Helper.sendableToExpr($0) }
411-
return FunctionExpression("logical_minimum", exprs)
411+
return FunctionExpression("min", exprs)
412412
}
413413

414414
// MARK: Vector Operations

Firestore/Swift/Source/SwiftAPI/Pipeline/DistanceMeasure.swift

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,11 @@ public struct DistanceMeasure: Sendable, Equatable, Hashable {
2929
case dotProduct = "dot_product"
3030
}
3131

32-
public static var euclidean: DistanceMeasure {
33-
return self.init(kind: .euclidean)
34-
}
32+
public static let euclidean: DistanceMeasure = DistanceMeasure(kind: .euclidean)
3533

36-
public static var cosine: DistanceMeasure {
37-
return self.init(kind: .cosine)
38-
}
34+
public static let cosine: DistanceMeasure = DistanceMeasure(kind: .cosine)
3935

40-
public static var dotProduct: DistanceMeasure {
41-
return self.init(kind: .dotProduct)
42-
}
36+
public static let dotProduct: DistanceMeasure = DistanceMeasure(kind: .dotProduct)
4337

4438
init(kind: Kind) {
4539
self.kind = kind

Firestore/Swift/Source/SwiftAPI/Pipeline/Ordering.swift

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,9 @@ struct Direction: Sendable, Equatable, Hashable {
3535
case descending
3636
}
3737

38-
static var ascending: Direction {
39-
return self.init(kind: .ascending, rawValue: "ascending")
40-
}
38+
static let ascending = Direction(kind: .ascending, rawValue: "ascending")
4139

42-
static var descending: Direction {
43-
return self.init(kind: .descending, rawValue: "descending")
44-
}
40+
static let descending = Direction(kind: .descending, rawValue: "descending")
4541

4642
init(kind: Kind, rawValue: String) {
4743
self.kind = kind

Firestore/Swift/Source/SwiftAPI/Pipeline/Pipeline.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -391,11 +391,11 @@ public struct Pipeline: @unchecked Sendable {
391391
///
392392
/// ```swift
393393
/// // let pipeline: Pipeline = ... // Assume pipeline from a collection with vector embeddings.
394-
/// let queryVector: [Double] = [0.1, 0.2, ..., 0.8] // Example query vector.
394+
/// let queryVector = VectorValue([0.1, 0.2, ..., 0.8]) // Example query vector.
395395
/// let nearestNeighborsPipeline = pipeline.findNearest(
396396
/// field: Field("embedding_field"), // Field containing the vector.
397397
/// vectorValue: queryVector, // Query vector for comparison.
398-
/// distanceMeasure: .COSINE, // Distance metric.
398+
/// distanceMeasure: .cosine, // Distance metric.
399399
/// limit: 10, // Return top 10 nearest neighbors.
400400
/// distanceField: "similarityScore" // Optional: field for distance score.
401401
/// )
@@ -404,13 +404,13 @@ public struct Pipeline: @unchecked Sendable {
404404
///
405405
/// - Parameters:
406406
/// - field: The `Field` containing vector embeddings.
407-
/// - vectorValue: An array of `Double` representing the query vector.
408-
/// - distanceMeasure: The `DistanceMeasure` (e.g., `.EUCLIDEAN`, `.COSINE`) for comparison.
407+
/// - vectorValue: A `VectorValue` instance representing the query vector.
408+
/// - distanceMeasure: The `DistanceMeasure` (e.g., `.euclidean`, `.cosine`) for comparison.
409409
/// - limit: Optional. Maximum number of similar documents to return.
410410
/// - distanceField: Optional. Name for a new field to store the calculated distance.
411411
/// - Returns: A new `Pipeline` object with this stage appended.
412412
public func findNearest(field: Field,
413-
vectorValue: [Double],
413+
vectorValue: VectorValue,
414414
distanceMeasure: DistanceMeasure,
415415
limit: Int? = nil,
416416
distanceField: String? = nil) -> Pipeline {

Firestore/Swift/Source/SwiftAPI/Stages.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -242,13 +242,13 @@ class FindNearest: Stage {
242242
let name: String = "findNearest"
243243
let bridge: StageBridge
244244
private var field: Field
245-
private var vectorValue: [Double]
245+
private var vectorValue: VectorValue
246246
private var distanceMeasure: DistanceMeasure
247247
private var limit: Int?
248248
private var distanceField: String?
249249

250250
init(field: Field,
251-
vectorValue: [Double],
251+
vectorValue: VectorValue,
252252
distanceMeasure: DistanceMeasure,
253253
limit: Int? = nil,
254254
distanceField: String? = nil) {
@@ -259,7 +259,7 @@ class FindNearest: Stage {
259259
self.distanceField = distanceField
260260
bridge = FindNearestStageBridge(
261261
field: field.bridge as! FieldBridge,
262-
vectorValue: VectorValue(vectorValue),
262+
vectorValue: vectorValue,
263263
distanceMeasure: distanceMeasure.kind.rawValue,
264264
limit: limit as NSNumber?,
265265
distanceField: distanceField.map { Field($0).toBridge() } ?? nil

Firestore/Swift/Tests/Integration/PipelineApiTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ final class PipelineApiTests: FSTIntegrationTestCase {
203203
func testFindNearestStage() async throws {
204204
_ = db.pipeline().collection("books").findNearest(
205205
field: Field("embedding"),
206-
vectorValue: [5.0],
206+
vectorValue: VectorValue([5.0]),
207207
distanceMeasure: .cosine,
208208
limit: 3)
209209
}

Firestore/Swift/Tests/Integration/PipelineTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1541,7 +1541,7 @@ class PipelineIntegrationTests: FSTIntegrationTestCase {
15411541
.collection(collRef.path)
15421542
.findNearest(
15431543
field: Field("embedding"),
1544-
vectorValue: [10, 1, 3, 1, 2, 1, 1, 1, 1, 1],
1544+
vectorValue: VectorValue([10, 1, 3, 1, 2, 1, 1, 1, 1, 1]),
15451545
distanceMeasure: measure, limit: 3
15461546
)
15471547
.select(["title"])
@@ -1569,7 +1569,7 @@ class PipelineIntegrationTests: FSTIntegrationTestCase {
15691569
.collection(collRef.path)
15701570
.findNearest(
15711571
field: Field("embedding"),
1572-
vectorValue: [10, 1, 2, 1, 1, 1, 1, 1, 1, 1],
1572+
vectorValue: VectorValue([10, 1, 2, 1, 1, 1, 1, 1, 1, 1]),
15731573
distanceMeasure: .euclidean, limit: 2,
15741574
distanceField: "computedDistance"
15751575
)

0 commit comments

Comments
 (0)