diff --git a/include/override.h b/include/override.h index 9a6cf607..0496bc4b 100644 --- a/include/override.h +++ b/include/override.h @@ -40,8 +40,6 @@ struct override_t { std::string sort_by; std::string replace_query; - std::set 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 47999e06..817faf24 100644 --- a/src/collection.cpp +++ b/src/collection.cpp @@ -865,7 +865,7 @@ void Collection::curate_results(string& actual_query, const string& filter_query const auto& override = override_it->second; - if(override.tags == tags) { + if(override.rule.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, @@ -905,7 +905,7 @@ void Collection::curate_results(string& actual_query, const string& filter_query const auto& override = override_it->second; std::set matching_tags; - std::set_intersection(override.tags.begin(), override.tags.end(), + std::set_intersection(override.rule.tags.begin(), override.rule.tags.end(), tags.begin(), tags.end(), std::inserter(matching_tags, matching_tags.begin())); @@ -4121,9 +4121,9 @@ Option 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()) { + if(overrides.count(override.id) != 0 && !overrides[override.id].rule.tags.empty()) { // remove existing tags - for(auto& tag: overrides[override.id].tags) { + for(auto& tag: overrides[override.id].rule.tags) { if(override_tags.count(tag) != 0) { override_tags[tag].erase(override.id); } @@ -4131,7 +4131,7 @@ Option Collection::add_override(const override_t & override, bool writ } overrides[override.id] = override; - for(const auto& tag: override.tags) { + for(const auto& tag: override.rule.tags) { override_tags[tag].insert(override.id); } @@ -4146,7 +4146,7 @@ Option Collection::remove_override(const std::string & id) { } std::unique_lock lock(mutex); - for(const auto& tag: overrides[id].tags) { + for(const auto& tag: overrides[id].rule.tags) { if(override_tags.count(tag) != 0) { override_tags[tag].erase(id); } diff --git a/src/override.cpp b/src/override.cpp index 12e3820f..d8934fab 100644 --- a/src/override.cpp +++ b/src/override.cpp @@ -28,6 +28,20 @@ Option override_t::parse(const nlohmann::json& override_json, const std::s "`filter_by`, `sort_by`, `remove_matched_tokens`, `replace_query`."); } + if(override_json["rule"].count("tags") != 0) { + if(!override_json["rule"]["tags"].is_array()) { + return Option(400, "The `tags` value must be an array of strings."); + } + + for(const auto& tag: override_json["rule"]["tags"]) { + if(!tag.is_string()) { + return Option(400, "The `tags` value must be an array of strings."); + } + + override.rule.tags.insert(tag.get()); + } + } + if(override_json.count("includes") != 0) { if(!override_json["includes"].is_array()) { return Option(400, "The `includes` value must be an array."); @@ -101,20 +115,6 @@ Option 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(400, "The `tags` value must be an array of strings."); - } - - for(const auto& tag: override_json["tags"]) { - if(!tag.is_string()) { - return Option(400, "The `tags` value must be an array of strings."); - } - - override.tags.insert(tag.get()); - } - } - if(!id.empty()) { override.id = id; } else if(override_json.count("id") != 0) { @@ -238,6 +238,10 @@ nlohmann::json override_t::to_json() const { override["rule"]["filter_by"] = rule.filter_by; } + if(!rule.tags.empty()) { + override["rule"]["tags"] = rule.tags; + } + override["includes"] = nlohmann::json::array(); for(const auto & add_hit: add_hits) { @@ -266,10 +270,6 @@ 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 a9adeb28..73c3ec98 100644 --- a/test/collection_override_test.cpp +++ b/test/collection_override_test.cpp @@ -3339,10 +3339,10 @@ TEST_F(CollectionOverrideTest, OverrideWithTags) { "id": "ov-1", "rule": { "query": "queryA", - "match": "exact" + "match": "exact", + "tags": ["alpha", "beta"] }, - "filter_by": "category: kids", - "tags": ["alpha", "beta"] + "filter_by": "category: kids" })"_json; override_t override1; @@ -3355,10 +3355,10 @@ TEST_F(CollectionOverrideTest, OverrideWithTags) { "id": "ov-2", "rule": { "query": "queryA", - "match": "exact" + "match": "exact", + "tags": ["alpha"] }, - "filter_by": "category: kitchen", - "tags": ["alpha"] + "filter_by": "category: kitchen" })"_json; override_t override2; @@ -3475,10 +3475,10 @@ TEST_F(CollectionOverrideTest, OverrideWithTagsPartialMatch) { "id": "ov-1", "rule": { "query": "queryA", - "match": "exact" + "match": "exact", + "tags": ["alpha", "beta"] }, - "filter_by": "category: kids", - "tags": ["alpha", "beta"] + "filter_by": "category: kids" })"_json; override_t override1; @@ -3491,10 +3491,10 @@ TEST_F(CollectionOverrideTest, OverrideWithTagsPartialMatch) { "id": "ov-2", "rule": { "query": "queryB", - "match": "exact" + "match": "exact", + "tags": ["alpha"] }, - "filter_by": "category: kitchen", - "tags": ["alpha"] + "filter_by": "category: kitchen" })"_json; override_t override2; @@ -3554,12 +3554,12 @@ TEST_F(CollectionOverrideTest, OverrideWithTagsWithoutStopProcessing) { "id": "ov-1", "rule": { "query": "queryA", - "match": "exact" + "match": "exact", + "tags": ["alpha", "beta"] }, "stop_processing": false, "remove_matched_tokens": false, - "filter_by": "category: kids", - "tags": ["alpha", "beta"] + "filter_by": "category: kids" })"_json; override_t override1; @@ -3572,12 +3572,12 @@ TEST_F(CollectionOverrideTest, OverrideWithTagsWithoutStopProcessing) { "id": "ov-2", "rule": { "query": "queryA", - "match": "exact" + "match": "exact", + "tags": ["alpha"] }, "stop_processing": false, "remove_matched_tokens": false, - "filter_by": "category: kitchen", - "tags": ["alpha"] + "filter_by": "category: kitchen" })"_json; override_t override2;