Skip to content

Open Source alternative to Algolia + Pinecone and an Easier-to-Use alternative to ElasticSearch ⚡ 🔍 ✨ Fast, typo tolerant, in-memory fuzzy Search Engine for building delightful search experiences

Added support to add text embedding fields with schema update

ozanarmagandede71bad993
diff --git a/src/collection.cpp b/src/collection.cpp
index 59b81ea3..d6aa3403 100644
--- a/src/collection.cpp
+++ b/src/collection.cpp
@@ -4131,7 +4131,7 @@ Option<bool> Collection::validate_alter_payload(nlohmann::json& schema_changes,
}
if(!f.embed_from.empty()) {
- return Option<bool>(400, "Embedding fields can only be added at the time of collection creation.");
+ embedding_fields.emplace(f.name, f);
}
if(f.nested && enable_nested_fields) {
@@ -4146,7 +4146,7 @@ Option<bool> Collection::validate_alter_payload(nlohmann::json& schema_changes,
updated_nested_fields.emplace(prefix_kv.key(), prefix_kv.value());
if(!prefix_kv.value().embed_from.empty()) {
- return Option<bool>(400, "Embedding fields can only be added at the time of collection creation.");
+ embedding_fields.emplace(prefix_kv.key(), prefix_kv.value());
}
if(is_reindex) {
diff --git a/test/collection_schema_change_test.cpp b/test/collection_schema_change_test.cpp
index 1a301446..88d8e940 100644
--- a/test/collection_schema_change_test.cpp
+++ b/test/collection_schema_change_test.cpp
@@ -1455,10 +1455,13 @@ TEST_F(CollectionSchemaChangeTest, UpdateSchemaWithNewEmbeddingField) {
]
})"_json;
+ TextEmbedderManager::set_model_dir("/tmp/typesense_test/models");
+ TextEmbedderManager::download_default_model();
auto op = collectionManager.create_collection(schema);
ASSERT_TRUE(op.ok());
Collection* coll = op.get();
+
nlohmann::json update_schema = R"({
"fields": [
@@ -1468,8 +1471,17 @@ TEST_F(CollectionSchemaChangeTest, UpdateSchemaWithNewEmbeddingField) {
auto res = coll->alter(update_schema);
- ASSERT_FALSE(res.ok());
- ASSERT_EQ("Embedding fields can only be added at the time of collection creation.", res.error());
+ ASSERT_TRUE(res.ok());
+ ASSERT_EQ(1, coll->get_embedding_fields().size());
+
+ nlohmann::json doc;
+ doc["names"] = {"hello", "world"};
+ auto add_op = coll->add(doc.dump());
+
+ ASSERT_TRUE(add_op.ok());
+ auto added_doc = add_op.get();
+
+ ASSERT_EQ(384, added_doc["embedding"].get<std::vector<float>>().size());
}
TEST_F(CollectionSchemaChangeTest, DropFieldUsedForEmbedding) {