Fix + add test for int64 as default sorting field.

This commit is contained in:
kishorenc 2020-08-13 07:42:15 +05:30
parent 9a4105111d
commit f54bc8a4b3
3 changed files with 19 additions and 25 deletions

View File

@ -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() &&

View File

@ -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);

View File

@ -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);