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 + add test for int64 as default sorting field.

kishorencf54bc8a4b375
diff --git a/src/index.cpp b/src/index.cpp
index a22e3def..e920c35e 100644
--- a/src/index.cpp
+++ b/src/index.cpp
@@ -218,12 +218,7 @@ Option<uint32_t> Index::validate_index_in_memory(const nlohmann::json &document,
}
if(!document[default_sorting_field].is_number_integer() && !document[default_sorting_field].is_number_float()) {
- return Option<>(400, "Default sorting field `" + default_sorting_field + "` must be of type int32 or float.");
- }
-
- if(search_schema.at(default_sorting_field).is_single_integer() &&
- document[default_sorting_field].get<int64_t>() > std::numeric_limits<int32_t>::max()) {
- return Option<>(400, "Default sorting field `" + default_sorting_field + "` exceeds maximum value of an int32.");
+ return Option<>(400, "Default sorting field `" + default_sorting_field + "` must be a single valued numerical field.");
}
if(search_schema.at(default_sorting_field).is_single_float() &&
diff --git a/test/collection_sorting_test.cpp b/test/collection_sorting_test.cpp
index 1a5901a5..8cdb8104 100644
--- a/test/collection_sorting_test.cpp
+++ b/test/collection_sorting_test.cpp
@@ -155,21 +155,25 @@ TEST_F(CollectionSortingTest, Int64AsDefaultSortingField) {
coll_mul_fields = collectionManager.create_collection("coll_mul_fields", fields, "points").get();
}
- std::string json_line;
+ auto doc_str1 = "{\"title\": \"foo\", \"starring\": \"bar\", \"points\": 343234324234233234, \"cast\": [\"baz\"] }";
+ const Option<nlohmann::json> & add_op = coll_mul_fields->add(doc_str1);
+ ASSERT_TRUE(add_op.ok());
- while (std::getline(infile, json_line)) {
- coll_mul_fields->add(json_line);
- }
+ auto doc_str2 = "{\"title\": \"foo\", \"starring\": \"bar\", \"points\": 343234324234233232, \"cast\": [\"baz\"] }";
+ auto doc_str3 = "{\"title\": \"foo\", \"starring\": \"bar\", \"points\": 343234324234233235, \"cast\": [\"baz\"] }";
+ auto doc_str4 = "{\"title\": \"foo\", \"starring\": \"bar\", \"points\": 343234324234233231, \"cast\": [\"baz\"] }";
- infile.close();
+ coll_mul_fields->add(doc_str2);
+ coll_mul_fields->add(doc_str3);
+ coll_mul_fields->add(doc_str4);
query_fields = {"title"};
std::vector<std::string> facets;
sort_fields = { sort_by("points", "ASC") };
- nlohmann::json results = coll_mul_fields->search("the", query_fields, "", facets, sort_fields, 0, 15, 1, FREQUENCY, false).get();
- ASSERT_EQ(10, results["hits"].size());
+ nlohmann::json results = coll_mul_fields->search("foo", query_fields, "", facets, sort_fields, 0, 10, 1, FREQUENCY, false).get();
+ ASSERT_EQ(4, results["hits"].size());
- std::vector<std::string> ids = {"17", "13", "10", "4", "0", "1", "8", "6", "16", "11"};
+ std::vector<std::string> ids = {"3", "1", "0", "2"};
for(size_t i = 0; i < results["hits"].size(); i++) {
nlohmann::json result = results["hits"].at(i);
@@ -178,12 +182,12 @@ TEST_F(CollectionSortingTest, Int64AsDefaultSortingField) {
ASSERT_STREQ(id.c_str(), result_id.c_str());
}
- // limiting results to just 5, "ASC" keyword must be case insensitive
- sort_fields = { sort_by("points", "asc") };
- results = coll_mul_fields->search("the", query_fields, "", facets, sort_fields, 0, 5, 1, FREQUENCY, false).get();
- ASSERT_EQ(5, results["hits"].size());
+ // DESC
+ sort_fields = { sort_by("points", "desc") };
+ results = coll_mul_fields->search("foo", query_fields, "", facets, sort_fields, 0, 10, 1, FREQUENCY, false).get();
+ ASSERT_EQ(4, results["hits"].size());
- ids = {"17", "13", "10", "4", "0"};
+ ids = {"2", "0", "1", "3"};
for(size_t i = 0; i < results["hits"].size(); i++) {
nlohmann::json result = results["hits"].at(i);
diff --git a/test/collection_test.cpp b/test/collection_test.cpp
index 6ef3b73f..31873bfe 100644
--- a/test/collection_test.cpp
+++ b/test/collection_test.cpp
@@ -1742,12 +1742,7 @@ TEST_F(CollectionTest, IndexingWithBadData) {
doc_str = "{\"name\": \"foo\", \"age\": \"34\", \"tags\": [], \"average\": 34 }";
const Option<nlohmann::json> & bad_default_sorting_field_op1 = sample_collection->add(doc_str);
ASSERT_FALSE(bad_default_sorting_field_op1.ok());
- ASSERT_STREQ("Default sorting field `age` must be of type int32 or float.", bad_default_sorting_field_op1.error().c_str());
-
- doc_str = "{\"name\": \"foo\", \"age\": 343234324234233234, \"tags\": [], \"average\": 34 }";
- const Option<nlohmann::json> & bad_default_sorting_field_op2 = sample_collection->add(doc_str);
- ASSERT_FALSE(bad_default_sorting_field_op2.ok());
- ASSERT_STREQ("Default sorting field `age` exceeds maximum value of an int32.", bad_default_sorting_field_op2.error().c_str());
+ ASSERT_STREQ("Default sorting field `age` must be a single valued numerical field.", bad_default_sorting_field_op1.error().c_str());
doc_str = "{\"name\": \"foo\", \"tags\": [], \"average\": 34 }";
const Option<nlohmann::json> & bad_default_sorting_field_op3 = sample_collection->add(doc_str);