Move tags inside rule.

This commit is contained in:
Kishore Nallan 2023-11-08 13:07:36 +05:30
parent ec7c54d31c
commit a7180d2b45
4 changed files with 42 additions and 44 deletions

View File

@ -40,8 +40,6 @@ 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;

View File

@ -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<std::string> 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<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()) {
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<uint32_t> 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<uint32_t> 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);
}

View File

@ -28,6 +28,20 @@ Option<bool> 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<bool>(400, "The `tags` value must be an array of strings.");
}
for(const auto& tag: override_json["rule"]["tags"]) {
if(!tag.is_string()) {
return Option<bool>(400, "The `tags` value must be an array of strings.");
}
override.rule.tags.insert(tag.get<std::string>());
}
}
if(override_json.count("includes") != 0) {
if(!override_json["includes"].is_array()) {
return Option<bool>(400, "The `includes` value must be an array.");
@ -101,20 +115,6 @@ 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) {
@ -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;
}

View File

@ -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;