Skip to content

Commit 762b005

Browse files
committed
findnearest update and length rename
1 parent a62cdde commit 762b005

File tree

8 files changed

+157
-109
lines changed

8 files changed

+157
-109
lines changed

google-cloud-firestore/src/main/java/com/google/cloud/firestore/Pipeline.java

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import com.google.cloud.firestore.pipeline.stages.Distinct;
4242
import com.google.cloud.firestore.pipeline.stages.Documents;
4343
import com.google.cloud.firestore.pipeline.stages.FindNearest;
44+
import com.google.cloud.firestore.pipeline.stages.FindNearestOptions;
4445
import com.google.cloud.firestore.pipeline.stages.GenericStage;
4546
import com.google.cloud.firestore.pipeline.stages.Limit;
4647
import com.google.cloud.firestore.pipeline.stages.Offset;
@@ -495,23 +496,29 @@ public Pipeline distinct(Selectable... selectables) {
495496
* <pre>{@code
496497
* // Find books with similar "topicVectors" to the given targetVector
497498
* firestore.pipeline().collection("books")
498-
* .findNearest("topicVectors", targetVector,
499+
* .findNearest("topicVectors", targetVector, FindNearest.DistanceMeasure.cosine(),
499500
* FindNearestOptions
500-
* .withLimitAndMeasure(10, DistanceMeasure.cosine())
501-
* .withOutputField("distance"));
501+
* .builder()
502+
* .limit(10)
503+
* .distanceField("distance")
504+
* .build());
502505
* }</pre>
503506
*
504507
* @param fieldName The name of the field containing the vector data. This field should store
505508
* {@link VectorValue}.
506509
* @param vector The target vector to compare against.
507-
* @param options Configuration options for the nearest neighbor search, such as the distance
508-
* metric to use.
510+
* @param distanceMeasure The distance measure to use: cosine, euclidean, etc.
511+
* @param options Configuration options for the nearest neighbor search, such as limit and output
512+
* distance field name.
509513
* @return A new {@code Pipeline} object with this stage appended to the stage list.
510514
*/
511515
@BetaApi
512516
public Pipeline findNearest(
513-
String fieldName, double[] vector, FindNearest.FindNearestOptions options) {
514-
return findNearest(Field.of(fieldName), vector, options);
517+
String fieldName,
518+
double[] vector,
519+
FindNearest.DistanceMeasure distanceMeasure,
520+
FindNearestOptions options) {
521+
return findNearest(Field.of(fieldName), vector, distanceMeasure, options);
515522
}
516523

517524
/**
@@ -527,29 +534,38 @@ public Pipeline findNearest(
527534
* <pre>{@code
528535
* // Find books with similar "topicVectors" to the given targetVector
529536
* firestore.pipeline().collection("books")
530-
* .findNearest(Field.of("topicVectors"), targetVector,
537+
* .findNearest(Field.of("topicVectors"), targetVector, FindNearest.DistanceMeasure.cosine(),
531538
* FindNearestOptions
532-
* .withLimitAndMeasure(10, DistanceMeasure.cosine())
533-
* .withOutputField("distance"));
539+
* .builder()
540+
* .limit(10)
541+
* .distanceField("distance")
542+
* .build());
534543
* }</pre>
535544
*
536545
* @param property The expression that evaluates to a vector value using the stage inputs.
537546
* @param vector The target vector to compare against.
538-
* @param options Configuration options for the nearest neighbor search, such as the distance
539-
* metric to use.
547+
* @param distanceMeasure The distance measure to use: cosine, euclidean, etc.
548+
* @param options Configuration options for the nearest neighbor search, such as limit and output
549+
* distance field name.
540550
* @return A new {@code Pipeline} object with this stage appended to the stage list.
541551
*/
542552
@BetaApi
543553
public Pipeline findNearest(
544-
Expr property, double[] vector, FindNearest.FindNearestOptions options) {
554+
Expr property,
555+
double[] vector,
556+
FindNearest.DistanceMeasure distanceMeasure,
557+
FindNearestOptions options) {
545558
// Implementation for findNearest (add the FindNearest stage if needed)
546559
return new Pipeline(
547560
this.db,
548561
ImmutableList.<Stage>builder()
549562
.addAll(stages)
550563
.add(
551564
new FindNearest(
552-
property, vector, options)) // Assuming FindNearest takes these arguments
565+
property,
566+
vector,
567+
distanceMeasure,
568+
options)) // Assuming FindNearest takes these arguments
553569
.build());
554570
}
555571

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
@@ -768,14 +768,14 @@ default Max max() {
768768
*
769769
* <pre>{@code
770770
* // Get the length of the 'name' field
771-
* Field.of("name").length();
771+
* Field.of("name").strLength();
772772
* }</pre>
773773
*
774774
* @return A new {@code Expr} representing the length of the string.
775775
*/
776776
@BetaApi
777-
default Length length() {
778-
return new Length(this);
777+
default StrLength strLength() {
778+
return new StrLength(this);
779779
}
780780

781781
/**

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
@@ -1496,15 +1496,15 @@ public static ArrayTransform arrayTransform(String field, Function transform) {
14961496
*
14971497
* <pre>{@code
14981498
* // Get the length of the 'name' field
1499-
* Function.length(Field.of("name"));
1499+
* Function.strLength(Field.of("name"));
15001500
* }</pre>
15011501
*
15021502
* @param expr The expression representing the string to calculate the length of.
15031503
* @return A new {@code Expr} representing the length of the string.
15041504
*/
15051505
@BetaApi
1506-
public static Length length(Expr expr) {
1507-
return new Length(expr);
1506+
public static StrLength strLength(Expr expr) {
1507+
return new StrLength(expr);
15081508
}
15091509

15101510
/**
@@ -1514,15 +1514,15 @@ public static Length length(Expr expr) {
15141514
*
15151515
* <pre>{@code
15161516
* // Get the length of the 'name' field
1517-
* Function.length("name");
1517+
* Function.strLength("name");
15181518
* }</pre>
15191519
*
15201520
* @param field The name of the field containing the string.
15211521
* @return A new {@code Expr} representing the length of the string.
15221522
*/
15231523
@BetaApi
1524-
public static Length length(String field) {
1525-
return new Length(Field.of(field));
1524+
public static StrLength strLength(String field) {
1525+
return new StrLength(Field.of(field));
15261526
}
15271527

15281528
/**

google-cloud-firestore/src/main/java/com/google/cloud/firestore/pipeline/expressions/Length.java renamed to google-cloud-firestore/src/main/java/com/google/cloud/firestore/pipeline/expressions/StrLength.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@
2121
import com.google.common.collect.Lists;
2222

2323
@BetaApi
24-
public final class Length extends Function {
24+
public final class StrLength extends Function {
2525
@InternalApi
26-
Length(Expr expr) {
26+
StrLength(Expr expr) {
2727
super("length", Lists.newArrayList(expr));
2828
}
2929
}

google-cloud-firestore/src/main/java/com/google/cloud/firestore/pipeline/stages/FindNearest.java

Lines changed: 47 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -16,47 +16,13 @@
1616

1717
package com.google.cloud.firestore.pipeline.stages;
1818

19+
import com.google.api.core.BetaApi;
1920
import com.google.api.core.InternalApi;
2021
import com.google.cloud.firestore.pipeline.expressions.Expr;
21-
import com.google.cloud.firestore.pipeline.expressions.Field;
22-
import javax.annotation.Nullable;
2322

23+
@BetaApi
2424
public final class FindNearest implements Stage {
2525

26-
private static final String name = "find_nearest";
27-
private final Expr property;
28-
private final double[] vector;
29-
private final FindNearest.FindNearestOptions options;
30-
31-
@InternalApi
32-
public FindNearest(Expr property, double[] vector, FindNearest.FindNearestOptions options) {
33-
this.property = property;
34-
this.vector = vector;
35-
this.options = options;
36-
}
37-
38-
@Override
39-
@InternalApi
40-
public String getName() {
41-
return name;
42-
}
43-
44-
@InternalApi
45-
public Expr getProperty() {
46-
return property;
47-
}
48-
49-
@InternalApi
50-
public double[] getVector() {
51-
return vector;
52-
}
53-
54-
@InternalApi
55-
public FindNearestOptions getOptions() {
56-
return options;
57-
}
58-
59-
// Nested interfaces and classes
6026
public interface DistanceMeasure {
6127

6228
enum Type {
@@ -65,27 +31,27 @@ enum Type {
6531
DOT_PRODUCT
6632
}
6733

68-
static FindNearest.DistanceMeasure euclidean() {
69-
return new FindNearest.EuclideanDistanceMeasure();
34+
static DistanceMeasure euclidean() {
35+
return new EuclideanDistanceMeasure();
7036
}
7137

72-
static FindNearest.DistanceMeasure cosine() {
73-
return new FindNearest.CosineDistanceMeasure();
38+
static DistanceMeasure cosine() {
39+
return new CosineDistanceMeasure();
7440
}
7541

76-
static FindNearest.DistanceMeasure dotProduct() {
77-
return new FindNearest.DotProductDistanceMeasure();
42+
static DistanceMeasure dotProduct() {
43+
return new DotProductDistanceMeasure();
7844
}
7945

80-
static FindNearest.DistanceMeasure generic(String name) {
81-
return new FindNearest.GenericDistanceMeasure(name);
46+
static DistanceMeasure generic(String name) {
47+
return new GenericDistanceMeasure(name);
8248
}
8349

8450
@InternalApi
8551
String toProtoString();
8652
}
8753

88-
static class EuclideanDistanceMeasure implements FindNearest.DistanceMeasure {
54+
public static class EuclideanDistanceMeasure implements DistanceMeasure {
8955

9056
@Override
9157
@InternalApi
@@ -94,7 +60,7 @@ public String toProtoString() {
9460
}
9561
}
9662

97-
static class CosineDistanceMeasure implements FindNearest.DistanceMeasure {
63+
public static class CosineDistanceMeasure implements DistanceMeasure {
9864

9965
@Override
10066
@InternalApi
@@ -103,7 +69,7 @@ public String toProtoString() {
10369
}
10470
}
10571

106-
static class DotProductDistanceMeasure implements FindNearest.DistanceMeasure {
72+
public static class DotProductDistanceMeasure implements DistanceMeasure {
10773

10874
@Override
10975
@InternalApi
@@ -112,7 +78,7 @@ public String toProtoString() {
11278
}
11379
}
11480

115-
static class GenericDistanceMeasure implements FindNearest.DistanceMeasure {
81+
public static class GenericDistanceMeasure implements DistanceMeasure {
11682

11783
String name;
11884

@@ -127,45 +93,44 @@ public String toProtoString() {
12793
}
12894
}
12995

130-
public static class FindNearestOptions {
131-
132-
private final Long limit;
133-
private final FindNearest.DistanceMeasure distanceMeasure;
134-
135-
@Nullable private final Field distanceField;
136-
137-
private FindNearestOptions(Long limit, FindNearest.DistanceMeasure distanceMeasure) {
138-
this.limit = limit;
139-
this.distanceMeasure = distanceMeasure;
140-
this.distanceField = null;
141-
}
96+
private static final String name = "find_nearest";
97+
private final Expr property;
98+
private final double[] vector;
99+
private final DistanceMeasure distanceMeasure;
100+
private final FindNearestOptions options;
142101

143-
private FindNearestOptions(
144-
Long limit, FindNearest.DistanceMeasure distanceMeasure, Field field) {
145-
this.limit = limit;
146-
this.distanceMeasure = distanceMeasure;
147-
this.distanceField = field;
148-
}
102+
@InternalApi
103+
public FindNearest(
104+
Expr property, double[] vector, DistanceMeasure distanceMeasure, FindNearestOptions options) {
105+
this.property = property;
106+
this.vector = vector;
107+
this.distanceMeasure = distanceMeasure;
108+
this.options = options;
109+
}
149110

150-
public static FindNearest.FindNearestOptions withLimitAndMeasure(
151-
long limit, FindNearest.DistanceMeasure distanceMeasure) {
152-
return new FindNearest.FindNearestOptions(limit, distanceMeasure);
153-
}
111+
@Override
112+
@InternalApi
113+
public String getName() {
114+
return name;
115+
}
154116

155-
public FindNearest.FindNearestOptions withDistanceField(String name) {
156-
return new FindNearest.FindNearestOptions(limit, distanceMeasure, Field.of(name));
157-
}
117+
@InternalApi
118+
public Expr getProperty() {
119+
return property;
120+
}
158121

159-
public Long getLimit() {
160-
return limit;
161-
}
122+
@InternalApi
123+
public double[] getVector() {
124+
return vector;
125+
}
162126

163-
public DistanceMeasure getDistanceMeasure() {
164-
return distanceMeasure;
165-
}
127+
@InternalApi
128+
public DistanceMeasure getDistanceMeasure() {
129+
return distanceMeasure;
130+
}
166131

167-
public Field getDistanceField() {
168-
return distanceField;
169-
}
132+
@InternalApi
133+
public FindNearestOptions getOptions() {
134+
return options;
170135
}
171136
}

0 commit comments

Comments
 (0)