Skip to content

Commit e021fdf

Browse files
committed
Add proper vector value support
1 parent 07226db commit e021fdf

File tree

4 files changed

+15
-16
lines changed

4 files changed

+15
-16
lines changed

google-cloud-firestore/src/main/java/com/google/cloud/firestore/pipeline/expressions/Constant.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
import com.google.cloud.Timestamp;
88
import com.google.cloud.firestore.Blob;
99
import com.google.cloud.firestore.DocumentReference;
10+
import com.google.cloud.firestore.FieldValue;
1011
import com.google.cloud.firestore.GeoPoint;
1112
import com.google.firestore.v1.Value;
1213
import java.util.Arrays;
1314
import java.util.Date;
1415
import java.util.Map;
15-
import java.util.stream.Collectors;
1616

1717
@BetaApi
1818
public final class Constant implements Expr {
@@ -112,9 +112,8 @@ public static <T> Constant of(Map<String, T> value) {
112112
}
113113

114114
@BetaApi
115-
public static Constant ofVector(double[] value) {
116-
// Convert double array to List<Double>
117-
return new Constant(Arrays.stream(value).boxed().collect(Collectors.toList()));
115+
public static Constant vector(double[] value) {
116+
return new Constant(FieldValue.vector(value));
118117
}
119118

120119
@InternalApi

google-cloud-firestore/src/main/java/com/google/cloud/firestore/pipeline/expressions/Expr.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1084,7 +1084,7 @@ default CosineDistance cosineDistance(Expr other) {
10841084
*/
10851085
@BetaApi
10861086
default CosineDistance cosineDistance(double[] other) {
1087-
return new CosineDistance(this, Constant.ofVector(other));
1087+
return new CosineDistance(this, Constant.vector(other));
10881088
}
10891089

10901090
/**
@@ -1102,7 +1102,7 @@ default CosineDistance cosineDistance(double[] other) {
11021102
*/
11031103
@BetaApi
11041104
default EuclideanDistance euclideanDistance(double[] other) {
1105-
return new EuclideanDistance(this, Constant.ofVector(other));
1105+
return new EuclideanDistance(this, Constant.vector(other));
11061106
}
11071107

11081108
/**
@@ -1138,7 +1138,7 @@ default EuclideanDistance euclideanDistance(Expr other) {
11381138
*/
11391139
@BetaApi
11401140
default DotProductDistance dotProductDistance(double[] other) {
1141-
return new DotProductDistance(this, Constant.ofVector(other));
1141+
return new DotProductDistance(this, Constant.vector(other));
11421142
}
11431143

11441144
/**

google-cloud-firestore/src/main/java/com/google/cloud/firestore/pipeline/expressions/Function.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2277,7 +2277,7 @@ public static CosineDistance cosineDistance(Expr expr, Expr other) {
22772277
*/
22782278
@BetaApi
22792279
public static CosineDistance cosineDistance(Expr expr, double[] other) {
2280-
return new CosineDistance(expr, Constant.ofVector(other));
2280+
return new CosineDistance(expr, Constant.vector(other));
22812281
}
22822282

22832283
/**
@@ -2315,7 +2315,7 @@ public static CosineDistance cosineDistance(String field, Expr other) {
23152315
*/
23162316
@BetaApi
23172317
public static CosineDistance cosineDistance(String field, double[] other) {
2318-
return new CosineDistance(Field.of(field), Constant.ofVector(other));
2318+
return new CosineDistance(Field.of(field), Constant.vector(other));
23192319
}
23202320

23212321
/**
@@ -2353,7 +2353,7 @@ public static DotProductDistance dotProductDistance(Expr expr, Expr other) {
23532353
*/
23542354
@BetaApi
23552355
public static DotProductDistance dotProductDistance(Expr expr, double[] other) {
2356-
return new DotProductDistance(expr, Constant.ofVector(other));
2356+
return new DotProductDistance(expr, Constant.vector(other));
23572357
}
23582358

23592359
/**
@@ -2391,7 +2391,7 @@ public static DotProductDistance dotProductDistance(String field, Expr other) {
23912391
*/
23922392
@BetaApi
23932393
public static DotProductDistance dotProductDistance(String field, double[] other) {
2394-
return new DotProductDistance(Field.of(field), Constant.ofVector(other));
2394+
return new DotProductDistance(Field.of(field), Constant.vector(other));
23952395
}
23962396

23972397
/**
@@ -2429,7 +2429,7 @@ public static EuclideanDistance euclideanDistance(Expr expr, Expr other) {
24292429
*/
24302430
@BetaApi
24312431
public static EuclideanDistance euclideanDistance(Expr expr, double[] other) {
2432-
return new EuclideanDistance(expr, Constant.ofVector(other));
2432+
return new EuclideanDistance(expr, Constant.vector(other));
24332433
}
24342434

24352435
/**
@@ -2467,7 +2467,7 @@ public static EuclideanDistance euclideanDistance(String field, Expr other) {
24672467
*/
24682468
@BetaApi
24692469
public static EuclideanDistance euclideanDistance(String field, double[] other) {
2470-
return new EuclideanDistance(Field.of(field), Constant.ofVector(other));
2470+
return new EuclideanDistance(Field.of(field), Constant.vector(other));
24712471
}
24722472

24732473
/**

google-cloud-firestore/src/test/java/com/google/cloud/firestore/it/ITPipelineTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -828,10 +828,10 @@ public void testDistanceFunctions() throws Exception {
828828
.pipeline()
829829
.collection(collection.getPath())
830830
.select(
831-
cosineDistance(Constant.ofVector(sourceVector), targetVector).as("cosineDistance"),
832-
dotProductDistance(Constant.ofVector(sourceVector), targetVector)
831+
cosineDistance(Constant.vector(sourceVector), targetVector).as("cosineDistance"),
832+
dotProductDistance(Constant.vector(sourceVector), targetVector)
833833
.as("dotProductDistance"),
834-
euclideanDistance(Constant.ofVector(sourceVector), targetVector)
834+
euclideanDistance(Constant.vector(sourceVector), targetVector)
835835
.as("euclideanDistance"))
836836
.limit(1)
837837
.execute()

0 commit comments

Comments
 (0)