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

Let sortable string field be a default sorting field.

Kishore Nallanf9942e435846
diff --git a/include/field.h b/include/field.h
index 699c6cf6..4948264b 100644
--- a/include/field.h
+++ b/include/field.h
@@ -248,11 +248,9 @@ struct field {
"`, see docs for supported data types.");
}
- if(field.name == default_sorting_field && !(field.type == field_types::INT32 ||
- field.type == field_types::INT64 ||
- field.type == field_types::FLOAT)) {
+ if(field.name == default_sorting_field && !field.is_sortable()) {
return Option<bool>(400, "Default sorting field `" + default_sorting_field +
- "` must be a single valued numerical field.");
+ "` is not a sortable type.");
}
if(field.name == default_sorting_field) {
diff --git a/src/index.cpp b/src/index.cpp
index fa215a32..71fa791d 100644
--- a/src/index.cpp
+++ b/src/index.cpp
@@ -165,6 +165,9 @@ int64_t Index::get_points_from_doc(const nlohmann::json &document, const std::st
memcpy(&points, &n, sizeof(int32_t));
points ^= ((points >> (std::numeric_limits<int32_t>::digits - 1)) | INT32_MIN);
points = -1 * (INT32_MAX - points);
+ } else if(document[default_sorting_field].is_string()) {
+ // not much value in supporting default sorting field as string, so we will just dummy it out
+ points = 0;
} else {
points = document[default_sorting_field];
}
diff --git a/test/collection_filtering_test.cpp b/test/collection_filtering_test.cpp
index dd406909..2d3d8879 100644
--- a/test/collection_filtering_test.cpp
+++ b/test/collection_filtering_test.cpp
@@ -519,7 +519,7 @@ TEST_F(CollectionFilteringTest, FilterOnNumericFields) {
// ensure that default_sorting_field is a non-array numerical field
auto coll_op = collectionManager.create_collection("coll_array_fields", 4, fields, "years");
ASSERT_EQ(false, coll_op.ok());
- ASSERT_STREQ("Default sorting field `years` must be a single valued numerical field.", coll_op.error().c_str());
+ ASSERT_STREQ("Default sorting field `years` is not a sortable type.", coll_op.error().c_str());
// let's try again properly
coll_op = collectionManager.create_collection("coll_array_fields", 4, fields, "age");
diff --git a/test/collection_sorting_test.cpp b/test/collection_sorting_test.cpp
index 8dcb686f..4a9bf745 100644
--- a/test/collection_sorting_test.cpp
+++ b/test/collection_sorting_test.cpp
@@ -130,7 +130,7 @@ TEST_F(CollectionSortingTest, DefaultSortingFieldValidations) {
Option<Collection*> collection_op = collectionManager.create_collection("sample_collection", 4, fields, "name");
ASSERT_FALSE(collection_op.ok());
- ASSERT_EQ("Default sorting field `name` must be a single valued numerical field.", collection_op.error());
+ ASSERT_EQ("Default sorting field `name` is not a sortable type.", collection_op.error());
collectionManager.drop_collection("sample_collection");
// Default sorting field must exist as a field in schema
@@ -1054,7 +1054,9 @@ TEST_F(CollectionSortingTest, SortByTitle) {
coll1 = collectionManager.get_collection("coll1").get();
if(coll1 == nullptr) {
- coll1 = collectionManager.create_collection("coll1", 2, fields, "points").get();
+ auto create_op = collectionManager.create_collection("coll1", 2, fields, "title");
+ ASSERT_TRUE(create_op.ok());
+ coll1 = create_op.get();
}
std::vector<std::vector<std::string>> records = {
@@ -1132,5 +1134,15 @@ TEST_F(CollectionSortingTest, SortByTitle) {
ASSERT_FALSE(res_op.ok());
ASSERT_EQ("Could not find a field named `artist` in the schema for sorting.", res_op.error());
+ // don't allow non-sort string field to be used as default sorting field
+
+ fields = {field("title", field_types::STRING, false, false, true, "", false),
+ field("artist", field_types::STRING, true),
+ field("points", field_types::INT32, false),};
+
+ auto create_op = collectionManager.create_collection("coll2", 2, fields, "title");
+ ASSERT_FALSE(create_op.ok());
+ ASSERT_EQ("Default sorting field `title` is not a sortable type.", create_op.error());
+
collectionManager.drop_collection("coll1");
}