From 0591fd508cb2406b17aea67d33481704e8812759 Mon Sep 17 00:00:00 2001 From: Kishore Nallan Date: Wed, 13 Oct 2021 11:14:58 +0530 Subject: [PATCH] Handle missing field when dynamic filtering is used. --- src/index.cpp | 8 +++++- test/collection_override_test.cpp | 48 +++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 1 deletion(-) diff --git a/src/index.cpp b/src/index.cpp index cd394aa9..fc6789d0 100644 --- a/src/index.cpp +++ b/src/index.cpp @@ -2658,7 +2658,13 @@ void Index::search_field(const uint8_t & field_id, // NOTE: `query_tokens` preserve original tokens, while `search_tokens` could be a result of dropped tokens size_t max_cost = (num_typos < 0 || num_typos > 2) ? 2 : num_typos; - auto& the_field = search_schema.at(field); + auto field_it = search_schema.find(field); + + if(field_it == search_schema.end()) { + return; + } + + auto& the_field = field_it->second; if(the_field.locale != "" && the_field.locale != "en") { // disable fuzzy trie traversal for non-english locales diff --git a/test/collection_override_test.cpp b/test/collection_override_test.cpp index 70bc8b3a..c211aaf1 100644 --- a/test/collection_override_test.cpp +++ b/test/collection_override_test.cpp @@ -937,6 +937,54 @@ TEST_F(CollectionOverrideTest, DynamicFilteringExactMatchBasics) { collectionManager.drop_collection("coll1"); } +TEST_F(CollectionOverrideTest, DynamicFilteringMissingField) { + Collection *coll1; + + std::vector fields = {field("name", field_types::STRING, false), + field("category", field_types::STRING, true), + field("points", field_types::INT32, false)}; + + coll1 = collectionManager.get_collection("coll1").get(); + if(coll1 == nullptr) { + coll1 = collectionManager.create_collection("coll1", 1, fields, "points").get(); + } + + nlohmann::json doc1; + doc1["id"] = "0"; + doc1["name"] = "Amazing Shoes"; + doc1["category"] = "shoes"; + doc1["points"] = 3; + + ASSERT_TRUE(coll1->add(doc1.dump()).ok()); + + std::vector sort_fields = { sort_by("_text_match", "DESC"), sort_by("points", "DESC") }; + + nlohmann::json override_json = { + {"id", "dynamic-cat-filter"}, + { + "rule", { + {"query", "{categories}"}, // this field does NOT exist + {"match", override_t::MATCH_EXACT} + } + }, + {"remove_matched_tokens", true}, + {"filter_by", "category: {categories}"} + }; + + override_t override; + auto op = override_t::parse(override_json, "dynamic-cat-filter", override); + ASSERT_TRUE(op.ok()); + coll1->add_override(override); + + auto results = coll1->search("shoes", {"name", "category"}, "", + {}, sort_fields, {2, 2}, 10).get(); + + ASSERT_EQ(1, results["hits"].size()); + ASSERT_EQ("0", results["hits"][0]["document"]["id"].get()); + + collectionManager.drop_collection("coll1"); +} + TEST_F(CollectionOverrideTest, DynamicFilteringMultiplePlaceholders) { Collection* coll1;