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

Pinned and hidden hits will take precedence over override rules.

kishorenc2cf67d0c1e5d
diff --git a/src/collection.cpp b/src/collection.cpp
index ce8ab1a7..f679c1c6 100644
--- a/src/collection.cpp
+++ b/src/collection.cpp
@@ -289,31 +289,6 @@ void Collection::populate_overrides(std::string query,
std::vector<uint32_t> & excluded_ids) {
StringUtils::tolowercase(query);
- // NOTE: if pinned or hidden hits are provided, then overrides will be just ignored
-
- if(!pinned_hits.empty()) {
- for(const auto & hit: pinned_hits) {
- Option<uint32_t> seq_id_op = doc_id_to_seq_id(hit.first);
- if(seq_id_op.ok()) {
- included_ids.push_back(seq_id_op.get());
- id_pos_map[seq_id_op.get()] = hit.second;
- }
- }
- }
-
- if(!hidden_hits.empty()) {
- for(const auto & hit: hidden_hits) {
- Option<uint32_t> seq_id_op = doc_id_to_seq_id(hit);
- if(seq_id_op.ok()) {
- excluded_ids.push_back(seq_id_op.get());
- }
- }
- }
-
- if(!hidden_hits.empty() || !pinned_hits.empty()) {
- return ;
- }
-
for(const auto & override_kv: overrides) {
const auto & override = override_kv.second;
@@ -335,6 +310,29 @@ void Collection::populate_overrides(std::string query,
}
}
}
+
+ // If pinned or hidden hits are provided, they take precedence over overrides
+
+ if(!pinned_hits.empty()) {
+ for(const auto & hit: pinned_hits) {
+ Option<uint32_t> seq_id_op = doc_id_to_seq_id(hit.first);
+ if(seq_id_op.ok()) {
+ included_ids.push_back(seq_id_op.get());
+ id_pos_map[seq_id_op.get()] = hit.second;
+ }
+ }
+ }
+
+ if(!hidden_hits.empty()) {
+ for(const auto & hit: hidden_hits) {
+ Option<uint32_t> seq_id_op = doc_id_to_seq_id(hit);
+ if(seq_id_op.ok()) {
+ included_ids.erase(std::remove(included_ids.begin(), included_ids.end(), seq_id_op.get()), included_ids.end());
+ id_pos_map.erase(seq_id_op.get());
+ excluded_ids.push_back(seq_id_op.get());
+ }
+ }
+ }
}
Option<nlohmann::json> Collection::search(const std::string & query, const std::vector<std::string> & search_fields,
diff --git a/test/collection_override_test.cpp b/test/collection_override_test.cpp
index d23c886e..3a73f567 100644
--- a/test/collection_override_test.cpp
+++ b/test/collection_override_test.cpp
@@ -261,7 +261,7 @@ TEST_F(CollectionOverrideTest, IncludeExcludeHitsQuery) {
// basic pinning
- auto results = coll_mul_fields->search("the", {"title"}, "", {"starring"}, {}, 0, 10, 1, FREQUENCY,
+ auto results = coll_mul_fields->search("the", {"title"}, "", {"starring"}, {}, 0, 50, 1, FREQUENCY,
false, Index::DROP_TOKENS_THRESHOLD,
spp::sparse_hash_set<std::string>(),
spp::sparse_hash_set<std::string>(), 10, 500, "starring: will", 30,
@@ -278,7 +278,7 @@ TEST_F(CollectionOverrideTest, IncludeExcludeHitsQuery) {
// both pinning and hiding
hidden_hits = {"11", "16"};
- results = coll_mul_fields->search("the", {"title"}, "", {"starring"}, {}, 0, 10, 1, FREQUENCY,
+ results = coll_mul_fields->search("the", {"title"}, "", {"starring"}, {}, 0, 50, 1, FREQUENCY,
false, Index::DROP_TOKENS_THRESHOLD,
spp::sparse_hash_set<std::string>(),
spp::sparse_hash_set<std::string>(), 10, 500, "starring: will", 30,
@@ -302,16 +302,20 @@ TEST_F(CollectionOverrideTest, IncludeExcludeHitsQuery) {
};
// trying to include an ID that is also being hidden via `hidden_hits` query param will not work
- // as if pinned or hidden hits are provided, overrides will be entirely skipped
+ // as pinned and hidden hits will take precedence over override rules
override_json_include["includes"] = nlohmann::json::array();
override_json_include["includes"][0] = nlohmann::json::object();
override_json_include["includes"][0]["id"] = "11";
- override_json_include["includes"][0]["position"] = 1;
+ override_json_include["includes"][0]["position"] = 2;
+
+ override_json_include["includes"][1] = nlohmann::json::object();
+ override_json_include["includes"][1]["id"] = "8";
+ override_json_include["includes"][1]["position"] = 1;
override_t override_include(override_json_include);
coll_mul_fields->add_override(override_include);
- results = coll_mul_fields->search("the", {"title"}, "", {"starring"}, {}, 0, 10, 1, FREQUENCY,
+ results = coll_mul_fields->search("the", {"title"}, "", {"starring"}, {}, 0, 50, 1, FREQUENCY,
false, Index::DROP_TOKENS_THRESHOLD,
spp::sparse_hash_set<std::string>(),
spp::sparse_hash_set<std::string>(), 10, 500, "starring: will", 30,
@@ -319,6 +323,6 @@ TEST_F(CollectionOverrideTest, IncludeExcludeHitsQuery) {
{}, {hidden_hits}).get();
ASSERT_EQ(8, results["found"].get<size_t>());
- ASSERT_STREQ("6", results["hits"][0]["document"]["id"].get<std::string>().c_str());
- ASSERT_STREQ("8", results["hits"][1]["document"]["id"].get<std::string>().c_str());
+ ASSERT_STREQ("8", results["hits"][0]["document"]["id"].get<std::string>().c_str());
+ ASSERT_STREQ("6", results["hits"][1]["document"]["id"].get<std::string>().c_str());
}
\ No newline at end of file