Skip to content

Commit c8340bf

Browse files
committed
Add comment and fix formatting
1 parent a371467 commit c8340bf

File tree

2 files changed

+34
-27
lines changed

2 files changed

+34
-27
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,7 @@ public static Value<?> fromPb(com.google.datastore.v1.Value proto) {
218218
if (valueType == null) return RawValue.MARSHALLER.fromProto(proto).build();
219219

220220
Value<?> returnValue = valueType.getMarshaller().fromProto(proto).build();
221+
// If the proto is a list of doubles with a meaning of 31, use the VectorValue marshaller.
221222
if (valueType == ValueType.LIST && proto.getMeaning() == VECTOR_MEANING) {
222223
for (com.google.datastore.v1.Value item : proto.getArrayValue().getValuesList()) {
223224
if (item.getValueTypeCase() != ValueTypeCase.DOUBLE_VALUE) {

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

Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
import com.google.cloud.datastore.Entity;
4747
import com.google.cloud.datastore.EntityQuery;
4848
import com.google.cloud.datastore.EntityValue;
49+
import com.google.cloud.datastore.FindNearest;
4950
import com.google.cloud.datastore.FullEntity;
5051
import com.google.cloud.datastore.GqlQuery;
5152
import com.google.cloud.datastore.IncompleteKey;
@@ -56,8 +57,6 @@
5657
import com.google.cloud.datastore.LatLngValue;
5758
import com.google.cloud.datastore.ListValue;
5859
import com.google.cloud.datastore.NullValue;
59-
import com.google.cloud.datastore.VectorValue;
60-
import com.google.cloud.datastore.FindNearest;
6160
import com.google.cloud.datastore.PathElement;
6261
import com.google.cloud.datastore.ProjectionEntity;
6362
import com.google.cloud.datastore.Query;
@@ -72,6 +71,7 @@
7271
import com.google.cloud.datastore.TimestampValue;
7372
import com.google.cloud.datastore.Transaction;
7473
import com.google.cloud.datastore.ValueType;
74+
import com.google.cloud.datastore.VectorValue;
7575
import com.google.cloud.datastore.models.ExecutionStats;
7676
import com.google.cloud.datastore.models.ExplainMetrics;
7777
import com.google.cloud.datastore.models.ExplainOptions;
@@ -2120,48 +2120,54 @@ public void testQueryWithStartCursor() {
21202120
@Test
21212121
public void testQueryWithVectorSearch() {
21222122
Entity entity1 =
2123-
Entity.newBuilder(
2124-
Key.newBuilder(PROJECT_ID, KIND1, "name-01", options.getDatabaseId()).build())
2125-
.set(
2126-
"vector_property",
2127-
VectorValue.newBuilder(3.0, 1.0, 2.0).setExcludeFromIndexes(true).build())
2128-
.build();
2123+
Entity.newBuilder(
2124+
Key.newBuilder(PROJECT_ID, KIND1, "name-01", options.getDatabaseId()).build())
2125+
.set(
2126+
"vector_property",
2127+
VectorValue.newBuilder(3.0, 1.0, 2.0).setExcludeFromIndexes(true).build())
2128+
.build();
21292129
datastore.put(entity1);
21302130

21312131
VectorValue vectorValue = VectorValue.newBuilder(1.78, 2.56, 3.88).build();
21322132

21332133
// Query to find the nearest 1 neighbor
21342134
FindNearest vectorQuery =
2135-
new FindNearest(
2136-
"vector_property", vectorValue, FindNearest.DistanceMeasure.COSINE, 1, "distance");
2137-
Query<Entity> queryWithVectorSearch = Query.newEntityQueryBuilder().setKind(KIND1).setFindNearest(vectorQuery).build();
2135+
new FindNearest(
2136+
"vector_property", vectorValue, FindNearest.DistanceMeasure.COSINE, 1, "distance");
2137+
Query<Entity> queryWithVectorSearch =
2138+
Query.newEntityQueryBuilder().setKind(KIND1).setFindNearest(vectorQuery).build();
21382139
QueryResults<Entity> vectorSearchResult = datastore.run(queryWithVectorSearch);
21392140
assertTrue(vectorSearchResult.hasNext());
21402141
assertEquals(entity1, vectorSearchResult.next());
21412142
assertFalse(vectorSearchResult.hasNext());
21422143

21432144
Entity entity2 =
2144-
Entity.newBuilder(
2145-
Key.newBuilder(PROJECT_ID, KIND1, "name-02", options.getDatabaseId()).build())
2146-
.set(
2147-
"vector_property",
2148-
VectorValue.newBuilder(5.0, 0.7, 2.0).setExcludeFromIndexes(true).build())
2149-
.build();
2145+
Entity.newBuilder(
2146+
Key.newBuilder(PROJECT_ID, KIND1, "name-02", options.getDatabaseId()).build())
2147+
.set(
2148+
"vector_property",
2149+
VectorValue.newBuilder(5.0, 0.7, 2.0).setExcludeFromIndexes(true).build())
2150+
.build();
21502151
Entity entity3 =
2151-
Entity.newBuilder(
2152-
Key.newBuilder(PROJECT_ID, KIND1, "name-03", options.getDatabaseId()).build())
2153-
.set(
2154-
"vector_property",
2155-
VectorValue.newBuilder(2.0, 1.7, 1.0).setExcludeFromIndexes(true).build())
2156-
.build();
2152+
Entity.newBuilder(
2153+
Key.newBuilder(PROJECT_ID, KIND1, "name-03", options.getDatabaseId()).build())
2154+
.set(
2155+
"vector_property",
2156+
VectorValue.newBuilder(2.0, 1.7, 1.0).setExcludeFromIndexes(true).build())
2157+
.build();
21572158
datastore.put(entity2, entity3);
21582159

21592160
// Query to find the nearest 2 neighbors
21602161
FindNearest vectorQueryWithLimit =
2161-
new FindNearest(
2162-
"vector_property", VectorValue.newBuilder(2.8, 2.56, 3.88).build(), FindNearest.DistanceMeasure.EUCLIDEAN, 2, "distance");
2163-
2164-
Query<Entity> queryWithVectorSearchLimit = Query.newEntityQueryBuilder().setKind(KIND1).setFindNearest(vectorQueryWithLimit).build();
2162+
new FindNearest(
2163+
"vector_property",
2164+
VectorValue.newBuilder(2.8, 2.56, 3.88).build(),
2165+
FindNearest.DistanceMeasure.EUCLIDEAN,
2166+
2,
2167+
"distance");
2168+
2169+
Query<Entity> queryWithVectorSearchLimit =
2170+
Query.newEntityQueryBuilder().setKind(KIND1).setFindNearest(vectorQueryWithLimit).build();
21652171
QueryResults<Entity> resultsWithVectorLimit = datastore.run(queryWithVectorSearchLimit);
21662172
List<Entity> resultsCopy = makeResultsCopy(resultsWithVectorLimit);
21672173
assertEquals(2, resultsCopy.size());

0 commit comments

Comments
 (0)