1818
1919import com .google .common .base .MoreObjects ;
2020import com .google .common .base .MoreObjects .ToStringHelper ;
21- import com .google .protobuf .ByteString ;
2221import com .google .protobuf .DoubleValue ;
2322import com .google .protobuf .Int32Value ;
24-
2523import java .io .Serializable ;
2624import java .util .Objects ;
27-
2825import javax .annotation .Nullable ;
2926
30-
3127/**
32- * A query that finds the entities whose vector fields are closest to a certain query vector.
33- * Create an instance of `FindNearest` with {@link Query#findNearest}.
28+ * A query that finds the entities whose vector fields are closest to a certain query vector. Create
29+ * an instance of `FindNearest` with {@link Query#findNearest}.
3430 */
3531public final class FindNearest implements Serializable {
3632
@@ -48,7 +44,13 @@ public final class FindNearest implements Serializable {
4844 private static final long serialVersionUID = 4688656124180403551L ;
4945
5046 /** Creates a VectorQuery */
51- public FindNearest (String vectorProperty , VectorValue queryVector , DistanceMeasure measure , int limit , @ Nullable String distanceResultField ,@ Nullable Double distanceThreshold ) {
47+ public FindNearest (
48+ String vectorProperty ,
49+ VectorValue queryVector ,
50+ DistanceMeasure measure ,
51+ int limit ,
52+ @ Nullable String distanceResultField ,
53+ @ Nullable Double distanceThreshold ) {
5254 this .vectorProperty = vectorProperty ;
5355 this .queryVector = queryVector ;
5456 this .measure = measure ;
@@ -57,21 +59,33 @@ public FindNearest(String vectorProperty, VectorValue queryVector, DistanceMeasu
5759 this .distanceThreshold = distanceThreshold ;
5860 }
5961
60- public FindNearest (String vectorProperty , VectorValue queryVector , DistanceMeasure measure , int limit ) {
62+ public FindNearest (
63+ String vectorProperty , VectorValue queryVector , DistanceMeasure measure , int limit ) {
6164 this (vectorProperty , queryVector , measure , limit , null , null );
6265 }
6366
64- public FindNearest (String vectorProperty , VectorValue queryVector , DistanceMeasure measure , int limit , @ Nullable String distanceResultField ) {
67+ public FindNearest (
68+ String vectorProperty ,
69+ VectorValue queryVector ,
70+ DistanceMeasure measure ,
71+ int limit ,
72+ @ Nullable String distanceResultField ) {
6573 this (vectorProperty , queryVector , measure , limit , distanceResultField , null );
6674 }
6775
68- public FindNearest (String vectorProperty , VectorValue queryVector , DistanceMeasure measure , int limit , @ Nullable Double distanceThreshold ) {
76+ public FindNearest (
77+ String vectorProperty ,
78+ VectorValue queryVector ,
79+ DistanceMeasure measure ,
80+ int limit ,
81+ @ Nullable Double distanceThreshold ) {
6982 this (vectorProperty , queryVector , measure , limit , null , distanceThreshold );
7083 }
7184
7285 @ Override
7386 public int hashCode () {
74- return Objects .hash (vectorProperty , queryVector , measure , limit , distanceResultField , distanceThreshold );
87+ return Objects .hash (
88+ vectorProperty , queryVector , measure , limit , distanceResultField , distanceThreshold );
7589 }
7690
7791 /**
@@ -90,11 +104,11 @@ public boolean equals(Object obj) {
90104 }
91105 FindNearest otherQuery = (FindNearest ) obj ;
92106 return Objects .equals (vectorProperty , otherQuery .vectorProperty )
93- && Objects .equals (queryVector , otherQuery .queryVector )
94- && Objects .equals (distanceResultField , otherQuery .distanceResultField )
95- && Objects .equals (distanceThreshold , otherQuery .distanceThreshold )
96- && limit == otherQuery .limit
97- && measure == otherQuery .measure ;
107+ && Objects .equals (queryVector , otherQuery .queryVector )
108+ && Objects .equals (distanceResultField , otherQuery .distanceResultField )
109+ && Objects .equals (distanceThreshold , otherQuery .distanceThreshold )
110+ && limit == otherQuery .limit
111+ && measure == otherQuery .measure ;
98112 }
99113
100114 @ Override
@@ -111,22 +125,38 @@ public String toString() {
111125
112126 static FindNearest fromPb (com .google .datastore .v1 .FindNearest findNearestPb ) {
113127 String vectorProperty = findNearestPb .getVectorProperty ().getName ();
114- VectorValue queryVector = VectorValue .MARSHALLER .fromProto (findNearestPb .getQueryVector ()).build ();
115- DistanceMeasure distanceMeasure = DistanceMeasure .valueOf (findNearestPb .getDistanceMeasure ().toString ());
128+ VectorValue queryVector =
129+ VectorValue .MARSHALLER .fromProto (findNearestPb .getQueryVector ()).build ();
130+ DistanceMeasure distanceMeasure =
131+ DistanceMeasure .valueOf (findNearestPb .getDistanceMeasure ().toString ());
116132 int limit = findNearestPb .getLimit ().getValue ();
117- String distanceResultField = findNearestPb .getDistanceResultProperty () == null || findNearestPb .getDistanceResultProperty ().isEmpty () ? null :findNearestPb .getDistanceResultProperty ();
118- Double distanceThreshold = findNearestPb .getDistanceThreshold () == null || findNearestPb .getDistanceThreshold () == DoubleValue .getDefaultInstance () ? null : findNearestPb .getDistanceThreshold ().getValue ();
119- return new FindNearest (vectorProperty ,queryVector , distanceMeasure , limit , distanceResultField , distanceThreshold );
133+ String distanceResultField =
134+ findNearestPb .getDistanceResultProperty () == null
135+ || findNearestPb .getDistanceResultProperty ().isEmpty ()
136+ ? null
137+ : findNearestPb .getDistanceResultProperty ();
138+ Double distanceThreshold =
139+ findNearestPb .getDistanceThreshold () == null
140+ || findNearestPb .getDistanceThreshold () == DoubleValue .getDefaultInstance ()
141+ ? null
142+ : findNearestPb .getDistanceThreshold ().getValue ();
143+ return new FindNearest (
144+ vectorProperty ,
145+ queryVector ,
146+ distanceMeasure ,
147+ limit ,
148+ distanceResultField ,
149+ distanceThreshold );
120150 }
121151
122152 com .google .datastore .v1 .FindNearest toPb () {
123- com .google .datastore .v1 .FindNearest .Builder findNearestPb = com .google .datastore .v1 .FindNearest .newBuilder ();
153+ com .google .datastore .v1 .FindNearest .Builder findNearestPb =
154+ com .google .datastore .v1 .FindNearest .newBuilder ();
124155 findNearestPb .getVectorPropertyBuilder ().setName (vectorProperty );
125156 findNearestPb .setQueryVector (queryVector .toPb ());
126157 findNearestPb .setDistanceMeasure (toProto (measure ));
127158 findNearestPb .setLimit (Int32Value .of (limit ));
128- if (distanceResultField != null )
129- {
159+ if (distanceResultField != null ) {
130160 findNearestPb .setDistanceResultProperty (distanceResultField );
131161 }
132162 if (distanceThreshold != null ) {
@@ -136,7 +166,7 @@ com.google.datastore.v1.FindNearest toPb() {
136166 }
137167
138168 protected static com .google .datastore .v1 .FindNearest .DistanceMeasure toProto (
139- DistanceMeasure distanceMeasure ) {
169+ DistanceMeasure distanceMeasure ) {
140170 switch (distanceMeasure ) {
141171 case COSINE :
142172 return com .google .datastore .v1 .FindNearest .DistanceMeasure .COSINE ;
@@ -152,7 +182,7 @@ protected static com.google.datastore.v1.FindNearest.DistanceMeasure toProto(
152182 /**
153183 * The distance measure to use when comparing vectors in a {@link FindNearest query}.
154184 *
155- * @see com.google.cloud.firestore .Query#findNearest
185+ * @see com.google.cloud.datastore .Query#findNearest
156186 */
157187 public enum DistanceMeasure {
158188 /**
0 commit comments