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

Fix boolean field as default sorting field.

Kishore Nallan2a75f6fc7368
diff --git a/src/index.cpp b/src/index.cpp
index 9e539236..749ca44d 100644
--- a/src/index.cpp
+++ b/src/index.cpp
@@ -215,7 +215,8 @@ int64_t Index::get_points_from_doc(const nlohmann::json &document, const std::st
// 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];
+ points = document[default_sorting_field].is_boolean() ? int64_t(document[default_sorting_field].get<bool>()) :
+ document[default_sorting_field].get<int64_t>();
}
return points;
diff --git a/test/collection_sorting_test.cpp b/test/collection_sorting_test.cpp
index afd678c1..8bd805a9 100644
--- a/test/collection_sorting_test.cpp
+++ b/test/collection_sorting_test.cpp
@@ -124,6 +124,7 @@ TEST_F(CollectionSortingTest, DefaultSortingFieldValidations) {
std::vector<field> fields = {field("name", field_types::STRING, false),
field("tags", field_types::STRING_ARRAY, true),
field("age", field_types::INT32, false),
+ field("in_stock", field_types::BOOL, false),
field("average", field_types::INT32, false) };
std::vector<sort_by> sort_fields = { sort_by("age", "DESC"), sort_by("average", "DESC") };
@@ -140,6 +141,19 @@ TEST_F(CollectionSortingTest, DefaultSortingFieldValidations) {
ASSERT_FALSE(collection_op.ok());
ASSERT_EQ("Default sorting field is defined as `NOT-DEFINED` but is not found in the schema.", collection_op.error());
collectionManager.drop_collection("sample_collection");
+
+ // must be able to use boolean field as default sorting field
+ collection_op = collectionManager.create_collection("sample_collection", 4, fields, "in_stock");
+ ASSERT_TRUE(collection_op.ok());
+ auto coll = collection_op.get();
+ nlohmann::json doc;
+ doc["name"] = "Example";
+ doc["tags"] = {"example"};
+ doc["age"] = 100;
+ doc["in_stock"] = true;
+ doc["average"] = 45;
+
+ ASSERT_TRUE(coll->add(doc.dump()).ok());
}
TEST_F(CollectionSortingTest, NoDefaultSortingField) {