From 15dd0626be1c62ba325fa1b2da9357fe3c0e2d05 Mon Sep 17 00:00:00 2001 From: Kishore Nallan Date: Wed, 6 Mar 2024 21:11:09 +0530 Subject: [PATCH] Fix override filter match regression. Caused by switch to new fuzzy search API. --- src/index.cpp | 4 +- test/collection_override_test.cpp | 68 +++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 2 deletions(-) diff --git a/src/index.cpp b/src/index.cpp index 6ef2fa70..4fce857d 100644 --- a/src/index.cpp +++ b/src/index.cpp @@ -2562,7 +2562,7 @@ bool Index::check_for_overrides(const token_ordering& token_order, const string& std::vector sort_fields; std::vector fq_fields; - fq_fields.emplace_back(field_name, field_it.value().faceted_name(), 1, 0, true, enable_t::off); + fq_fields.emplace_back(field_name, field_it.value().faceted_name(), 1, 0, false, enable_t::off); uint32_t* filter_ids = nullptr; filter_result_iterator_t filter_result_it(filter_ids, 0); @@ -2574,7 +2574,7 @@ bool Index::check_for_overrides(const token_ordering& token_order, const string& fq_fields, window_tokens, {}, text_match_type_t::max_score, nullptr, 0, &filter_result_it, {}, {}, sort_fields, {0}, searched_queries, qtoken_set, topster, groups_processed, result_ids, result_ids_len, - 0, group_by_fields, false, true, false, false, query_hashes, MAX_SCORE, {true}, 1, + 0, group_by_fields, false, true, false, false, query_hashes, MAX_SCORE, {false}, 1, false, 4, 3, 7, 0, nullptr, field_values, geopoint_indices, "", true); if(!fuzzy_search_fields_op.ok()) { diff --git a/test/collection_override_test.cpp b/test/collection_override_test.cpp index 9f5d1aaf..51dfe1f2 100644 --- a/test/collection_override_test.cpp +++ b/test/collection_override_test.cpp @@ -1943,6 +1943,74 @@ TEST_F(CollectionOverrideTest, DynamicFilteringExactMatchBasics) { collectionManager.drop_collection("coll1"); } +TEST_F(CollectionOverrideTest, DynamicFilteringPrefixMatchShouldNotWork) { + Collection *coll1; + + std::vector fields = {field("name", field_types::STRING, false), + field("category", field_types::STRING, true), + field("brand", 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"] = "shoe"; + doc1["brand"] = "Nike"; + doc1["points"] = 3; + + nlohmann::json doc2; + doc2["id"] = "1"; + doc2["name"] = "Track Gym"; + doc2["category"] = "shoes"; + doc2["brand"] = "Adidas"; + doc2["points"] = 5; + + nlohmann::json doc3; + doc3["id"] = "2"; + doc3["name"] = "Running Shoe"; + doc3["category"] = "shoes"; + doc3["brand"] = "Nike"; + doc3["points"] = 5; + + ASSERT_TRUE(coll1->add(doc1.dump()).ok()); + ASSERT_TRUE(coll1->add(doc2.dump()).ok()); + ASSERT_TRUE(coll1->add(doc3.dump()).ok()); + + std::vector sort_fields = { sort_by("_text_match", "DESC"), sort_by("points", "DESC") }; + + // with override, results will be different + + nlohmann::json override_json = { + {"id", "dynamic-cat-filter"}, + { + "rule", { + {"query", "{category}"}, + {"match", override_t::MATCH_EXACT} + } + }, + {"remove_matched_tokens", true}, + {"filter_by", "category: {category}"} + }; + + 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("shoe", {"name", "category", "brand"}, "", + {}, sort_fields, {2, 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, DynamicFilteringMissingField) { Collection *coll1;