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

Allow override filter_by rule without query/match.

Kishore Nallancbd251aa2de4
diff --git a/src/collection.cpp b/src/collection.cpp
index 8c566db6..212e1e70 100644
--- a/src/collection.cpp
+++ b/src/collection.cpp
@@ -452,10 +452,14 @@ void Collection::curate_results(string& actual_query, const string& filter_query
filter_overrides.push_back(&override);
}
- if ((override.rule.match == override_t::MATCH_EXACT && override.rule.query == query) ||
- (override.rule.match == override_t::MATCH_CONTAINS &&
- StringUtils::contains_word(query, override.rule.query))) {
+ 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.query == query) ||
+ (override.rule.match == override_t::MATCH_CONTAINS &&
+ StringUtils::contains_word(query, override.rule.query));
+
+ if (filter_by_match || query_match) {
if(!override.rule.filter_by.empty() && override.rule.filter_by != filter_query) {
continue;
}
diff --git a/src/override.cpp b/src/override.cpp
index 1249f4a9..aba4f8d1 100644
--- a/src/override.cpp
+++ b/src/override.cpp
@@ -10,7 +10,8 @@ Option<bool> override_t::parse(const nlohmann::json& override_json, const std::s
return Option<bool>(400, "Missing `rule` definition.");
}
- if(override_json["rule"].count("query") == 0 || override_json["rule"].count("match") == 0) {
+ if (override_json["rule"].count("filter_by") == 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`.");
}
@@ -103,10 +104,11 @@ Option<bool> override_t::parse(const nlohmann::json& override_json, const std::s
return Option<bool>(400, "Override `id` not provided.");
}
- override.rule.query = override_json["rule"]["query"].get<std::string>();
- override.rule.match = override_json["rule"]["match"].get<std::string>();
+ const auto& json_rule = override_json["rule"];
+ override.rule.query = json_rule.count("query") == 0 ? "" : json_rule["query"].get<std::string>();
+ override.rule.match = json_rule.count("match") == 0 ? "" : json_rule["match"].get<std::string>();
- if(override_json["rule"].count("filter_by") != 0) {
+ if(json_rule.count("filter_by") != 0) {
if(!override_json["rule"]["filter_by"].is_string()) {
return Option<bool>(400, "Override `rule.filter_by` must be a string.");
}
diff --git a/test/collection_override_test.cpp b/test/collection_override_test.cpp
index 5f73d6a6..9d29806e 100644
--- a/test/collection_override_test.cpp
+++ b/test/collection_override_test.cpp
@@ -978,6 +978,30 @@ TEST_F(CollectionOverrideTest, FilterRule) {
// check to_json
nlohmann::json override_json_ser = override_rule.to_json();
ASSERT_EQ("points: 50", override_json_ser["rule"]["filter_by"]);
+
+ // without q/match
+ override_json = R"({
+ "id": "rule-2",
+ "rule": {
+ "filter_by": "points: 1"
+ },
+ "includes": [{
+ "id": "0",
+ "position": 1
+ }]
+ })"_json;
+
+ override_t override_rule2;
+ op = override_t::parse(override_json, "rule-2", override_rule2);
+ ASSERT_TRUE(op.ok());
+ coll1->add_override(override_rule2);
+
+ results = coll1->search("socks", {"name"}, "points: 1",
+ {}, sort_fields, {2}, 10, 1, FREQUENCY, {true}, 0).get();
+
+ ASSERT_EQ(2, results["hits"].size());
+ ASSERT_EQ("0", results["hits"][0]["document"]["id"].get<std::string>());
+ ASSERT_EQ("2", results["hits"][1]["document"]["id"].get<std::string>());
}
TEST_F(CollectionOverrideTest, PinnedAndHiddenHits) {