@@ -2124,54 +2124,54 @@ public void testQueryWithVectorSearch() {
21242124 Key .newBuilder (PROJECT_ID , KIND1 , "name-01" , options .getDatabaseId ()).build ())
21252125 .set (
21262126 "vector_property" ,
2127- VectorValue .newBuilder (3.0 , 1 .0 , 2.0 ).setExcludeFromIndexes (true ).build ())
2127+ VectorValue .newBuilder (3.0 , 9 .0 , 11.1 ).setExcludeFromIndexes (true ).build ())
21282128 .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 =
2138- Query .newEntityQueryBuilder ().setKind (KIND1 ).setFindNearest (vectorQuery ).build ();
2139- QueryResults <Entity > vectorSearchResult = datastore .run (queryWithVectorSearch );
2140- assertTrue (vectorSearchResult .hasNext ());
2141- assertEquals (entity1 , vectorSearchResult .next ());
2142- assertFalse (vectorSearchResult .hasNext ());
2143-
21442129 Entity entity2 =
21452130 Entity .newBuilder (
21462131 Key .newBuilder (PROJECT_ID , KIND1 , "name-02" , options .getDatabaseId ()).build ())
21472132 .set (
21482133 "vector_property" ,
2149- VectorValue .newBuilder (5.0 , 0.7 , 2.0 ).setExcludeFromIndexes (true ).build ())
2134+ VectorValue .newBuilder (2.8 , 2.56 , 3.8 ).setExcludeFromIndexes (true ).build ())
21502135 .build ();
21512136 Entity entity3 =
21522137 Entity .newBuilder (
21532138 Key .newBuilder (PROJECT_ID , KIND1 , "name-03" , options .getDatabaseId ()).build ())
21542139 .set (
21552140 "vector_property" ,
2156- VectorValue .newBuilder (2.0 , 1.7 , 1.0 ).setExcludeFromIndexes (true ).build ())
2141+ VectorValue .newBuilder (2.8 , 2.56 , 3.88 ).setExcludeFromIndexes (true ).build ())
21572142 .build ();
2158- datastore .put (entity2 , entity3 );
2143+ datastore .put (entity1 , entity2 , entity3 );
21592144
2160- // Query to find the nearest 2 neighbors
2161- FindNearest vectorQueryWithLimit =
2145+ // Query to find the nearest 2 neighbors with COSINE distance
2146+ FindNearest findNearestQuery =
21622147 new FindNearest (
21632148 "vector_property" ,
2164- VectorValue .newBuilder (2.8 , 2.56 , 3.88 ).build (),
2165- FindNearest .DistanceMeasure .EUCLIDEAN ,
2149+ VectorValue .newBuilder (1.78 , 2.56 , 3.88 ).build (),
2150+ FindNearest .DistanceMeasure .COSINE ,
21662151 2 ,
21672152 "distance" );
2168-
2169- Query < Entity > queryWithVectorSearchLimit =
2170- Query . newEntityQueryBuilder (). setKind ( KIND1 ). setFindNearest ( vectorQueryWithLimit ). build ( );
2171- QueryResults <Entity > resultsWithVectorLimit = datastore . run ( queryWithVectorSearchLimit );
2172- List < Entity > resultsCopy = makeResultsCopy ( resultsWithVectorLimit );
2153+ Query < Entity > queryWithVectorSearch =
2154+ Query . newEntityQueryBuilder (). setKind ( KIND1 ). setFindNearest ( findNearestQuery ). build ();
2155+ QueryResults < Entity > vectorSearchResult = datastore . run ( queryWithVectorSearch );
2156+ List <Entity > resultsCopy = makeResultsCopy ( vectorSearchResult );
2157+ // Should return nearest 2 neighbors
21732158 assertEquals (2 , resultsCopy .size ());
21742159
2160+ // Query to find the nearest neighbor with EUCLIDEAN distance
2161+ FindNearest findNearestWithLimit1 =
2162+ new FindNearest (
2163+ "vector_property" ,
2164+ VectorValue .newBuilder (2.8 , 2.56 , 3.88 ).build (),
2165+ FindNearest .DistanceMeasure .EUCLIDEAN ,
2166+ 1 ,
2167+ "distance" );
2168+ Query <Entity > vectorQueryWithLimit1 =
2169+ Query .newEntityQueryBuilder ().setKind (KIND1 ).setFindNearest (findNearestWithLimit1 ).build ();
2170+ QueryResults <Entity > resultsWithVectorLimit1 = datastore .run (vectorQueryWithLimit1 );
2171+ assertTrue (resultsWithVectorLimit1 .hasNext ());
2172+ // entity3 should be the nearest neighbor
2173+ assertEquals (entity3 , resultsWithVectorLimit1 .next ());
2174+ assertFalse (resultsWithVectorLimit1 .hasNext ());
21752175 datastore .delete (entity1 .getKey (), entity2 .getKey (), entity3 .getKey ());
21762176 }
21772177
0 commit comments