From 5de270f4ae78ecae5605010e3d109c3675a8bf98 Mon Sep 17 00:00:00 2001 From: kishorenc Date: Thu, 20 Feb 2020 18:34:31 +0530 Subject: [PATCH] Allow large float values for `default_sorting_field`. Fixes https://github.com/typesense/typesense/issues/94 --- src/index.cpp | 4 ++-- test/collection_test.cpp | 9 ++++++++- test/float_documents.jsonl | 2 +- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/index.cpp b/src/index.cpp index c7e5ca32..200f0847 100644 --- a/src/index.cpp +++ b/src/index.cpp @@ -157,12 +157,12 @@ Option Index::validate_index_in_memory(const nlohmann::json &document, return Option<>(400, "Default sorting field `" + default_sorting_field + "` must be of type int32 or float."); } - if(document[default_sorting_field].is_number_integer() && + if(search_schema.at(default_sorting_field).is_single_integer() && document[default_sorting_field].get() > std::numeric_limits::max()) { return Option<>(400, "Default sorting field `" + default_sorting_field + "` exceeds maximum value of an int32."); } - if(document[default_sorting_field].is_number_float() && + if(search_schema.at(default_sorting_field).is_single_float() && document[default_sorting_field].get() > std::numeric_limits::max()) { return Option<>(400, "Default sorting field `" + default_sorting_field + "` exceeds maximum value of a float."); } diff --git a/test/collection_test.cpp b/test/collection_test.cpp index 3a8405af..b3af6125 100644 --- a/test/collection_test.cpp +++ b/test/collection_test.cpp @@ -1083,7 +1083,14 @@ TEST_F(CollectionTest, FilterOnNumericFields) { coll_array_fields = collectionManager.get_collection("coll_array_fields"); if(coll_array_fields == nullptr) { - coll_array_fields = collectionManager.create_collection("coll_array_fields", fields, "age").get(); + // ensure that default_sorting_field is a non-array numerical field + auto coll_op = collectionManager.create_collection("coll_array_fields", fields, "years"); + ASSERT_EQ(false, coll_op.ok()); + ASSERT_STREQ("Default sorting field `years` must be of type int32 or float.", coll_op.error().c_str()); + + // let's try again properly + coll_op = collectionManager.create_collection("coll_array_fields", fields, "age"); + coll_array_fields = coll_op.get(); } std::string json_line; diff --git a/test/float_documents.jsonl b/test/float_documents.jsonl index 9ae520f7..b6b04cd3 100644 --- a/test/float_documents.jsonl +++ b/test/float_documents.jsonl @@ -1,6 +1,6 @@ {"title": "Jeremy Howard", "score": 1.09, "average": 1.45} {"title": "Jeremy Howard", "score": -9.998, "average": -2.408 } -{"title": "Jeremy Howard", "score": 7.812, "average": 0.001 } +{"title": "Jeremy Howard", "score": 1582186644000, "average": 0.001 } {"title": "Jeremy Howard", "score": 0.0, "average": 11.533 } {"title": "Jeremy Howard", "score": -9.999, "average": -11.38 } {"title": "Jeremy Howard", "score": -9.999, "average": 300 }