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

Make typo correction less eager.

Kishore Nallan9dd3ba9d6d20
diff --git a/src/art.cpp b/src/art.cpp
index 0114fcd4..f14461b2 100644
--- a/src/art.cpp
+++ b/src/art.cpp
@@ -1271,7 +1271,8 @@ static inline int fuzzy_search_state(const bool prefix, int key_index, bool last
// a) because key's null character will appear first
if(last_key_char) {
- if(term_len > key_len && (term_len - key_len) <= max_cost) {
+ if(key_len > 5 && term_len > key_len && (term_len - key_len) <= max_cost) {
+ // used to handle some trailing edge cases, but limit to larger keys to prevent eager matches
cost = std::min(cost_row[key_len], cost_row[term_len]);
} else {
cost = cost_row[term_len];
diff --git a/src/collection.cpp b/src/collection.cpp
index eb9539ae..6a1193eb 100644
--- a/src/collection.cpp
+++ b/src/collection.cpp
@@ -1182,7 +1182,9 @@ Option<nlohmann::json> Collection::search(const std::string & query, const std::
}
}
- wrapper_doc["geo_distance_meters"] = geo_distances;
+ if(!geo_distances.empty()) {
+ wrapper_doc["geo_distance_meters"] = geo_distances;
+ }
hits_array.push_back(wrapper_doc);
}
diff --git a/test/collection_specific_test.cpp b/test/collection_specific_test.cpp
index 1e7c72a4..83760c56 100644
--- a/test/collection_specific_test.cpp
+++ b/test/collection_specific_test.cpp
@@ -144,6 +144,58 @@ TEST_F(CollectionSpecificTest, ExplicitHighlightFieldsConfig) {
collectionManager.drop_collection("coll1");
}
+TEST_F(CollectionSpecificTest, PrefixWithTypos1) {
+ std::vector<field> fields = {field("title", field_types::STRING, false),
+ field("points", field_types::INT32, false),};
+
+ Collection* coll1 = collectionManager.create_collection("coll1", 1, fields, "points").get();
+
+ nlohmann::json doc1;
+ doc1["id"] = "0";
+ doc1["title"] = "PRÍNCIPE - Restaurante e Snack Bar";
+ doc1["points"] = 100;
+
+ ASSERT_TRUE(coll1->add(doc1.dump()).ok());
+
+ auto results = coll1->search("maria", {"title"}, "", {}, {}, {2}, 10,
+ 1, FREQUENCY, {true}).get();
+
+ ASSERT_EQ(0, results["hits"].size());
+
+ results = coll1->search("maria", {"title"}, "", {}, {}, {2}, 10,
+ 1, FREQUENCY, {false}).get();
+
+ ASSERT_EQ(0, results["hits"].size());
+
+ collectionManager.drop_collection("coll1");
+}
+
+TEST_F(CollectionSpecificTest, PrefixWithTypos2) {
+ std::vector<field> fields = {field("title", field_types::STRING, false),
+ field("points", field_types::INT32, false),};
+
+ Collection* coll1 = collectionManager.create_collection("coll1", 1, fields, "points").get();
+
+ nlohmann::json doc1;
+ doc1["id"] = "0";
+ doc1["title"] = "Av. Mal. Humberto Delgado 206, 4760-012 Vila Nova de Famalicão, Portugal";
+ doc1["points"] = 100;
+
+ ASSERT_TRUE(coll1->add(doc1.dump()).ok());
+
+ auto results = coll1->search("maria", {"title"}, "", {}, {}, {2}, 10,
+ 1, FREQUENCY, {true}).get();
+
+ ASSERT_EQ(0, results["hits"].size());
+
+ results = coll1->search("maria", {"title"}, "", {}, {}, {2}, 10,
+ 1, FREQUENCY, {false}).get();
+
+ ASSERT_EQ(0, results["hits"].size());
+
+ collectionManager.drop_collection("coll1");
+}
+
TEST_F(CollectionSpecificTest, CreateManyCollectionsAndDeleteOneOfThem) {
std::vector<field> fields = {field("title", field_types::STRING, false),
field("points", field_types::INT32, false),};