Skip to content

Commit a371467

Browse files
committed
Add Integration test
1 parent 0072de4 commit a371467

File tree

2 files changed

+56
-20
lines changed

2 files changed

+56
-20
lines changed

com/google/datastore/snippets/ConceptsTest.java

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -734,35 +734,17 @@ public void testInequalitySortInvalidNotFirst() {
734734
@Test
735735
public void testVectorSearch() {
736736
setUpQueryTests();
737+
// [START datastore_vector_search]
737738
VectorValue vectorValue = VectorValue.newBuilder(1.78, 2.56, 3.88).build();
738739
FindNearest vectorQuery =
739740
new FindNearest(
740741
"vector_property", vectorValue, FindNearest.DistanceMeasure.COSINE, 1, "distance");
741742

742743
Query<Entity> query = Query.newEntityQueryBuilder().setFindNearest(vectorQuery).build();
744+
// [END datastore_vector_search]
743745
assertValidQuery(query);
744746
}
745747

746-
@Test
747-
public void testVectorSearchWithEmptyVector() {
748-
setUpQueryTests();
749-
VectorValue emptyVector = VectorValue.newBuilder().build();
750-
FindNearest vectorQuery =
751-
new FindNearest("vector_property", emptyVector, FindNearest.DistanceMeasure.EUCLIDEAN, 1);
752-
Query<Entity> query = Query.newEntityQueryBuilder().setFindNearest(vectorQuery).build();
753-
assertInvalidQuery(query);
754-
}
755-
756-
@Test
757-
public void testVectorSearchWithUnmatchedVectorSize() {
758-
setUpQueryTests();
759-
VectorValue vectorValue = VectorValue.newBuilder(1.78, 2.56, 3.88, 4.33).build();
760-
FindNearest vectorQuery =
761-
new FindNearest("vector_property", vectorValue, FindNearest.DistanceMeasure.DOT_PRODUCT, 1);
762-
Query<Entity> query = Query.newEntityQueryBuilder().setFindNearest(vectorQuery).build();
763-
assertInvalidQuery(query);
764-
}
765-
766748
@Test
767749
public void testLimit() {
768750
setUpQueryTests();

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

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@
5656
import com.google.cloud.datastore.LatLngValue;
5757
import com.google.cloud.datastore.ListValue;
5858
import com.google.cloud.datastore.NullValue;
59+
import com.google.cloud.datastore.VectorValue;
60+
import com.google.cloud.datastore.FindNearest;
5961
import com.google.cloud.datastore.PathElement;
6062
import com.google.cloud.datastore.ProjectionEntity;
6163
import com.google.cloud.datastore.Query;
@@ -2115,6 +2117,58 @@ public void testQueryWithStartCursor() {
21152117
datastore.delete(entity1.getKey(), entity2.getKey(), entity3.getKey());
21162118
}
21172119

2120+
@Test
2121+
public void testQueryWithVectorSearch() {
2122+
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();
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 = Query.newEntityQueryBuilder().setKind(KIND1).setFindNearest(vectorQuery).build();
2138+
QueryResults<Entity> vectorSearchResult = datastore.run(queryWithVectorSearch);
2139+
assertTrue(vectorSearchResult.hasNext());
2140+
assertEquals(entity1, vectorSearchResult.next());
2141+
assertFalse(vectorSearchResult.hasNext());
2142+
2143+
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();
2150+
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();
2157+
datastore.put(entity2, entity3);
2158+
2159+
// Query to find the nearest 2 neighbors
2160+
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();
2165+
QueryResults<Entity> resultsWithVectorLimit = datastore.run(queryWithVectorSearchLimit);
2166+
List<Entity> resultsCopy = makeResultsCopy(resultsWithVectorLimit);
2167+
assertEquals(2, resultsCopy.size());
2168+
2169+
datastore.delete(entity1.getKey(), entity2.getKey(), entity3.getKey());
2170+
}
2171+
21182172
@Test
21192173
public void testQueryWithReadTime() throws InterruptedException {
21202174
Entity entity1 =

0 commit comments

Comments
 (0)