@@ -1120,7 +1120,8 @@ void SearchFamily::FtAlter(CmdArgList args, const CommandContext& cmd_cntx) {
11201120 // Rebuild index
11211121 // TODO: Introduce partial rebuild
11221122 auto upd_cb = [idx_name, index_info](Transaction* tx, EngineShard* es) {
1123- es->search_indices ()->DropIndex (idx_name);
1123+ // Drop the old index (we don't need the documents)
1124+ (void )es->search_indices ()->DropIndex (idx_name);
11241125 es->search_indices ()->InitIndex (tx->GetOpArgs (es), idx_name, index_info);
11251126 return OpStatus::OK;
11261127 };
@@ -1142,31 +1143,26 @@ void SearchFamily::FtDropIndex(CmdArgList args, const CommandContext& cmd_cntx)
11421143 }
11431144 }
11441145
1145- // Collect document keys (if DD is set), drop index, and delete documents in single transaction
1146- vector<vector<string>> shard_doc_keys (delete_docs ? shard_set->size () : 0 );
11471146 atomic_uint num_deleted{0 };
11481147
11491148 auto cb = [&](Transaction* t, EngineShard* es) {
1150- // Step 1: If DD is set, collect all document keys before dropping the index
1151- if (delete_docs) {
1152- auto * index = es->search_indices ()->GetIndex (idx_name);
1153- if (index) {
1154- shard_doc_keys[es->shard_id ()] = index->GetAllKeys ();
1155- }
1156- }
1149+ // Drop the index and get its pointer
1150+ auto index = es->search_indices ()->DropIndex (idx_name);
1151+ if (!index)
1152+ return OpStatus::OK;
11571153
1158- // Step 2: Drop the index
1159- if (es->search_indices ()->DropIndex (idx_name))
1160- num_deleted.fetch_add (1 );
1154+ num_deleted.fetch_add (1 );
11611155
1162- // Step 3: If DD is set, delete all documents that were in the index
1156+ // If DD is set, delete all documents that were in the index
11631157 if (delete_docs) {
1164- const auto & keys = shard_doc_keys[es->shard_id ()];
1165- if (!keys.empty ()) {
1158+ // Get const reference to document keys map (index will be destroyed after this scope)
1159+ const auto & doc_keys = index->key_index ().GetDocKeysMap ();
1160+
1161+ if (!doc_keys.empty ()) {
11661162 auto op_args = t->GetOpArgs (es);
11671163 auto & db_slice = op_args.GetDbSlice ();
11681164
1169- for (const auto & key : keys ) {
1165+ for (const auto & [ key, doc_id] : doc_keys ) {
11701166 auto it = db_slice.FindMutable (op_args.db_cntx , key).it ;
11711167 if (IsValid (it)) {
11721168 db_slice.Del (op_args.db_cntx , it);
0 commit comments