From 5f4d43f877a1aee5320a80cf3c1a885c103ddf76 Mon Sep 17 00:00:00 2001 From: Kishore Nallan Date: Sat, 18 Feb 2023 14:28:43 +0530 Subject: [PATCH 1/2] The `id` cannot be a default sorting field. --- include/field.h | 2 +- src/collection_manager.cpp | 4 ++++ test/collection_sorting_test.cpp | 17 +++++++++++++++++ 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/include/field.h b/include/field.h index 35813c95..c8ba94b0 100644 --- a/include/field.h +++ b/include/field.h @@ -355,7 +355,7 @@ struct field { } } - if(!default_sorting_field.empty() && !found_default_sorting_field && !fields.empty()) { + if(!default_sorting_field.empty() && !found_default_sorting_field) { return Option(400, "Default sorting field is defined as `" + default_sorting_field + "` but is not found in the schema."); } diff --git a/src/collection_manager.cpp b/src/collection_manager.cpp index 126d4824..ec95d1db 100644 --- a/src/collection_manager.cpp +++ b/src/collection_manager.cpp @@ -1130,6 +1130,10 @@ Option CollectionManager::create_collection(nlohmann::json& req_jso const std::string& default_sorting_field = req_json[DEFAULT_SORTING_FIELD].get(); + if(default_sorting_field == "id") { + return Option(400, "Invalid `default_sorting_field` value: cannot be `id`."); + } + std::string fallback_field_type; std::vector fields; auto parse_op = field::json_fields_to_fields(req_json[ENABLE_NESTED_FIELDS].get(), diff --git a/test/collection_sorting_test.cpp b/test/collection_sorting_test.cpp index 8bd805a9..121a0e64 100644 --- a/test/collection_sorting_test.cpp +++ b/test/collection_sorting_test.cpp @@ -2147,6 +2147,23 @@ TEST_F(CollectionSortingTest, OptionalFilteringViaSortingSearch) { collectionManager.drop_collection("coll1"); } +TEST_F(CollectionSortingTest, DisallowIdAsDefaultSortingField) { + std::string coll_schema = R"( + { + "name": "coll1", + "default_sorting_field": "id", + "fields": [ + {"name": "id", "type": "string" } + ] + } + )"; + + nlohmann::json schema = nlohmann::json::parse(coll_schema); + auto coll_op = collectionManager.create_collection(schema); + ASSERT_FALSE(coll_op.ok()); + ASSERT_EQ("Invalid `default_sorting_field` value: cannot be `id`.", coll_op.error()); +} + TEST_F(CollectionSortingTest, OptionalFilteringViaSortingSecondThirdParams) { std::string coll_schema = R"( { From e80b17085a27321b77fb02c02e8e9de79c394539 Mon Sep 17 00:00:00 2001 From: Kishore Nallan Date: Mon, 20 Feb 2023 18:10:22 +0530 Subject: [PATCH 2/2] Allow sequence id based sorting. --- src/collection.cpp | 3 ++- test/collection_sorting_test.cpp | 26 ++++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/collection.cpp b/src/collection.cpp index db5f9046..35a7a697 100644 --- a/src/collection.cpp +++ b/src/collection.cpp @@ -722,7 +722,8 @@ Option Collection::validate_and_standardize_sort_fields(const std::vector< } } - if (sort_field_std.name != sort_field_const::text_match && sort_field_std.name != sort_field_const::eval) { + if (sort_field_std.name != sort_field_const::text_match && sort_field_std.name != sort_field_const::eval && + sort_field_std.name != sort_field_const::seq_id) { const auto field_it = search_schema.find(sort_field_std.name); if(field_it == search_schema.end() || !field_it.value().sort || !field_it.value().index) { std::string error = "Could not find a field named `" + sort_field_std.name + diff --git a/test/collection_sorting_test.cpp b/test/collection_sorting_test.cpp index 121a0e64..99cb95c7 100644 --- a/test/collection_sorting_test.cpp +++ b/test/collection_sorting_test.cpp @@ -1952,6 +1952,32 @@ TEST_F(CollectionSortingTest, DisallowSortingOnNonIndexedIntegerField) { collectionManager.drop_collection("coll1"); } +TEST_F(CollectionSortingTest, WildcardSearchSequenceIdSort) { + nlohmann::json schema = R"({ + "name": "coll1", + "fields": [ + {"name": "category", "type": "string"} + ] + })"_json; + + Collection* coll1 = collectionManager.create_collection(schema).get(); + + nlohmann::json doc; + doc["category"] = "Shoes"; + + for(size_t i = 0; i < 30; i++) { + ASSERT_TRUE(coll1->add(doc.dump()).ok()); + } + + std::vector sort_fields = { + sort_by("_seq_id", "DESC"), + }; + + auto res = coll1->search("*", {"category"}, "", {}, sort_fields, {2}, 10, 1, FREQUENCY, {true}, 0).get(); + ASSERT_EQ(10, res["hits"].size()); + ASSERT_EQ(30, res["found"].get()); +} + TEST_F(CollectionSortingTest, OptionalFilteringViaSortingWildcard) { std::string coll_schema = R"( {