|
56 | 56 | import com.google.cloud.datastore.LatLngValue; |
57 | 57 | import com.google.cloud.datastore.ListValue; |
58 | 58 | import com.google.cloud.datastore.NullValue; |
| 59 | +import com.google.cloud.datastore.VectorValue; |
| 60 | +import com.google.cloud.datastore.FindNearest; |
59 | 61 | import com.google.cloud.datastore.PathElement; |
60 | 62 | import com.google.cloud.datastore.ProjectionEntity; |
61 | 63 | import com.google.cloud.datastore.Query; |
@@ -2115,6 +2117,58 @@ public void testQueryWithStartCursor() { |
2115 | 2117 | datastore.delete(entity1.getKey(), entity2.getKey(), entity3.getKey()); |
2116 | 2118 | } |
2117 | 2119 |
|
| 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 | + |
2118 | 2172 | @Test |
2119 | 2173 | public void testQueryWithReadTime() throws InterruptedException { |
2120 | 2174 | Entity entity1 = |
|
0 commit comments