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

Handle empty hits because of curated ids.

Kishore Nallan0be6c0ad7526
diff --git a/src/index.cpp b/src/index.cpp
index 6095ed06..f958cc00 100644
--- a/src/index.cpp
+++ b/src/index.cpp
@@ -4196,8 +4196,11 @@ Option<bool> Index::fuzzy_search_fields(const std::vector<search_field_t>& the_f
resume_typo_loop:
auto results_count = group_limit != 0 ? groups_processed.size() : all_result_ids_len;
- if(!exhaustive_search && results_count >= typo_tokens_threshold) {
+ if(!exhaustive_search && (results_count >= typo_tokens_threshold ||
+ (results_count == 0 && !curated_ids.empty()))) {
// if typo threshold is breached, we are done
+ // Also, if there are curated hits, results_count will be zero when all hits overlap with curated hits
+ // in that case, we should not think that no results were found.
return Option<bool>(true);
}
diff --git a/test/collection_override_test.cpp b/test/collection_override_test.cpp
index d696273f..ea5c7abf 100644
--- a/test/collection_override_test.cpp
+++ b/test/collection_override_test.cpp
@@ -4320,4 +4320,72 @@ TEST_F(CollectionOverrideTest, FilterPinnedHits) {
ASSERT_EQ("0", results["hits"][1]["document"]["id"].get<std::string>());
ASSERT_EQ("1", results["hits"][2]["document"]["id"].get<std::string>());
ASSERT_EQ("2", results["hits"][3]["document"]["id"].get<std::string>());
-}
\ No newline at end of file
+}
+
+TEST_F(CollectionOverrideTest, AvoidTypoMatchingWhenOverlapWithCuratedData) {
+ std::vector<field> fields = {field("title", field_types::STRING, false),
+ field("points", field_types::INT32, false)};
+
+ Collection* coll3 = collectionManager.get_collection("coll3").get();
+ if (coll3 == nullptr) {
+ coll3 = collectionManager.create_collection("coll3", 1, fields, "points").get();
+ }
+
+ nlohmann::json doc;
+
+ doc["title"] = "Snapdragon 7 gen 2023";
+ doc["points"] = 100;
+ ASSERT_TRUE(coll3->add(doc.dump()).ok());
+
+ doc["title"] = "Snapdragon 732G 2023";
+ doc["points"] = 91;
+ ASSERT_TRUE(coll3->add(doc.dump()).ok());
+
+ doc["title"] = "Mediatak 4 gen 2023";
+ doc["points"] = 65;
+ ASSERT_TRUE(coll3->add(doc.dump()).ok());
+
+ doc["title"] = "Mediatek Dimensity 720G 2022";
+ doc["points"] = 87;
+ ASSERT_TRUE(coll3->add(doc.dump()).ok());
+
+ doc["title"] = "Mediatek Dimensity 470G 2023";
+ doc["points"] = 63;
+ ASSERT_TRUE(coll3->add(doc.dump()).ok());
+
+ auto pinned_hits = "3:1, 4:2";
+
+ auto results = coll3->search("Mediatek", {"title"}, "", {}, {},
+ {2}, 50, 1, FREQUENCY,
+ {false}, Index::DROP_TOKENS_THRESHOLD,
+ spp::sparse_hash_set<std::string>(),
+ spp::sparse_hash_set<std::string>(), 10,
+ "", 30, 5, "",
+ 10, pinned_hits, {}, {}, 3,
+ "<mark>", "</mark>", {}, UINT_MAX,
+ true, false, true, "",
+ false, 6000 * 1000, 4, 7,
+ fallback, 4, {off}, INT16_MAX,
+ INT16_MAX, 2, false).get();
+
+ ASSERT_EQ(2, results["hits"].size());
+ ASSERT_EQ("3", results["hits"][0]["document"]["id"].get<std::string>());
+ ASSERT_EQ("4", results["hits"][1]["document"]["id"].get<std::string>());
+
+ results = coll3->search("Mediatek", {"title"}, "", {}, {},
+ {2}, 50, 1, FREQUENCY,
+ {false}, Index::DROP_TOKENS_THRESHOLD,
+ spp::sparse_hash_set<std::string>(),
+ spp::sparse_hash_set<std::string>(), 10,
+ "", 30, 5, "",
+ 10, pinned_hits, {}, {}, 3,
+ "<mark>", "</mark>", {}, UINT_MAX,
+ true, false, true, "",
+ false, 6000 * 1000, 4, 7,
+ fallback, 4, {off}, INT16_MAX,
+ INT16_MAX, 2, false).get();
+
+ ASSERT_EQ(2, results["hits"].size());
+ ASSERT_EQ("3", results["hits"][0]["document"]["id"].get<std::string>());
+ ASSERT_EQ("4", results["hits"][1]["document"]["id"].get<std::string>());
+}