diff --git a/src/index.cpp b/src/index.cpp index 71c73de1..c17702ca 100644 --- a/src/index.cpp +++ b/src/index.cpp @@ -4916,6 +4916,10 @@ void Index::refresh_schemas(const std::vector& new_fields, const std::vec search_schema.erase(del_field.name); + if(!del_field.index) { + continue; + } + if(del_field.is_string() || field_types::is_string_or_array(del_field.type)) { art_tree_destroy(search_index[del_field.name]); delete search_index[del_field.name]; diff --git a/test/collection_schema_change_test.cpp b/test/collection_schema_change_test.cpp index 00fcc526..867e101f 100644 --- a/test/collection_schema_change_test.cpp +++ b/test/collection_schema_change_test.cpp @@ -984,7 +984,7 @@ TEST_F(CollectionSchemaChangeTest, ChangeFromStringStarToAutoField) { ASSERT_EQ(1, coll1->get_dynamic_fields().size()); } -TEST_F(CollectionSchemaChangeTest, ChangeFromGeoToIntField) { +TEST_F(CollectionSchemaChangeTest, OrderOfDropShouldNotMatter) { nlohmann::json req_json = R"({ "name": "coll1", "fields": [ @@ -1025,3 +1025,40 @@ TEST_F(CollectionSchemaChangeTest, ChangeFromGeoToIntField) { alter_op = coll1->alter(schema_changes); ASSERT_FALSE(alter_op.ok()); } + +TEST_F(CollectionSchemaChangeTest, IndexFalseToTrue) { + nlohmann::json req_json = R"({ + "name": "coll1", + "fields": [ + {"name": "title", "type": "string", "index": false, "facet": false, "optional": true} + ] + })"_json; + + auto coll1_op = collectionManager.create_collection(req_json); + ASSERT_TRUE(coll1_op.ok()); + + auto coll1 = coll1_op.get(); + + nlohmann::json doc; + doc["id"] = "0"; + doc["title"] = "Typesense"; + + ASSERT_TRUE(coll1->add(doc.dump()).ok()); + + // make field indexable + + auto schema_changes = R"({ + "fields": [ + {"name": "title", "drop": true}, + {"name": "title", "type": "string", "index": true, "facet": true, "optional": true} + ] + })"_json; + + auto alter_op = coll1->alter(schema_changes); + ASSERT_TRUE(alter_op.ok()); + + auto res_op = coll1->search("type", {"title"}, "", {"title"}, {}, {0}, 3, 1, FREQUENCY, {true}, 5); + ASSERT_TRUE(res_op.ok()); + ASSERT_EQ(1, res_op.get()["found"].get()); + ASSERT_EQ(1, res_op.get()["facet_counts"].size()); +} \ No newline at end of file