@@ -3374,3 +3374,45 @@ TYPED_TEST(HNSWTieredIndexTest, parallelRangeSearch) {
33743374 // Cleanup.
33753375 delete index_ctx;
33763376}
3377+
3378+ TYPED_TEST (HNSWTieredIndexTestBasic, preferAdHocOptimization) {
3379+ size_t dim = 4 ;
3380+
3381+ HNSWParams params = {
3382+ .type = TypeParam::get_index_type (),
3383+ .dim = dim,
3384+ .metric = VecSimMetric_L2,
3385+ };
3386+ VecSimParams hnsw_params = CreateParams (params);
3387+ auto jobQ = JobQueue ();
3388+ auto index_ctx = new IndexExtCtx ();
3389+ size_t memory_ctx = 0 ;
3390+
3391+ // Create tiered index with buffer limit set to 0.
3392+ auto *tiered_index = this ->CreateTieredHNSWIndex (hnsw_params, &jobQ, index_ctx, &memory_ctx);
3393+ auto allocator = tiered_index->getAllocator ();
3394+
3395+ auto hnsw = tiered_index->backendIndex ;
3396+ auto flat = tiered_index->frontendIndex ;
3397+
3398+ // Insert 5 vectors to the main index.
3399+ for (size_t i = 0 ; i < 5 ; i++) {
3400+ GenerateAndAddVector<TEST_DATA_T>(hnsw, dim, i, i);
3401+ }
3402+ // Sanity check. Should choose as HNSW.
3403+ ASSERT_EQ (tiered_index->preferAdHocSearch (5 , 5 , true ), hnsw->preferAdHocSearch (5 , 5 , true ));
3404+
3405+ // Insert 6 vectors to the flat index.
3406+ for (size_t i = 0 ; i < 6 ; i++) {
3407+ GenerateAndAddVector<TEST_DATA_T>(flat, dim, i, i);
3408+ }
3409+ // Sanity check. Should choose as flat as it has more vectors.
3410+ ASSERT_EQ (tiered_index->preferAdHocSearch (5 , 5 , true ), flat->preferAdHocSearch (5 , 5 , true ));
3411+
3412+ // Check for preference of tiered with subset (10) smaller than the tiered index size (11),
3413+ // but larger than any of the underlying indexes.
3414+ ASSERT_NO_THROW (tiered_index->preferAdHocSearch (10 , 5 , false ));
3415+
3416+ // Cleanup.
3417+ delete index_ctx;
3418+ }
0 commit comments