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
Support wildcard tag rule.
Kishore Nallane961c53d7cdc
diff --git a/include/collection.h b/include/collection.hindex 4aa0b120..bc4b87ad 100644--- a/include/collection.h+++ b/include/collection.h@@ -188,6 +188,7 @@ private:string& actual_query, const string& filter_query,bool already_segmented,const bool tags_matched,+ const bool wildcard_tag_matched,const std::map<size_t, std::vector<std::string>>& pinned_hits,const std::vector<std::string>& hidden_hits,std::vector<std::pair<uint32_t, uint32_t>>& included_ids,diff --git a/src/collection.cpp b/src/collection.cppindex 7ce55121..f3ba4796 100644--- a/src/collection.cpp+++ b/src/collection.cpp@@ -815,6 +815,7 @@ bool Collection::does_override_match(const override_t& override, std::string& qustring& actual_query, const string& filter_query,bool already_segmented,const bool tags_matched,+ const bool wildcard_tag_matched,const std::map<size_t, std::vector<std::string>>& pinned_hits,const std::vector<std::string>& hidden_hits,std::vector<std::pair<uint32_t, uint32_t>>& included_ids,@@ -824,7 +825,7 @@ bool Collection::does_override_match(const override_t& override, std::string& qustd::string& curated_sort_by,nlohmann::json& override_metadata) const {- if(!tags_matched && !override.rule.tags.empty()) {+ if(!wildcard_tag_matched && !tags_matched && !override.rule.tags.empty()) {// only untagged overrides must be considered when no tags are given in the queryreturn false;}@@ -843,7 +844,7 @@ bool Collection::does_override_match(const override_t& override, std::string& qufilter_overrides.push_back(&override);}- if(tags_matched && override.rule.query.empty() && override.rule.filter_by.empty()) {+ if((wildcard_tag_matched || tags_matched) && override.rule.query.empty() && override.rule.filter_by.empty()) {// allowed} else {bool filter_by_match = (override.rule.query.empty() && override.rule.match.empty() &&@@ -962,7 +963,7 @@ void Collection::curate_results(string& actual_query, const string& filter_queryif(override.rule.tags == tags) {bool match_found = does_override_match(override, query, excluded_set, actual_query,- filter_query, already_segmented, true,+ filter_query, already_segmented, true, false,pinned_hits, hidden_hits, included_ids,excluded_ids, filter_overrides, filter_curated_hits,curated_sort_by, override_metadata);@@ -1009,7 +1010,7 @@ void Collection::curate_results(string& actual_query, const string& filter_query}bool match_found = does_override_match(override, query, excluded_set, actual_query,- filter_query, already_segmented, true,+ filter_query, already_segmented, true, false,pinned_hits, hidden_hits, included_ids,excluded_ids, filter_overrides, filter_curated_hits,curated_sort_by, override_metadata);@@ -1027,8 +1028,10 @@ void Collection::curate_results(string& actual_query, const string& filter_query// no override tags givenfor(const auto& override_kv: overrides) {const auto& override = override_kv.second;+ bool wildcard_tag = override.rule.tags.size() == 1 && *override.rule.tags.begin() == "*";bool match_found = does_override_match(override, query, excluded_set, actual_query, filter_query,- already_segmented, false, pinned_hits, hidden_hits, included_ids,+ already_segmented, false, wildcard_tag,+ pinned_hits, hidden_hits, included_ids,excluded_ids, filter_overrides, filter_curated_hits,curated_sort_by, override_metadata);if(match_found && override.stop_processing) {diff --git a/src/index.cpp b/src/index.cppindex 9884ec49..74b35cac 100644--- a/src/index.cpp+++ b/src/index.cpp@@ -2013,7 +2013,9 @@ bool Index::static_filter_query_eval(const override_t* override,bool tag_matched = (!override->rule.tags.empty() && override->rule.filter_by.empty() &&override->rule.query.empty());- if (tag_matched ||+ bool wildcard_tag_matched = (override->rule.tags.size() == 1 && *override->rule.tags.begin() == "*");++ if (tag_matched || wildcard_tag_matched ||(override->rule.match == override_t::MATCH_EXACT && override->rule.normalized_query == query) ||(override->rule.match == override_t::MATCH_CONTAINS &&StringUtils::contains_word(query, override->rule.normalized_query))) {diff --git a/src/override.cpp b/src/override.cppindex bd8a70be..a2f2be5d 100644--- a/src/override.cpp+++ b/src/override.cpp@@ -17,7 +17,7 @@ Option<bool> override_t::parse(const nlohmann::json& override_json, const std::sif (override_json["rule"].count("filter_by") == 0 && override_json["rule"].count("tags") == 0 &&(override_json["rule"].count("query") == 0 || override_json["rule"].count("match") == 0)) {- return Option<bool>(400, "The `rule` definition must contain a `query` and `match`.");+ return Option<bool>(400, "The `rule` definition must contain either a `tags` or a `query` and `match`.");}if(override_json.count("includes") == 0 && override_json.count("excludes") == 0 &&diff --git a/test/collection_override_test.cpp b/test/collection_override_test.cppindex dd254699..8a97bb18 100644--- a/test/collection_override_test.cpp+++ b/test/collection_override_test.cpp@@ -3641,6 +3641,123 @@ TEST_F(CollectionOverrideTest, OverrideWithTagsWithoutStopProcessing) {collectionManager.drop_collection("coll1");}+TEST_F(CollectionOverrideTest, WildcardTagRuleThatMatchesAllQueries) {+ Collection* coll1;++ std::vector<field> fields = {field("name", field_types::STRING, false),+ field("category", field_types::STRING, true),};++ coll1 = collectionManager.get_collection("coll1").get();+ if (coll1 == nullptr) {+ coll1 = collectionManager.create_collection("coll1", 1, fields, "").get();+ }++ nlohmann::json doc1;+ doc1["id"] = "0";+ doc1["name"] = "queryA";+ doc1["category"] = "kids";++ nlohmann::json doc2;+ doc2["id"] = "1";+ doc2["name"] = "queryA";+ doc2["category"] = "kitchen";++ nlohmann::json doc3;+ doc3["id"] = "2";+ doc3["name"] = "Clay Toy";+ doc3["category"] = "home";++ ASSERT_TRUE(coll1->add(doc1.dump()).ok());+ ASSERT_TRUE(coll1->add(doc2.dump()).ok());+ ASSERT_TRUE(coll1->add(doc3.dump()).ok());++ std::vector<sort_by> sort_fields = {sort_by("_text_match", "DESC")};++ nlohmann::json override_json1 = R"({+ "id": "ov-1",+ "rule": {++ },+ "filter_by": "category: kids"+ })"_json;++ override_t override1;+ auto op = override_t::parse(override_json1, "ov-1", override1);+ ASSERT_FALSE(op.ok());+ ASSERT_EQ("The `rule` definition must contain either a `tags` or a `query` and `match`.", op.error());++ override_json1 = R"({+ "id": "ov-1",+ "rule": {+ "tags": ["*"]+ },+ "filter_by": "category: kids"+ })"_json;++ op = override_t::parse(override_json1, "ov-1", override1);+ ASSERT_TRUE(op.ok());+ coll1->add_override(override1);++ // should match all search queries, even without passing any tags+ std::string override_tags = "";+ auto results = coll1->search("queryB", {"name"}, "",+ {}, sort_fields, {2}, 10, 1, FREQUENCY,+ {false}, Index::DROP_TOKENS_THRESHOLD,+ spp::sparse_hash_set<std::string>(),+ spp::sparse_hash_set<std::string>(), 10, "", 30, 4, "title", 20, {}, {}, {}, 0,+ "<mark>", "</mark>", {}, 1000, true, false, true, "", false, 10000,+ 4, 7, fallback, 4, {off}, 100, 100, 2, 2, false, "", true, 0, max_score, 100, 0,+ 0, HASH, 30000, 2, "", {}, {}, "right_to_left",+ true, true, false, -1, "", override_tags).get();++ ASSERT_EQ(1, results["hits"].size());+ ASSERT_EQ("0", results["hits"][0]["document"]["id"].get<std::string>());++ results = coll1->search("queryA", {"name"}, "",+ {}, sort_fields, {2}, 10, 1, FREQUENCY,+ {false}, Index::DROP_TOKENS_THRESHOLD,+ spp::sparse_hash_set<std::string>(),+ spp::sparse_hash_set<std::string>(), 10, "", 30, 4, "title", 20, {}, {}, {}, 0,+ "<mark>", "</mark>", {}, 1000, true, false, true, "", false, 10000,+ 4, 7, fallback, 4, {off}, 100, 100, 2, 2, false, "", true, 0, max_score, 100, 0,+ 0, HASH, 30000, 2, "", {}, {}, "right_to_left",+ true, true, false, -1, "", override_tags).get();++ ASSERT_EQ(1, results["hits"].size());+ ASSERT_EQ("0", results["hits"][0]["document"]["id"].get<std::string>());++ // includes instead of filter_by+ coll1->remove_override("ov-1");+ override_json1 = R"({+ "id": "ov-1",+ "rule": {+ "tags": ["*"]+ },+ "includes": [+ {"id": "1", "position": 1}+ ]+ })"_json;++ op = override_t::parse(override_json1, "ov-1", override1);+ ASSERT_TRUE(op.ok());+ coll1->add_override(override1);++ results = coll1->search("foobar", {"name"}, "",+ {}, sort_fields, {2}, 10, 1, FREQUENCY,+ {false}, Index::DROP_TOKENS_THRESHOLD,+ spp::sparse_hash_set<std::string>(),+ spp::sparse_hash_set<std::string>(), 10, "", 30, 4, "title", 20, {}, {}, {}, 0,+ "<mark>", "</mark>", {}, 1000, true, false, true, "", false, 10000,+ 4, 7, fallback, 4, {off}, 100, 100, 2, 2, false, "", true, 0, max_score, 100, 0,+ 0, HASH, 30000, 2, "", {}, {}, "right_to_left",+ true, true, false, -1, "", override_tags).get();++ ASSERT_EQ(1, results["hits"].size());+ ASSERT_EQ("1", results["hits"][0]["document"]["id"].get<std::string>());++ collectionManager.drop_collection("coll1");+}+TEST_F(CollectionOverrideTest, TagsOnlyRule) {Collection* coll1;