|
24 | 24 | import static org.junit.Assert.assertNull; |
25 | 25 |
|
26 | 26 | 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.*; |
46 | 28 | import com.google.cloud.datastore.StructuredQuery.CompositeFilter; |
47 | 29 | import com.google.cloud.datastore.StructuredQuery.OrderBy; |
48 | 30 | import com.google.cloud.datastore.StructuredQuery.PropertyFilter; |
49 | | -import com.google.cloud.datastore.Transaction; |
50 | 31 | import com.google.cloud.datastore.testing.LocalDatastoreHelper; |
51 | 32 | import com.google.common.collect.ImmutableList; |
52 | 33 | import com.google.common.collect.ImmutableMap; |
@@ -399,6 +380,9 @@ private void setUpQueryTests() { |
399 | 380 | "description", |
400 | 381 | StringValue.newBuilder("Learn Cloud Datastore").setExcludeFromIndexes(true).build()) |
401 | 382 | .set("tag", "fun", "l", "programming", "learn") |
| 383 | + .set( |
| 384 | + "vector_property", |
| 385 | + VectorValue.newBuilder(3.0, 1.0, 2.0).setExcludeFromIndexes(true).build()) |
402 | 386 | .build()); |
403 | 387 | } |
404 | 388 |
|
@@ -717,6 +701,38 @@ public void testInequalitySortInvalidNotFirst() { |
717 | 701 | assertInvalidQuery(query); |
718 | 702 | } |
719 | 703 |
|
| 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 | + |
720 | 736 | @Test |
721 | 737 | public void testLimit() { |
722 | 738 | setUpQueryTests(); |
|
0 commit comments