Skip to content

Commit 9d79efc

Browse files
committed
Merge branch 'cindy/vector-search-1' of https://github.com/googleapis/java-datastore into cindy/vector-search-1
2 parents fca3689 + 1806f0a commit 9d79efc

File tree

4 files changed

+73
-64
lines changed

4 files changed

+73
-64
lines changed

com/google/datastore/snippets/ConceptsTest.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -409,9 +409,7 @@ private void setUpQueryTests() {
409409
"description",
410410
StringValue.newBuilder("Learn Cloud Datastore").setExcludeFromIndexes(true).build())
411411
.set("tag", "fun", "l", "programming", "learn")
412-
.set(
413-
"vector_property",
414-
VectorValue.newBuilder(3.0, 1.0, 2.0).build())
412+
.set("vector_property", VectorValue.newBuilder(3.0, 1.0, 2.0).build())
415413
.build());
416414
}
417415

@@ -1204,8 +1202,8 @@ public void testVectorSearch() {
12041202
// [START datastore_vector_search]
12051203
VectorValue vectorValue = VectorValue.newBuilder(1.78, 2.56, 3.88).build();
12061204
FindNearest vectorQuery =
1207-
new FindNearest(
1208-
"vector_property", vectorValue, FindNearest.DistanceMeasure.COSINE, 1, "distance");
1205+
new FindNearest(
1206+
"vector_property", vectorValue, FindNearest.DistanceMeasure.COSINE, 1, "distance");
12091207

12101208
Query<Entity> query = Query.newEntityQueryBuilder().setFindNearest(vectorQuery).build();
12111209
// [END datastore_vector_search]

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,7 @@ public class BaseEntityTest {
3737
private static final Timestamp TIMESTAMP = Timestamp.now();
3838
private static final LatLng LAT_LNG = new LatLng(37.422035, -122.084124);
3939
private static final VectorValue VECTOR =
40-
VectorValue.newBuilder(1.78, 2.56, 3.88)
41-
.setMeaning(VECTOR_MEANING)
42-
.build();
40+
VectorValue.newBuilder(1.78, 2.56, 3.88).setMeaning(VECTOR_MEANING).build();
4341
private static final Key KEY = Key.newBuilder("ds1", "k1", "n1").build();
4442
private static final Entity ENTITY = Entity.newBuilder(KEY).set("name", "foo").build();
4543
private static final IncompleteKey INCOMPLETE_KEY = IncompleteKey.newBuilder("ds1", "k1").build();

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -156,9 +156,7 @@ private void setUpQueryTests() {
156156
"description",
157157
StringValue.newBuilder("Learn Cloud Datastore").setExcludeFromIndexes(true).build())
158158
.set("tag", "fun", "l", "programming", "learn")
159-
.set(
160-
"embedding_field",
161-
VectorValue.newBuilder(3.0, 1.0, 2.0).build())
159+
.set("embedding_field", VectorValue.newBuilder(3.0, 1.0, 2.0).build())
162160
.build());
163161
}
164162

@@ -582,8 +580,8 @@ public void testVectorSearch() {
582580
new FindNearest(
583581
"embedding_field", vectorValue, FindNearest.DistanceMeasure.COSINE, 1, "distance");
584582

585-
Query<Entity> query = Query.newEntityQueryBuilder()
586-
.setKind(TASK_CONCEPTS).setFindNearest(vectorQuery).build();
583+
Query<Entity> query =
584+
Query.newEntityQueryBuilder().setKind(TASK_CONCEPTS).setFindNearest(vectorQuery).build();
587585
assertValidQuery(query);
588586
}
589587

@@ -592,7 +590,8 @@ public void testVectorSearchWithEmptyVector() {
592590
VectorValue emptyVector = VectorValue.newBuilder().build();
593591
FindNearest vectorQuery =
594592
new FindNearest("embedding_field", emptyVector, FindNearest.DistanceMeasure.EUCLIDEAN, 1);
595-
Query<Entity> query = Query.newEntityQueryBuilder().setKind(TASK_CONCEPTS).setFindNearest(vectorQuery).build();
593+
Query<Entity> query =
594+
Query.newEntityQueryBuilder().setKind(TASK_CONCEPTS).setFindNearest(vectorQuery).build();
596595
assertInvalidQuery(query);
597596
}
598597

@@ -601,7 +600,8 @@ public void testVectorSearchWithUnmatchedVectorSize() {
601600
VectorValue vectorValue = VectorValue.newBuilder(1.78, 2.56, 3.88, 4.33).build();
602601
FindNearest vectorQuery =
603602
new FindNearest("embedding_field", vectorValue, FindNearest.DistanceMeasure.DOT_PRODUCT, 1);
604-
Query<Entity> query = Query.newEntityQueryBuilder().setKind(TASK_CONCEPTS).setFindNearest(vectorQuery).build();
603+
Query<Entity> query =
604+
Query.newEntityQueryBuilder().setKind(TASK_CONCEPTS).setFindNearest(vectorQuery).build();
605605
assertInvalidQuery(query);
606606
}
607607

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

Lines changed: 62 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,13 @@ public ITDatastoreTest(
149149

150150
PROJECT_ID = this.options.getProjectId();
151151
NAMESPACE = this.options.getNamespace();
152-
System.out.println("Project: " + PROJECT_ID + ", Namespace: " + NAMESPACE + ", db: " + options.getDatabaseId());
152+
System.out.println(
153+
"Project: "
154+
+ PROJECT_ID
155+
+ ", Namespace: "
156+
+ NAMESPACE
157+
+ ", db: "
158+
+ options.getDatabaseId());
153159

154160
ROOT_KEY =
155161
Key.newBuilder(PROJECT_ID, "rootkey", "default", options.getDatabaseId())
@@ -172,7 +178,8 @@ public ITDatastoreTest(
172178
Key.newBuilder(options.getProjectId(), KIND2, 100, options.getDatabaseId())
173179
.setNamespace(NAMESPACE)
174180
.build();
175-
VECTORKEY = Key.newBuilder(PROJECT_ID, VECTOR_KIND, "bean1", options.getDatabaseId())
181+
VECTORKEY =
182+
Key.newBuilder(PROJECT_ID, VECTOR_KIND, "bean1", options.getDatabaseId())
176183
.setNamespace(NAMESPACE)
177184
.build();
178185

@@ -225,28 +232,21 @@ public ITDatastoreTest(
225232
.set("partial2", ENTITY2)
226233
.build();
227234
VECTOR_ENTITY_1 =
228-
Entity.newBuilder(VECTORKEY)
229-
.set("name", "Arabica")
230-
.set(
231-
"embedding_field",
232-
VectorValue.newBuilder(1.0, 7.0, 11.1).build())
233-
.build();
235+
Entity.newBuilder(VECTORKEY)
236+
.set("name", "Arabica")
237+
.set("embedding_field", VectorValue.newBuilder(1.0, 7.0, 11.1).build())
238+
.build();
234239
VECTOR_ENTITY_2 =
235-
Entity.newBuilder(
236-
Key.newBuilder(VECTORKEY).setName("bean2").build())
237-
.set("name", "Robusta")
238-
.set(
239-
"embedding_field",
240-
VectorValue.newBuilder(1.0, 9.0, 11.1).build()).set("vector_distance", 0)
241-
.build();
240+
Entity.newBuilder(Key.newBuilder(VECTORKEY).setName("bean2").build())
241+
.set("name", "Robusta")
242+
.set("embedding_field", VectorValue.newBuilder(1.0, 9.0, 11.1).build())
243+
.set("vector_distance", 0)
244+
.build();
242245
VECTOR_ENTITY_3 =
243-
Entity.newBuilder(
244-
Key.newBuilder(VECTORKEY).setName("bean3").build())
245-
.set("name", "Excelsa")
246-
.set(
247-
"embedding_field",
248-
VectorValue.newBuilder(4.0, 9.0, 11.1).build())
249-
.build();
246+
Entity.newBuilder(Key.newBuilder(VECTORKEY).setName("bean3").build())
247+
.set("name", "Excelsa")
248+
.set("embedding_field", VectorValue.newBuilder(4.0, 9.0, 11.1).build())
249+
.build();
250250

251251
Key aggregationKey1 = datastore.newKeyFactory().setKind(MARKS_KIND).newKey(1);
252252
Key aggregationKey2 = datastore.newKeyFactory().setKind(MARKS_KIND).newKey(2);
@@ -289,7 +289,7 @@ public void tearDown() {
289289
@Parameterized.Parameters(name = "database: {2}")
290290
public static Iterable<Object[]> data() {
291291
return Arrays.asList(
292-
new Object[][] {{OPTIONS_1, DATASTORE_1, "default"}, {OPTIONS_2, DATASTORE_2, "test-db"}});
292+
new Object[][] {{OPTIONS_1, DATASTORE_1, "default"}, {OPTIONS_2, DATASTORE_2, "test-db"}});
293293
}
294294

295295
private <T> Iterator<T> getStronglyConsistentResults(Query scQuery, Query query)
@@ -2120,13 +2120,16 @@ public void testVectorSearchQueryWithLimit() {
21202120
datastore.put(VECTOR_ENTITY_1, VECTOR_ENTITY_2, VECTOR_ENTITY_3);
21212121
// Test FindNearest query with limit
21222122
FindNearest findNearestQueryWithLimit =
2123-
new FindNearest(
2124-
"embedding_field",
2125-
VectorValue.newBuilder(1, 9, 11.1).build(),
2126-
FindNearest.DistanceMeasure.DOT_PRODUCT,
2127-
3);
2123+
new FindNearest(
2124+
"embedding_field",
2125+
VectorValue.newBuilder(1, 9, 11.1).build(),
2126+
FindNearest.DistanceMeasure.DOT_PRODUCT,
2127+
3);
21282128
Query<Entity> queryWithLimit =
2129-
Query.newEntityQueryBuilder().setKind(VECTOR_KIND).setFindNearest(findNearestQueryWithLimit).build();
2129+
Query.newEntityQueryBuilder()
2130+
.setKind(VECTOR_KIND)
2131+
.setFindNearest(findNearestQueryWithLimit)
2132+
.build();
21302133

21312134
QueryResults<Entity> resultWithLimit = datastore.run(queryWithLimit);
21322135

@@ -2142,26 +2145,31 @@ public void testVectorSearchQueryWithDistanceThreshold() {
21422145

21432146
VectorValue vectorValue = VectorValue.newBuilder(1.78, 2.56, 3.88).build();
21442147
FindNearest vectorQuery =
2145-
new FindNearest(
2146-
"embedding_field", vectorValue, FindNearest.DistanceMeasure.COSINE, 1, "distance");
2148+
new FindNearest(
2149+
"embedding_field", vectorValue, FindNearest.DistanceMeasure.COSINE, 1, "distance");
21472150

21482151
Query<Entity> query = Query.newEntityQueryBuilder().setFindNearest(vectorQuery).build();
21492152

2150-
21512153
// Test FindNearest query with distanceThreshold
21522154
FindNearest findNearestQueryWithThreshold =
2153-
new FindNearest(
2154-
"embedding_field",
2155-
VectorValue.newBuilder(1, 9, 11.1).build(),
2156-
FindNearest.DistanceMeasure.EUCLIDEAN,
2157-
3, "vector_distance", 2.0);
2155+
new FindNearest(
2156+
"embedding_field",
2157+
VectorValue.newBuilder(1, 9, 11.1).build(),
2158+
FindNearest.DistanceMeasure.EUCLIDEAN,
2159+
3,
2160+
"vector_distance",
2161+
2.0);
21582162
Query<Entity> queryWithWithThreshold =
2159-
Query.newEntityQueryBuilder().setKind(VECTOR_KIND).setFindNearest(findNearestQueryWithThreshold).build();
2163+
Query.newEntityQueryBuilder()
2164+
.setKind(VECTOR_KIND)
2165+
.setFindNearest(findNearestQueryWithThreshold)
2166+
.build();
21602167
QueryResults<Entity> resultWithThreshold = datastore.run(queryWithWithThreshold);
21612168
List<Entity> resultsCopyWithThreshold = makeResultsCopy(resultWithThreshold);
21622169
// Verify threshold was applied regardless of limit
21632170
assertEquals(2, resultsCopyWithThreshold.size());
2164-
// Verify qualified EUCLIDEAN distance: d((1, 9, 11.1), (1, 9, 11.1)) = 0.0, d((1, 9, 11.1), (1, 7, 11.1)) = 2.0
2171+
// Verify qualified EUCLIDEAN distance: d((1, 9, 11.1), (1, 9, 11.1)) = 0.0, d((1, 9, 11.1), (1,
2172+
// 7, 11.1)) = 2.0
21652173
assertEquals(DoubleValue.of(0.0), resultsCopyWithThreshold.get(0).getValue("vector_distance"));
21662174
assertEquals(DoubleValue.of(2.0), resultsCopyWithThreshold.get(1).getValue("vector_distance"));
21672175
}
@@ -2171,21 +2179,26 @@ public void testQueryWithVectorSearchWithDistanceField() {
21712179
datastore.put(VECTOR_ENTITY_1, VECTOR_ENTITY_2, VECTOR_ENTITY_3);
21722180
// Test FindNearest query with distanceField
21732181
FindNearest findNearestQueryWithDistanceField =
2174-
new FindNearest(
2175-
"embedding_field",
2176-
VectorValue.newBuilder(1, 9, 11.1).build(),
2177-
FindNearest.DistanceMeasure.DOT_PRODUCT,
2178-
3, "vector_distance", 0.0);
2182+
new FindNearest(
2183+
"embedding_field",
2184+
VectorValue.newBuilder(1, 9, 11.1).build(),
2185+
FindNearest.DistanceMeasure.DOT_PRODUCT,
2186+
3,
2187+
"vector_distance",
2188+
0.0);
21792189
Query<Entity> queryWithWithDistanceField =
2180-
Query.newEntityQueryBuilder().setKind(VECTOR_KIND).setFindNearest(findNearestQueryWithDistanceField).build();
2190+
Query.newEntityQueryBuilder()
2191+
.setKind(VECTOR_KIND)
2192+
.setFindNearest(findNearestQueryWithDistanceField)
2193+
.build();
21812194
QueryResults<Entity> resultWithDistanceField = datastore.run(queryWithWithDistanceField);
21822195
List<Entity> resultsCopyWithDistanceField = makeResultsCopy(resultWithDistanceField);
21832196
// Verify results count
21842197
assertEquals(3, resultsCopyWithDistanceField.size());
2185-
for (int i = 0; i < resultsCopyWithDistanceField.size(); i++)
2186-
{
2198+
for (int i = 0; i < resultsCopyWithDistanceField.size(); i++) {
21872199
// Verify distance field was not 0
2188-
assertNotEquals(DoubleValue.of(0.0), resultsCopyWithDistanceField.get(i).getValue("vector_distance"));
2200+
assertNotEquals(
2201+
DoubleValue.of(0.0), resultsCopyWithDistanceField.get(i).getValue("vector_distance"));
21892202
}
21902203
}
21912204

@@ -2238,7 +2251,7 @@ public void testQueryWithReadTime() throws InterruptedException {
22382251
assertEquals(entity2, withReadTime.next());
22392252
assertFalse(withReadTime.hasNext());
22402253
} finally {
2241-
// datastore.delete(entity1.getKey(), entity2.getKey(), entity3.getKey());
2254+
// datastore.delete(entity1.getKey(), entity2.getKey(), entity3.getKey());
22422255
}
22432256
}
22442257

0 commit comments

Comments
 (0)