Skip to content

Commit 6165685

Browse files
committed
Fix javadoc
1 parent 2b11399 commit 6165685

File tree

3 files changed

+56
-34
lines changed

3 files changed

+56
-34
lines changed
Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,29 +24,10 @@
2424
import static org.junit.Assert.assertNull;
2525

2626
import com.google.cloud.Timestamp;
27-
import com.google.cloud.datastore.Cursor;
28-
import com.google.cloud.datastore.Datastore;
29-
import com.google.cloud.datastore.DatastoreException;
30-
import com.google.cloud.datastore.DatastoreOptions;
31-
import com.google.cloud.datastore.Entity;
32-
import com.google.cloud.datastore.EntityQuery;
33-
import com.google.cloud.datastore.FullEntity;
34-
import com.google.cloud.datastore.IncompleteKey;
35-
import com.google.cloud.datastore.Key;
36-
import com.google.cloud.datastore.KeyFactory;
37-
import com.google.cloud.datastore.KeyQuery;
38-
import com.google.cloud.datastore.ListValue;
39-
import com.google.cloud.datastore.PathElement;
40-
import com.google.cloud.datastore.ProjectionEntity;
41-
import com.google.cloud.datastore.Query;
42-
import com.google.cloud.datastore.QueryResults;
43-
import com.google.cloud.datastore.ReadOption;
44-
import com.google.cloud.datastore.StringValue;
45-
import com.google.cloud.datastore.StructuredQuery;
27+
import com.google.cloud.datastore.*;
4628
import com.google.cloud.datastore.StructuredQuery.CompositeFilter;
4729
import com.google.cloud.datastore.StructuredQuery.OrderBy;
4830
import com.google.cloud.datastore.StructuredQuery.PropertyFilter;
49-
import com.google.cloud.datastore.Transaction;
5031
import com.google.cloud.datastore.testing.LocalDatastoreHelper;
5132
import com.google.common.collect.ImmutableList;
5233
import com.google.common.collect.ImmutableMap;
@@ -399,6 +380,9 @@ private void setUpQueryTests() {
399380
"description",
400381
StringValue.newBuilder("Learn Cloud Datastore").setExcludeFromIndexes(true).build())
401382
.set("tag", "fun", "l", "programming", "learn")
383+
.set(
384+
"vector_property",
385+
VectorValue.newBuilder(3.0, 1.0, 2.0).setExcludeFromIndexes(true).build())
402386
.build());
403387
}
404388

@@ -717,6 +701,38 @@ public void testInequalitySortInvalidNotFirst() {
717701
assertInvalidQuery(query);
718702
}
719703

704+
@Test
705+
public void testVectorSearch() {
706+
setUpQueryTests();
707+
VectorValue vectorValue = VectorValue.newBuilder(1.78, 2.56, 3.88).build();
708+
FindNearest vectorQuery =
709+
new FindNearest(
710+
"vector_property", vectorValue, FindNearest.DistanceMeasure.COSINE, 1, "distance");
711+
712+
Query<Entity> query = Query.newEntityQueryBuilder().setFindNearest(vectorQuery).build();
713+
assertValidQuery(query);
714+
}
715+
716+
@Test
717+
public void testVectorSearchWithEmptyVector() {
718+
setUpQueryTests();
719+
VectorValue emptyVector = VectorValue.newBuilder().build();
720+
FindNearest vectorQuery =
721+
new FindNearest("vector_property", emptyVector, FindNearest.DistanceMeasure.EUCLIDEAN, 1);
722+
Query<Entity> query = Query.newEntityQueryBuilder().setFindNearest(vectorQuery).build();
723+
assertInvalidQuery(query);
724+
}
725+
726+
@Test
727+
public void testVectorSearchWithUnmatchedVectorSize() {
728+
setUpQueryTests();
729+
VectorValue vectorValue = VectorValue.newBuilder(1.78, 2.56, 3.88, 4.33).build();
730+
FindNearest vectorQuery =
731+
new FindNearest("vector_property", vectorValue, FindNearest.DistanceMeasure.DOT_PRODUCT, 1);
732+
Query<Entity> query = Query.newEntityQueryBuilder().setFindNearest(vectorQuery).build();
733+
assertInvalidQuery(query);
734+
}
735+
720736
@Test
721737
public void testLimit() {
722738
setUpQueryTests();

google-cloud-datastore/src/main/java/com/google/cloud/datastore/FindNearest.java

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,24 +26,34 @@
2626

2727
/**
2828
* 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}.
29+
* an instance of `FindNearest` with {@link Query}.
3030
*/
3131
public final class FindNearest implements Serializable {
3232

33+
/** An indexed vector property to search upon. */
3334
private final String vectorProperty;
35+
/** The query vector that we are searching on. */
3436
private final VectorValue queryVector;
37+
/** The Distance Measure to use, required. */
3538
private final DistanceMeasure measure;
39+
/** The number of nearest neighbors to return. Must be a positive integer of no more than 100. */
3640
private final int limit;
37-
/*
38-
Optional. Optional name of the field to output the result of the vector
39-
* distance calculation.
41+
42+
/**
43+
* Optional. Optional name of the field to output the result of the vector distance calculation.
4044
*/
4145
private final @Nullable String distanceResultField;
46+
47+
/**
48+
* Optional. Option to specify a threshold for which no less similar documents will be returned.
49+
* The behavior of the specified `distance_measure` will affect the meaning of the distance
50+
* threshold.
51+
*/
4252
private final @Nullable Double distanceThreshold;
4353

4454
private static final long serialVersionUID = 4688656124180403551L;
4555

46-
/** Creates a VectorQuery */
56+
/** Creates a FindNearest query. */
4757
public FindNearest(
4858
String vectorProperty,
4959
VectorValue queryVector,
@@ -89,10 +99,10 @@ public int hashCode() {
8999
}
90100

91101
/**
92-
* Returns true if this VectorQuery is equal to the provided object.
102+
* Returns true if this FindNearest query is equal to the provided object.
93103
*
94104
* @param obj The object to compare against.
95-
* @return Whether this VectorQuery is equal to the provided object.
105+
* @return Whether this FindNearest query is equal to the provided object.
96106
*/
97107
@Override
98108
public boolean equals(Object obj) {
@@ -179,19 +189,15 @@ protected static com.google.datastore.v1.FindNearest.DistanceMeasure toProto(
179189
}
180190
}
181191

182-
/**
183-
* The distance measure to use when comparing vectors in a {@link FindNearest query}.
184-
*
185-
* @see com.google.cloud.datastore.Query#findNearest
186-
*/
192+
/** The distance measure to use when comparing vectors in a {@link FindNearest query}. */
187193
public enum DistanceMeasure {
194+
DISTANCE_MEASURE_UNSPECIFIED,
188195
/**
189196
* COSINE distance compares vectors based on the angle between them, which allows you to measure
190197
* similarity that isn't based on the vectors' magnitude. We recommend using DOT_PRODUCT with
191198
* unit normalized vectors instead of COSINE distance, which is mathematically equivalent with
192199
* better performance.
193200
*/
194-
DISTANCE_MEASURE_UNSPECIFIED,
195201
COSINE,
196202
/** Measures the EUCLIDEAN distance between the vectors. */
197203
EUCLIDEAN,

google-cloud-datastore/src/main/java/com/google/cloud/datastore/ValueType.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public enum ValueType {
6363
/** Represents a {@link LatLng} value. */
6464
LAT_LNG(LatLngValue.MARSHALLER),
6565

66-
/** Represents a {@link Vector} value. */
66+
/** Represents a {@link VectorValue} value. */
6767
VECTOR(VectorValue.MARSHALLER);
6868

6969
private static final ImmutableMap<Integer, ValueType> DESCRIPTOR_TO_TYPE_MAP;

0 commit comments

Comments
 (0)