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
C++ algoliadatastoreelasticsearchenterprise-searchfacetingfull-text-searchfuzzy-searchgeosearchin-memoryinstantsearchmerchandisingpineconesearchsearch-enginesemantic-searchsimilarity-searchsite-searchsynonymstypo-tolerancevector-search
Ensure that the token ranking field is an unsigned int.
Kishore Nallan38fbbea71f65
diff --git a/src/collection.cpp b/src/collection.cppindex a92320a4..496b6559 100644--- a/src/collection.cpp+++ b/src/collection.cpp@@ -84,14 +84,18 @@ Option<uint32_t> Collection::index_in_memory(const nlohmann::json &document, uin"but is not found in the document.");}- if(!token_ranking_field.empty() && !document[token_ranking_field].is_number()) {- return Option<>(400, "Token ranking field `" + token_ranking_field + "` must be a number.");+ if(!token_ranking_field.empty() && !document[token_ranking_field].is_number_unsigned()) {+ return Option<>(400, "Token ranking field `" + token_ranking_field + "` must be an unsigned INT32.");}if(!token_ranking_field.empty() && document[token_ranking_field].get<int64_t>() > INT32_MAX) {return Option<>(400, "Token ranking field `" + token_ranking_field + "` exceeds maximum value of INT32.");}+ if(!token_ranking_field.empty() && document[token_ranking_field].get<int64_t>() < 0) {+ return Option<>(400, "Token ranking field `" + token_ranking_field + "` must not be negative.");+ }+uint32_t points = 0;if(!token_ranking_field.empty()) {points = document[token_ranking_field];@@ -369,7 +373,7 @@ void Collection::index_int64_array_field(const std::vector<int64_t> & values, co}}-void Collection::index_float_array_field(const std::vector<float> & values, const float score, art_tree *t,+void Collection::index_float_array_field(const std::vector<float> & values, const uint32_t score, art_tree *t,uint32_t seq_id) const {for(const float value: values) {index_float_field(value, score, t, seq_id);diff --git a/test/collection_test.cpp b/test/collection_test.cppindex 023ea17d..15a04b81 100644--- a/test/collection_test.cpp+++ b/test/collection_test.cpp@@ -1133,7 +1133,7 @@ TEST_F(CollectionTest, IndexingWithBadData) {doc_str = "{\"name\": \"foo\", \"age\": \"34\", \"tags\": [], \"average\": 34 }";const Option<std::string> & bad_token_ranking_field_op1 = sample_collection->add(doc_str);ASSERT_FALSE(bad_token_ranking_field_op1.ok());- ASSERT_STREQ("Token ranking field `age` must be an INT32.", bad_token_ranking_field_op1.error().c_str());+ ASSERT_STREQ("Token ranking field `age` must be an unsigned INT32.", bad_token_ranking_field_op1.error().c_str());doc_str = "{\"name\": \"foo\", \"age\": 343234324234233234, \"tags\": [], \"average\": 34 }";const Option<std::string> & bad_token_ranking_field_op2 = sample_collection->add(doc_str);@@ -1151,6 +1151,11 @@ TEST_F(CollectionTest, IndexingWithBadData) {ASSERT_FALSE(bad_rank_field_op.ok());ASSERT_STREQ("Sort field `average` must be a number.", bad_rank_field_op.error().c_str());+ doc_str = "{\"name\": \"foo\", \"age\": -10, \"tags\": [], \"average\": 34 }";+ const Option<std::string> & bad_token_ranking_field_op4 = sample_collection->add(doc_str);+ ASSERT_FALSE(bad_token_ranking_field_op4.ok());+ ASSERT_STREQ("Token ranking field `age` must be an unsigned INT32.", bad_token_ranking_field_op4.error().c_str());+collectionManager.drop_collection("sample_collection");}