|
46 | 46 | import com.google.cloud.datastore.Entity; |
47 | 47 | import com.google.cloud.datastore.EntityQuery; |
48 | 48 | import com.google.cloud.datastore.EntityValue; |
| 49 | +import com.google.cloud.datastore.FindNearest; |
49 | 50 | import com.google.cloud.datastore.FullEntity; |
50 | 51 | import com.google.cloud.datastore.GqlQuery; |
51 | 52 | import com.google.cloud.datastore.IncompleteKey; |
|
56 | 57 | import com.google.cloud.datastore.LatLngValue; |
57 | 58 | import com.google.cloud.datastore.ListValue; |
58 | 59 | import com.google.cloud.datastore.NullValue; |
59 | | -import com.google.cloud.datastore.VectorValue; |
60 | | -import com.google.cloud.datastore.FindNearest; |
61 | 60 | import com.google.cloud.datastore.PathElement; |
62 | 61 | import com.google.cloud.datastore.ProjectionEntity; |
63 | 62 | import com.google.cloud.datastore.Query; |
|
72 | 71 | import com.google.cloud.datastore.TimestampValue; |
73 | 72 | import com.google.cloud.datastore.Transaction; |
74 | 73 | import com.google.cloud.datastore.ValueType; |
| 74 | +import com.google.cloud.datastore.VectorValue; |
75 | 75 | import com.google.cloud.datastore.models.ExecutionStats; |
76 | 76 | import com.google.cloud.datastore.models.ExplainMetrics; |
77 | 77 | import com.google.cloud.datastore.models.ExplainOptions; |
@@ -2120,48 +2120,54 @@ public void testQueryWithStartCursor() { |
2120 | 2120 | @Test |
2121 | 2121 | public void testQueryWithVectorSearch() { |
2122 | 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(); |
| 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 | 2129 | datastore.put(entity1); |
2130 | 2130 |
|
2131 | 2131 | VectorValue vectorValue = VectorValue.newBuilder(1.78, 2.56, 3.88).build(); |
2132 | 2132 |
|
2133 | 2133 | // Query to find the nearest 1 neighbor |
2134 | 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(); |
| 2135 | + new FindNearest( |
| 2136 | + "vector_property", vectorValue, FindNearest.DistanceMeasure.COSINE, 1, "distance"); |
| 2137 | + Query<Entity> queryWithVectorSearch = |
| 2138 | + Query.newEntityQueryBuilder().setKind(KIND1).setFindNearest(vectorQuery).build(); |
2138 | 2139 | QueryResults<Entity> vectorSearchResult = datastore.run(queryWithVectorSearch); |
2139 | 2140 | assertTrue(vectorSearchResult.hasNext()); |
2140 | 2141 | assertEquals(entity1, vectorSearchResult.next()); |
2141 | 2142 | assertFalse(vectorSearchResult.hasNext()); |
2142 | 2143 |
|
2143 | 2144 | 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(); |
2150 | 2151 | 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(); |
2157 | 2158 | datastore.put(entity2, entity3); |
2158 | 2159 |
|
2159 | 2160 | // Query to find the nearest 2 neighbors |
2160 | 2161 | 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(); |
2165 | 2171 | QueryResults<Entity> resultsWithVectorLimit = datastore.run(queryWithVectorSearchLimit); |
2166 | 2172 | List<Entity> resultsCopy = makeResultsCopy(resultsWithVectorLimit); |
2167 | 2173 | assertEquals(2, resultsCopy.size()); |
|
0 commit comments