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

Improve validation for non-indexed sort field.

Kishore Nallan3f90943baef5
diff --git a/src/collection.cpp b/src/collection.cpp
index 7ca585e1..93f72334 100644
--- a/src/collection.cpp
+++ b/src/collection.cpp
@@ -552,7 +552,7 @@ Option<bool> Collection::validate_and_standardize_sort_fields(const std::vector<
sort_field_std.text_match_buckets = std::stoll(match_parts[1]);
} else {
- if(field_it == search_schema.end() || !field_it->second.sort) {
+ if(field_it == search_schema.end()) {
std::string error = "Could not find a field named `" + actual_field_name + "` in the schema for sorting.";
return Option<bool>(404, error);
}
@@ -674,10 +674,13 @@ Option<bool> Collection::validate_and_standardize_sort_fields(const std::vector<
}
}
- if(sort_field_std.name != sort_field_const::text_match && (search_schema.count(sort_field_std.name) == 0 ||
- !search_schema.at(sort_field_std.name).sort)) {
- std::string error = "Could not find a field named `" + sort_field_std.name + "` in the schema for sorting.";
- return Option<bool>(404, error);
+ if(sort_field_std.name != sort_field_const::text_match) {
+ const auto field_it = search_schema.find(sort_field_std.name);
+ if(field_it == search_schema.end() || !field_it->second.sort || !field_it->second.index) {
+ std::string error = "Could not find a field named `" + sort_field_std.name +
+ "` in the schema for sorting.";
+ return Option<bool>(404, error);
+ }
}
StringUtils::toupper(sort_field_std.order);
diff --git a/test/collection_sorting_test.cpp b/test/collection_sorting_test.cpp
index 476c1afb..134d5a2a 100644
--- a/test/collection_sorting_test.cpp
+++ b/test/collection_sorting_test.cpp
@@ -1778,3 +1778,35 @@ TEST_F(CollectionSortingTest, IntegerFloatAndBoolShouldDefaultSortTrue) {
collectionManager.drop_collection("coll1");
}
+
+TEST_F(CollectionSortingTest, DisallowSortingOnNonIndexedIntegerField) {
+ std::string coll_schema = R"(
+ {
+ "name": "coll1",
+ "fields": [
+ {"name": "title", "type": "string" },
+ {"name": "points", "type": "int32", "index": false, "optional": true }
+ ]
+ }
+ )";
+
+ nlohmann::json schema = nlohmann::json::parse(coll_schema);
+ Collection* coll1 = collectionManager.create_collection(schema).get();
+
+ nlohmann::json doc1;
+ doc1["id"] = "0";
+ doc1["title"] = "Right on";
+ doc1["points"] = 100;
+
+ ASSERT_TRUE(coll1->add(doc1.dump()).ok());
+
+ auto res_op = coll1->search("*", {"title"}, "", {}, {sort_by("points", "DESC")}, {2}, 10, 1, FREQUENCY, {true}, 10);
+ ASSERT_FALSE(res_op.ok());
+ ASSERT_EQ("Could not find a field named `points` in the schema for sorting.", res_op.error());
+
+ res_op = coll1->search("*", {"title"}, "", {}, {sort_by("points(missing_values: first)", "DESC")}, {2}, 10, 1, FREQUENCY, {true}, 10);
+ ASSERT_FALSE(res_op.ok());
+ ASSERT_EQ("Could not find a field named `points` in the schema for sorting.", res_op.error());
+
+ collectionManager.drop_collection("coll1");
+}