Skip to content

Commit 62da35d

Browse files
committed
Modify comment and fix formatting
1 parent c8340bf commit 62da35d

File tree

4 files changed

+35
-39
lines changed

4 files changed

+35
-39
lines changed

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@
2020
import java.util.ArrayList;
2121
import java.util.List;
2222

23-
/** A Google Cloud Datastore Vector value. A list value is a list of {@link Value} objects. */
23+
/**
24+
* A Google Cloud Datastore Vector value. A Vector value is a list of Double {@link Value} objects.
25+
*/
2426
public final class VectorValue extends Value<List<Value<Double>>> {
2527

2628
private static final long serialVersionUID = -5121887228607148859L;
@@ -87,7 +89,7 @@ public VectorValue.Builder addValue(double first, double... other) {
8789
}
8890

8991
/**
90-
* Sets the list of values of this {@code ListValue} builder to {@code values}. The provided
92+
* Sets the list of values of this {@code VectorValue} builder to {@code values}. The provided
9193
* list is copied.
9294
*
9395
* @see com.google.cloud.datastore.Value.BaseBuilder#set(java.lang.Object)
@@ -106,7 +108,7 @@ public List<Value<Double>> get() {
106108
return vectorBuilder.build();
107109
}
108110

109-
/** Creates a {@code ListValue} object. */
111+
/** Creates a {@code VectorValue} object. */
110112
@Override
111113
public VectorValue build() {
112114
return new VectorValue(this);
@@ -121,7 +123,7 @@ private VectorValue(Builder builder) {
121123
super(builder);
122124
}
123125

124-
/** Returns a builder for the list value object. */
126+
/** Returns a builder for the vector value object. */
125127
@Override
126128
public Builder toBuilder() {
127129
return new Builder().mergeFrom(this);
@@ -137,10 +139,9 @@ public static VectorValue of(List<Value<Double>> values) {
137139
return new VectorValue(values);
138140
}
139141

140-
/** Returns a builder for {@code ListValue} objects. */
142+
/** Returns a builder for {@code VectorValue} objects. */
141143
public static Builder newBuilder() {
142144
Builder builder = new VectorValue.Builder();
143-
builder.setExcludeFromIndexes(true);
144145
builder.setMeaning(VECTOR_MEANING);
145146
return builder;
146147
}

google-cloud-datastore/src/test/java/com/google/cloud/datastore/StructuredQueryTest.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,6 @@ public void testKeyQueryBuilder() {
111111
assertEquals(ORDER_BY, KEY_QUERY.getOrderBy());
112112
assertEquals(ImmutableList.of(StructuredQuery.KEY_PROPERTY_NAME), KEY_QUERY.getProjection());
113113
assertTrue(KEY_QUERY.getDistinctOn().isEmpty());
114-
assertEquals(LIMIT, KEY_QUERY.getLimit());
115114
}
116115

117116
@Test
@@ -157,9 +156,6 @@ private void compareMergedQuery(StructuredQuery<?> expected, StructuredQuery<?>
157156

158157
@Test
159158
public void testToAndFromPb() {
160-
EntityQuery a = ENTITY_QUERY;
161-
StructuredQuery<EntityQuery> pb =
162-
StructuredQuery.fromPb(ResultType.ENTITY, ENTITY_QUERY.getNamespace(), ENTITY_QUERY.toPb());
163159
assertEquals(
164160
ENTITY_QUERY,
165161
StructuredQuery.fromPb(

google-cloud-datastore/src/test/java/com/google/cloud/datastore/VectorValueTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ public class VectorValueTest {
2929

3030
@Test
3131
public void testToBuilder() {
32-
// StringValue value = StringValue.of(CONTENT);
3332
VectorValue value = VectorValue.of(0.3, 4.2, 3.7);
3433
assertEquals(value, value.toBuilder().build());
3534
}

google-cloud-datastore/src/test/java/com/google/cloud/datastore/it/ITDatastoreTest.java

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2124,54 +2124,54 @@ public void testQueryWithVectorSearch() {
21242124
Key.newBuilder(PROJECT_ID, KIND1, "name-01", options.getDatabaseId()).build())
21252125
.set(
21262126
"vector_property",
2127-
VectorValue.newBuilder(3.0, 1.0, 2.0).setExcludeFromIndexes(true).build())
2127+
VectorValue.newBuilder(3.0, 9.0, 11.1).setExcludeFromIndexes(true).build())
21282128
.build();
2129-
datastore.put(entity1);
2130-
2131-
VectorValue vectorValue = VectorValue.newBuilder(1.78, 2.56, 3.88).build();
2132-
2133-
// Query to find the nearest 1 neighbor
2134-
FindNearest vectorQuery =
2135-
new FindNearest(
2136-
"vector_property", vectorValue, FindNearest.DistanceMeasure.COSINE, 1, "distance");
2137-
Query<Entity> queryWithVectorSearch =
2138-
Query.newEntityQueryBuilder().setKind(KIND1).setFindNearest(vectorQuery).build();
2139-
QueryResults<Entity> vectorSearchResult = datastore.run(queryWithVectorSearch);
2140-
assertTrue(vectorSearchResult.hasNext());
2141-
assertEquals(entity1, vectorSearchResult.next());
2142-
assertFalse(vectorSearchResult.hasNext());
2143-
21442129
Entity entity2 =
21452130
Entity.newBuilder(
21462131
Key.newBuilder(PROJECT_ID, KIND1, "name-02", options.getDatabaseId()).build())
21472132
.set(
21482133
"vector_property",
2149-
VectorValue.newBuilder(5.0, 0.7, 2.0).setExcludeFromIndexes(true).build())
2134+
VectorValue.newBuilder(2.8, 2.56, 3.8).setExcludeFromIndexes(true).build())
21502135
.build();
21512136
Entity entity3 =
21522137
Entity.newBuilder(
21532138
Key.newBuilder(PROJECT_ID, KIND1, "name-03", options.getDatabaseId()).build())
21542139
.set(
21552140
"vector_property",
2156-
VectorValue.newBuilder(2.0, 1.7, 1.0).setExcludeFromIndexes(true).build())
2141+
VectorValue.newBuilder(2.8, 2.56, 3.88).setExcludeFromIndexes(true).build())
21572142
.build();
2158-
datastore.put(entity2, entity3);
2143+
datastore.put(entity1, entity2, entity3);
21592144

2160-
// Query to find the nearest 2 neighbors
2161-
FindNearest vectorQueryWithLimit =
2145+
// Query to find the nearest 2 neighbors with COSINE distance
2146+
FindNearest findNearestQuery =
21622147
new FindNearest(
21632148
"vector_property",
2164-
VectorValue.newBuilder(2.8, 2.56, 3.88).build(),
2165-
FindNearest.DistanceMeasure.EUCLIDEAN,
2149+
VectorValue.newBuilder(1.78, 2.56, 3.88).build(),
2150+
FindNearest.DistanceMeasure.COSINE,
21662151
2,
21672152
"distance");
2168-
2169-
Query<Entity> queryWithVectorSearchLimit =
2170-
Query.newEntityQueryBuilder().setKind(KIND1).setFindNearest(vectorQueryWithLimit).build();
2171-
QueryResults<Entity> resultsWithVectorLimit = datastore.run(queryWithVectorSearchLimit);
2172-
List<Entity> resultsCopy = makeResultsCopy(resultsWithVectorLimit);
2153+
Query<Entity> queryWithVectorSearch =
2154+
Query.newEntityQueryBuilder().setKind(KIND1).setFindNearest(findNearestQuery).build();
2155+
QueryResults<Entity> vectorSearchResult = datastore.run(queryWithVectorSearch);
2156+
List<Entity> resultsCopy = makeResultsCopy(vectorSearchResult);
2157+
// Should return nearest 2 neighbors
21732158
assertEquals(2, resultsCopy.size());
21742159

2160+
// Query to find the nearest neighbor with EUCLIDEAN distance
2161+
FindNearest findNearestWithLimit1 =
2162+
new FindNearest(
2163+
"vector_property",
2164+
VectorValue.newBuilder(2.8, 2.56, 3.88).build(),
2165+
FindNearest.DistanceMeasure.EUCLIDEAN,
2166+
1,
2167+
"distance");
2168+
Query<Entity> vectorQueryWithLimit1 =
2169+
Query.newEntityQueryBuilder().setKind(KIND1).setFindNearest(findNearestWithLimit1).build();
2170+
QueryResults<Entity> resultsWithVectorLimit1 = datastore.run(vectorQueryWithLimit1);
2171+
assertTrue(resultsWithVectorLimit1.hasNext());
2172+
// entity3 should be the nearest neighbor
2173+
assertEquals(entity3, resultsWithVectorLimit1.next());
2174+
assertFalse(resultsWithVectorLimit1.hasNext());
21752175
datastore.delete(entity1.getKey(), entity2.getKey(), entity3.getKey());
21762176
}
21772177

0 commit comments

Comments
 (0)