Skip to content

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

Override tagging.

Kishore Nallan87e0e1773cb5
diff --git a/include/collection.h b/include/collection.h
index 2ad4742c..d8894eb3 100644
--- a/include/collection.h
+++ b/include/collection.h
@@ -116,6 +116,9 @@ private:
std::map<std::string, override_t> overrides;
+ // maps tag name => override_ids
+ std::map<std::string, std::set<std::string>> override_tags;
+
std::string default_sorting_field;
const float max_memory_ratio;
@@ -175,7 +178,21 @@ private:
void process_remove_field_for_embedding_fields(const field& del_field, std::vector<field>& garbage_embed_fields);
+ bool does_override_match(const override_t& override, std::string& query,
+ std::set<uint32_t>& excluded_set,
+ string& actual_query, const string& filter_query,
+ bool already_segmented,
+ const std::set<std::string>& tags,
+ 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,
+ std::vector<uint32_t>& excluded_ids,
+ std::vector<const override_t*>& filter_overrides,
+ bool& filter_curated_hits,
+ std::string& curated_sort_by) const;
+
void curate_results(string& actual_query, const string& filter_query, bool enable_overrides, bool already_segmented,
+ const std::set<std::string>& tags,
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,
@@ -526,7 +543,8 @@ public:
const std::vector<ref_include_fields>& ref_include_fields_vec = {},
const std::string& drop_tokens_mode = "right_to_left",
const bool prioritize_num_matching_fields = true,
- const bool group_missing_values = true) const;
+ const bool group_missing_values = true,
+ const std::string& override_tags_str = "") const;
Option<bool> get_filter_ids(const std::string & filter_query, filter_result_t& filter_result) const;
diff --git a/include/override.h b/include/override.h
index 2900f866..9a6cf607 100644
--- a/include/override.h
+++ b/include/override.h
@@ -13,6 +13,7 @@ struct override_t {
std::string match;
bool dynamic_query = false;
std::string filter_by;
+ std::set<std::string> tags;
};
struct add_hit_t {
@@ -39,6 +40,8 @@ struct override_t {
std::string sort_by;
std::string replace_query;
+ std::set<std::string> tags;
+
// epoch seconds
int64_t effective_from_ts = -1;
int64_t effective_to_ts = -1;
diff --git a/src/collection.cpp b/src/collection.cpp
index 24b3d331..bdea8cd7 100644
--- a/src/collection.cpp
+++ b/src/collection.cpp
@@ -726,8 +726,90 @@ size_t Collection::batch_index_in_memory(std::vector<index_record>& index_record
return num_indexed;
}
+bool Collection::does_override_match(const override_t& override, std::string& query,
+ std::set<uint32_t>& excluded_set,
+ string& actual_query, const string& filter_query,
+ bool already_segmented,
+ const std::set<std::string>& tags,
+ 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,
+ std::vector<uint32_t>& excluded_ids,
+ std::vector<const override_t*>& filter_overrides,
+ bool& filter_curated_hits,
+ std::string& curated_sort_by) const {
+
+ auto now_epoch = int64_t(std::time(0));
+ if(override.effective_from_ts != -1 && now_epoch < override.effective_from_ts) {
+ return false;
+ }
+
+ if(override.effective_to_ts != -1 && now_epoch > override.effective_to_ts) {
+ return false;
+ }
+
+ // ID-based overrides are applied first as they take precedence over filter-based overrides
+ if(!override.filter_by.empty()) {
+ filter_overrides.push_back(&override);
+ }
+
+ bool filter_by_match = (override.rule.query.empty() && override.rule.match.empty() &&
+ !override.rule.filter_by.empty() && override.rule.filter_by == filter_query);
+
+ bool query_match = (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));
+
+ if(!filter_by_match && !query_match) {
+ return false;
+ }
+
+ if(!override.rule.filter_by.empty() && override.rule.filter_by != filter_query) {
+ return false;
+ }
+
+ // have to ensure that dropped hits take precedence over added hits
+ for(const auto & hit: override.drop_hits) {
+ Option<uint32_t> seq_id_op = doc_id_to_seq_id(hit.doc_id);
+ if(seq_id_op.ok()) {
+ excluded_ids.push_back(seq_id_op.get());
+ excluded_set.insert(seq_id_op.get());
+ }
+ }
+
+ for(const auto & hit: override.add_hits) {
+ Option<uint32_t> seq_id_op = doc_id_to_seq_id(hit.doc_id);
+ if(!seq_id_op.ok()) {
+ continue;
+ }
+ uint32_t seq_id = seq_id_op.get();
+ bool excluded = (excluded_set.count(seq_id) != 0);
+ if(!excluded) {
+ included_ids.emplace_back(seq_id, hit.position);
+ }
+ }
+
+ if(!override.replace_query.empty()) {
+ actual_query = override.replace_query;
+ } else if(override.remove_matched_tokens && override.filter_by.empty()) {
+ // don't prematurely remove tokens from query because dynamic filtering will require them
+ StringUtils::replace_all(query, override.rule.normalized_query, "");
+ StringUtils::trim(query);
+ if(query.empty()) {
+ query = "*";
+ }
+
+ actual_query = query;
+ }
+
+ filter_curated_hits = override.filter_curated_hits;
+ curated_sort_by = override.sort_by;
+ return true;
+}
+
void Collection::curate_results(string& actual_query, const string& filter_query,
bool enable_overrides, bool already_segmented,
+ const std::set<std::string>& tags,
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,
@@ -751,7 +833,6 @@ void Collection::curate_results(string& actual_query, const string& filter_query
}
}
-
if(enable_overrides && !overrides.empty()) {
std::string query;
@@ -764,73 +845,93 @@ void Collection::curate_results(string& actual_query, const string& filter_query
query = StringUtils::join(tokens, " ");
}
- for(const auto& override_kv: overrides) {
- const auto& override = override_kv.second;
-
- auto now_epoch = int64_t(std::time(0));
- if(override.effective_from_ts != -1 && now_epoch < override.effective_from_ts) {
- continue;
- }
+ if(!tags.empty()) {
+ bool all_tags_found = false;
+ std::set<std::string> found_overrides;
+ if(tags.size() > 1) {
+ // check for AND match only when multiple tags are sent
+ const auto& tag = *tags.begin();
+ auto override_ids_it = override_tags.find(tag);
+ if (override_ids_it != override_tags.end()) {
+ const auto &override_ids = override_ids_it->second;
+ for(const auto& id: override_ids) {
+ auto override_it = overrides.find(id);
+ if(override_it == overrides.end()) {
+ continue;
+ }
- if(override.effective_to_ts != -1 && now_epoch > override.effective_to_ts) {
- continue;
+ const auto& override = override_it->second;
+
+ if(override.tags == tags) {
+ bool match_found = does_override_match(override, query, excluded_set, actual_query,
+ filter_query, already_segmented, tags,
+ pinned_hits, hidden_hits, included_ids,
+ excluded_ids, filter_overrides, filter_curated_hits,
+ curated_sort_by);
+
+ if(match_found) {
+ all_tags_found = true;
+ found_overrides.insert(id);
+ if(override.stop_processing) {
+ break;
+ }
+ }
+ }
+ }
+ }
}
- // ID-based overrides are applied first as they take precedence over filter-based overrides
- if(!override.filter_by.empty()) {
- filter_overrides.push_back(&override);
- }
+ if(!all_tags_found) {
+ // check for partial tag matches
+ for(const auto& tag: tags) {
+ auto override_ids_it = override_tags.find(tag);
+ if (override_ids_it == override_tags.end()) {
+ continue;
+ }
- bool filter_by_match = (override.rule.query.empty() && override.rule.match.empty() &&
- !override.rule.filter_by.empty() && override.rule.filter_by == filter_query);
+ const auto &override_ids = override_ids_it->second;
- bool query_match = (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));
+ for(const auto& id: override_ids) {
+ if(found_overrides.count(id) != 0) {
+ continue;
+ }
+ auto override_it = overrides.find(id);
+ if (override_it == overrides.end()) {
+ continue;
+ }
- if (filter_by_match || query_match) {
- if(!override.rule.filter_by.empty() && override.rule.filter_by != filter_query) {
- continue;
- }
+ const auto& override = override_it->second;
+ std::set<std::string> matching_tags;
+ std::set_intersection(override.tags.begin(), override.tags.end(),
+ tags.begin(), tags.end(),
+ std::inserter(matching_tags, matching_tags.begin()));
- // have to ensure that dropped hits take precedence over added hits
- for(const auto & hit: override.drop_hits) {
- Option<uint32_t> seq_id_op = doc_id_to_seq_id(hit.doc_id);
- if(seq_id_op.ok()) {
- excluded_ids.push_back(seq_id_op.get());
- excluded_set.insert(seq_id_op.get());
- }
- }
+ if(matching_tags.empty()) {
+ continue;
+ }
- for(const auto & hit: override.add_hits) {
- Option<uint32_t> seq_id_op = doc_id_to_seq_id(hit.doc_id);
- if(!seq_id_op.ok()) {
- continue;
- }
- uint32_t seq_id = seq_id_op.get();
- bool excluded = (excluded_set.count(seq_id) != 0);
- if(!excluded) {
- included_ids.emplace_back(seq_id, hit.position);
- }
- }
+ bool match_found = does_override_match(override, query, excluded_set, actual_query,
+ filter_query, already_segmented, tags,
+ pinned_hits, hidden_hits, included_ids,
+ excluded_ids, filter_overrides, filter_curated_hits,
+ curated_sort_by);
- if(!override.replace_query.empty()) {
- actual_query = override.replace_query;
- } else if(override.remove_matched_tokens && override.filter_by.empty()) {
- // don't prematurely remove tokens from query because dynamic filtering will require them
- StringUtils::replace_all(query, override.rule.normalized_query, "");
- StringUtils::trim(query);
- if(query.empty()) {
- query = "*";
+ if(match_found) {
+ found_overrides.insert(id);
+ if(override.stop_processing) {
+ break;
+ }
+ }
}
-
- actual_query = query;
}
-
- filter_curated_hits = override.filter_curated_hits;
- curated_sort_by = override.sort_by;
-
- if(override.stop_processing) {
+ }
+ } else {
+ for(const auto& override_kv: overrides) {
+ const auto& override = override_kv.second;
+ bool match_found = does_override_match(override, query, excluded_set, actual_query, filter_query,
+ already_segmented, tags, pinned_hits, hidden_hits, included_ids,
+ excluded_ids, filter_overrides, filter_curated_hits, curated_sort_by);
+ if(match_found && override.stop_processing) {
break;
}
}
@@ -1410,7 +1511,8 @@ Option<nlohmann::json> Collection::search(std::string raw_query,
const std::vector<ref_include_fields>& ref_include_fields_vec,
const std::string& drop_tokens_mode,
const bool prioritize_num_matching_fields,
- const bool group_missing_values) const {
+ const bool group_missing_values,
+ const std::string& override_tags_str) const {
std::shared_lock lock(mutex);
@@ -1815,8 +1917,17 @@ Option<nlohmann::json> Collection::search(std::string raw_query,
std::vector<const override_t*> filter_overrides;
bool filter_curated_hits = false;
std::string curated_sort_by;
- curate_results(query, filter_query, enable_overrides, pre_segmented_query, pinned_hits, hidden_hits,
- included_ids, excluded_ids, filter_overrides, filter_curated_hits, curated_sort_by);
+ std::set<std::string> override_tag_set;
+
+ std::vector<std::string> override_tags_vec;
+ StringUtils::split(override_tags_str, override_tags_vec, ",");
+ for(const auto& tag: override_tags_vec) {
+ override_tag_set.insert(tag);
+ }
+
+ curate_results(query, filter_query, enable_overrides, pre_segmented_query, override_tag_set,
+ pinned_hits, hidden_hits, included_ids, excluded_ids, filter_overrides, filter_curated_hits,
+ curated_sort_by);
if(filter_curated_hits_option == 0 || filter_curated_hits_option == 1) {
// When query param has explicit value set, override level configuration takes lower precedence.
@@ -3879,7 +3990,21 @@ Option<uint32_t> Collection::add_override(const override_t & override, bool writ
}
std::unique_lock lock(mutex);
+
+ if(overrides.count(override.id) != 0 && !overrides[override.id].tags.empty()) {
+ // remove existing tags
+ for(auto& tag: overrides[override.id].tags) {
+ if(override_tags.count(tag) != 0) {
+ override_tags[tag].erase(override.id);
+ }
+ }
+ }
+
overrides[override.id] = override;
+ for(const auto& tag: override.tags) {
+ override_tags[tag].insert(override.id);
+ }
+
return Option<uint32_t>(200);
}
@@ -3891,7 +4016,14 @@ Option<uint32_t> Collection::remove_override(const std::string & id) {
}
std::unique_lock lock(mutex);
+ for(const auto& tag: overrides[id].tags) {
+ if(override_tags.count(tag) != 0) {
+ override_tags[tag].erase(id);
+ }
+ }
+
overrides.erase(id);
+
return Option<uint32_t>(200);
}
diff --git a/src/collection_manager.cpp b/src/collection_manager.cpp
index bf3baa70..053e8bd8 100644
--- a/src/collection_manager.cpp
+++ b/src/collection_manager.cpp
@@ -1117,6 +1117,7 @@ Option<bool> CollectionManager::do_search(std::map<std::string, std::string>& re
const char *DROP_TOKENS_MODE = "drop_tokens_mode";
const char *PRIORITIZE_NUM_MATCHING_FIELDS = "prioritize_num_matching_fields";
+ const char *OVERRIDE_TAGS = "override_tags";
// enrich params with values from embedded params
for(auto& item: embedded_params.items()) {
@@ -1241,6 +1242,7 @@ Option<bool> CollectionManager::do_search(std::map<std::string, std::string>& re
std::string drop_tokens_mode_str = "right_to_left";
bool prioritize_num_matching_fields = true;
+ std::string override_tags;
std::unordered_map<std::string, size_t*> unsigned_int_values = {
{MIN_LEN_1TYPO, &min_len_1typo},
@@ -1279,6 +1281,7 @@ Option<bool> CollectionManager::do_search(std::map<std::string, std::string>& re
{PINNED_HITS, &pinned_hits_str},
{HIDDEN_HITS, &hidden_hits_str},
{DROP_TOKENS_MODE, &drop_tokens_mode_str},
+ {OVERRIDE_TAGS, &override_tags},
};
std::unordered_map<std::string, bool*> bool_values = {
@@ -1493,7 +1496,8 @@ Option<bool> CollectionManager::do_search(std::map<std::string, std::string>& re
ref_include_fields_vec,
drop_tokens_mode_str,
prioritize_num_matching_fields,
- group_missing_values);
+ group_missing_values,
+ override_tags);
uint64_t timeMillis = std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::high_resolution_clock::now() - begin).count();
diff --git a/src/override.cpp b/src/override.cpp
index 572189d5..12e3820f 100644
--- a/src/override.cpp
+++ b/src/override.cpp
@@ -101,6 +101,20 @@ Option<bool> override_t::parse(const nlohmann::json& override_json, const std::s
}
}
+ if(override_json.count("tags") != 0) {
+ if(!override_json["tags"].is_array()) {
+ return Option<bool>(400, "The `tags` value must be an array of strings.");
+ }
+
+ for(const auto& tag: override_json["tags"]) {
+ if(!tag.is_string()) {
+ return Option<bool>(400, "The `tags` value must be an array of strings.");
+ }
+
+ override.tags.insert(tag.get<std::string>());
+ }
+ }
+
if(!id.empty()) {
override.id = id;
} else if(override_json.count("id") != 0) {
@@ -252,6 +266,10 @@ nlohmann::json override_t::to_json() const {
override["replace_query"] = replace_query;
}
+ if(!tags.empty()) {
+ override["tags"] = tags;
+ }
+
if(effective_from_ts != -1) {
override["effective_from_ts"] = effective_from_ts;
}
diff --git a/test/collection_override_test.cpp b/test/collection_override_test.cpp
index f5b368ec..175c33d0 100644
--- a/test/collection_override_test.cpp
+++ b/test/collection_override_test.cpp
@@ -3279,3 +3279,341 @@ TEST_F(CollectionOverrideTest, OverrideWithSymbolsToIndex) {
collectionManager.drop_collection("coll1");
}
+TEST_F(CollectionOverrideTest, OverrideWithTags) {
+ /*
+
+ If override1 is tagged tagA, tagB, override2 is tagged tagA, override3 is tagged with nothing:
+
+ Then if a search is tagged with tagA, we only consider overrides that contain tagA (override1 and override2)
+ with the usual logic - in alphabetic order of override name and then process both if stop rule processing is false.
+
+ If a search is tagged with tagA and tagB, we evaluate any rules that contain tagA and tagB first,
+ then tag A or tag B, but not overrides that contain no tags. Within each group, we evaluate in alphabetic order
+ and process multiple if stop rule processing is false
+
+ If a search has no tags, then we only consider rules that have no tags.
+ */
+
+ 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")};
+
+ auto results = coll1->search("Clay", {"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, 5,
+ "", 10).get();
+
+ ASSERT_EQ(1, results["hits"].size());
+
+ // create overrides containing 2 tags, single tag and no tags:
+ nlohmann::json override_json1 = R"({
+ "id": "ov-1",
+ "rule": {
+ "query": "queryA",
+ "match": "exact"
+ },
+ "filter_by": "category: kids",
+ "tags": ["alpha", "beta"]
+ })"_json;
+
+ override_t override1;
+ auto op = override_t::parse(override_json1, "ov-1", override1);
+ ASSERT_TRUE(op.ok());
+ coll1->add_override(override1);
+
+ //
+ nlohmann::json override_json2 = R"({
+ "id": "ov-2",
+ "rule": {
+ "query": "queryA",
+ "match": "exact"
+ },
+ "filter_by": "category: kitchen",
+ "tags": ["alpha"]
+ })"_json;
+
+ override_t override2;
+ override_t::parse(override_json2, "ov-2", override2);
+ ASSERT_TRUE(op.ok());
+ coll1->add_override(override2);
+
+ //
+ nlohmann::json override_json3 = R"({
+ "id": "ov-3",
+ "rule": {
+ "query": "queryA",
+ "match": "exact"
+ },
+ "filter_by": "category: home"
+ })"_json;
+
+ override_t override3;
+ op = override_t::parse(override_json3, "ov-3", override3);
+ ASSERT_TRUE(op.ok());
+ coll1->add_override(override3);
+
+ // when tag doesn't match any override, no results will be found
+ 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, "foo").get();
+
+ ASSERT_EQ(2, results["hits"].size());
+
+ // when multiple overrides match a given tag, return first matching record
+ 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, "alpha").get();
+
+ ASSERT_EQ(1, results["hits"].size());
+ ASSERT_EQ("0", results["hits"][0]["document"]["id"].get<std::string>());
+
+ // single tag matching rule with multiple tags
+ 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, "beta").get();
+
+ ASSERT_EQ(1, results["hits"].size());
+ ASSERT_EQ("0", results["hits"][0]["document"]["id"].get<std::string>());
+
+ // when multiple tags are passed, only consider rule with both tags
+ 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, "alpha,beta").get();
+
+ ASSERT_EQ(1, results["hits"].size());
+ ASSERT_EQ("0", results["hits"][0]["document"]["id"].get<std::string>());
+
+ collectionManager.drop_collection("coll1");
+}
+
+TEST_F(CollectionOverrideTest, OverrideWithTagsPartialMatch) {
+ 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": {
+ "query": "queryA",
+ "match": "exact"
+ },
+ "filter_by": "category: kids",
+ "tags": ["alpha", "beta"]
+ })"_json;
+
+ override_t override1;
+ auto op = override_t::parse(override_json1, "ov-1", override1);
+ ASSERT_TRUE(op.ok());
+ coll1->add_override(override1);
+
+ //
+ nlohmann::json override_json2 = R"({
+ "id": "ov-2",
+ "rule": {
+ "query": "queryB",
+ "match": "exact"
+ },
+ "filter_by": "category: kitchen",
+ "tags": ["alpha"]
+ })"_json;
+
+ override_t override2;
+ override_t::parse(override_json2, "ov-2", override2);
+ ASSERT_TRUE(op.ok());
+ coll1->add_override(override2);
+
+ // when only one of the two tags are found, apply that rule
+ 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, "alpha,zeta").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, OverrideWithTagsWithoutStopProcessing) {
+ Collection* coll1;
+
+ std::vector<field> fields = {field("name", field_types::STRING, false),
+ field("category", field_types::STRING_ARRAY, 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"] = {"kids", "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": {
+ "query": "queryA",
+ "match": "exact"
+ },
+ "stop_processing": false,
+ "remove_matched_tokens": false,
+ "filter_by": "category: kids",
+ "tags": ["alpha", "beta"]
+ })"_json;
+
+ override_t override1;
+ auto op = override_t::parse(override_json1, "ov-1", override1);
+ ASSERT_TRUE(op.ok());
+ coll1->add_override(override1);
+
+ //
+ nlohmann::json override_json2 = R"({
+ "id": "ov-2",
+ "rule": {
+ "query": "queryA",
+ "match": "exact"
+ },
+ "stop_processing": false,
+ "remove_matched_tokens": false,
+ "filter_by": "category: kitchen",
+ "tags": ["alpha"]
+ })"_json;
+
+ override_t override2;
+ override_t::parse(override_json2, "ov-2", override2);
+ ASSERT_TRUE(op.ok());
+ coll1->add_override(override2);
+
+ //
+ nlohmann::json override_json3 = R"({
+ "id": "ov-3",
+ "rule": {
+ "query": "queryA",
+ "match": "exact"
+ },
+ "stop_processing": false,
+ "remove_matched_tokens": false,
+ "filter_by": "category: home"
+ })"_json;
+
+ override_t override3;
+ op = override_t::parse(override_json3, "ov-3", override3);
+ ASSERT_TRUE(op.ok());
+ coll1->add_override(override3);
+
+ auto 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, "alpha").get();
+
+ ASSERT_EQ(1, results["hits"].size());
+ ASSERT_EQ("1", results["hits"][0]["document"]["id"].get<std::string>());
+
+ collectionManager.drop_collection("coll1");
+}