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

Ensure that default sorting field exists in schema during collection creation.

kishorenc53086d6a5298
diff --git a/src/collection_manager.cpp b/src/collection_manager.cpp
index 70d7dd22..d9786376 100644
--- a/src/collection_manager.cpp
+++ b/src/collection_manager.cpp
@@ -210,9 +210,9 @@ Option<Collection*> CollectionManager::create_collection(const std::string name,
return Option<Collection*>(409, std::string("A collection with name `") + name + "` already exists.");
}
- nlohmann::json collection_meta;
-
+ bool found_default_sorting_field = false;
nlohmann::json fields_json = nlohmann::json::array();;
+
for(const field & field: fields) {
nlohmann::json field_val;
field_val[fields::name] = field.name;
@@ -225,8 +225,18 @@ Option<Collection*> CollectionManager::create_collection(const std::string name,
return Option<Collection*>(400, "Default sorting field `" + default_sorting_field +
"` must be of type int32 or float.");
}
+
+ if(field.name == default_sorting_field) {
+ found_default_sorting_field = true;
+ }
+ }
+
+ if(!found_default_sorting_field) {
+ return Option<Collection*>(400, "Default sorting field is defined as `" + default_sorting_field +
+ "` but is not found in the schema.");
}
+ nlohmann::json collection_meta;
collection_meta[COLLECTION_NAME_KEY] = name;
collection_meta[COLLECTION_ID_KEY] = next_collection_id;
collection_meta[COLLECTION_SEARCH_FIELDS_KEY] = fields_json;
diff --git a/test/collection_test.cpp b/test/collection_test.cpp
index 11fb8eaa..2d747f6f 100644
--- a/test/collection_test.cpp
+++ b/test/collection_test.cpp
@@ -1939,7 +1939,8 @@ TEST_F(CollectionTest, SearchingWithMissingFields) {
collectionManager.drop_collection("coll_array_fields");
}
-TEST_F(CollectionTest, DefaultSortingFieldMustBeInt32OrFloat) {
+TEST_F(CollectionTest, DefaultSortingFieldValidations) {
+ // Default sorting field must be int32 or float
std::vector<field> fields = {field("name", field_types::STRING, false),
field("tags", field_types::STRING_ARRAY, true),
field("age", field_types::INT32, false),
@@ -1951,6 +1952,19 @@ TEST_F(CollectionTest, DefaultSortingFieldMustBeInt32OrFloat) {
EXPECT_FALSE(collection_op.ok());
EXPECT_EQ("Default sorting field `name` must be of type int32 or float.", collection_op.error());
collectionManager.drop_collection("sample_collection");
+
+ // Default sorting field must exist as a field in schema
+
+ fields = {field("name", field_types::STRING, false),
+ field("tags", field_types::STRING_ARRAY, true),
+ field("age", field_types::INT32, false),
+ field("average", field_types::INT32, false) };
+
+ sort_fields = { sort_by("age", "DESC"), sort_by("average", "DESC") };
+ collection_op = collectionManager.create_collection("sample_collection", fields, "NOT-DEFINED");
+ EXPECT_FALSE(collection_op.ok());
+ EXPECT_EQ("Default sorting field is defined as `NOT-DEFINED` but is not found in the schema.", collection_op.error());
+ collectionManager.drop_collection("sample_collection");
}
TEST_F(CollectionTest, IndexingWithBadData) {