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

The `id` cannot be a default sorting field.

Kishore Nallan5f4d43f877a1
diff --git a/include/field.h b/include/field.h
index 35813c95..c8ba94b0 100644
--- a/include/field.h
+++ b/include/field.h
@@ -355,7 +355,7 @@ struct field {
}
}
- if(!default_sorting_field.empty() && !found_default_sorting_field && !fields.empty()) {
+ if(!default_sorting_field.empty() && !found_default_sorting_field) {
return Option<bool>(400, "Default sorting field is defined as `" + default_sorting_field +
"` but is not found in the schema.");
}
diff --git a/src/collection_manager.cpp b/src/collection_manager.cpp
index 126d4824..ec95d1db 100644
--- a/src/collection_manager.cpp
+++ b/src/collection_manager.cpp
@@ -1130,6 +1130,10 @@ Option<Collection*> CollectionManager::create_collection(nlohmann::json& req_jso
const std::string& default_sorting_field = req_json[DEFAULT_SORTING_FIELD].get<std::string>();
+ if(default_sorting_field == "id") {
+ return Option<Collection *>(400, "Invalid `default_sorting_field` value: cannot be `id`.");
+ }
+
std::string fallback_field_type;
std::vector<field> fields;
auto parse_op = field::json_fields_to_fields(req_json[ENABLE_NESTED_FIELDS].get<bool>(),
diff --git a/test/collection_sorting_test.cpp b/test/collection_sorting_test.cpp
index 8bd805a9..121a0e64 100644
--- a/test/collection_sorting_test.cpp
+++ b/test/collection_sorting_test.cpp
@@ -2147,6 +2147,23 @@ TEST_F(CollectionSortingTest, OptionalFilteringViaSortingSearch) {
collectionManager.drop_collection("coll1");
}
+TEST_F(CollectionSortingTest, DisallowIdAsDefaultSortingField) {
+ std::string coll_schema = R"(
+ {
+ "name": "coll1",
+ "default_sorting_field": "id",
+ "fields": [
+ {"name": "id", "type": "string" }
+ ]
+ }
+ )";
+
+ nlohmann::json schema = nlohmann::json::parse(coll_schema);
+ auto coll_op = collectionManager.create_collection(schema);
+ ASSERT_FALSE(coll_op.ok());
+ ASSERT_EQ("Invalid `default_sorting_field` value: cannot be `id`.", coll_op.error());
+}
+
TEST_F(CollectionSortingTest, OptionalFilteringViaSortingSecondThirdParams) {
std::string coll_schema = R"(
{