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

Reduce false positives when considering trailing typos.

Kishore Nallan1b4616542385
diff --git a/src/art.cpp b/src/art.cpp
index 76dbddaf..2a6c99e9 100644
--- a/src/art.cpp
+++ b/src/art.cpp
@@ -1318,7 +1318,7 @@ static inline int fuzzy_search_state(const bool prefix, int key_index, bool last
// used to match q=strawberries on key=strawberry, but limit to larger keys to prevent eager matches
if(key_len > 5 && term_len > key_len && (term_len - key_len) <= max_cost &&
- cost >= min_cost && cost <= max_cost) {
+ cost >= min_cost-1 && cost <= max_cost-1) {
return 1;
}
diff --git a/test/art_test.cpp b/test/art_test.cpp
index ae900fcc..38c962db 100644
--- a/test/art_test.cpp
+++ b/test/art_test.cpp
@@ -622,17 +622,33 @@ TEST(ArtTest, test_art_fuzzy_search_single_leaf_prefix) {
EXPECT_EQ(1, posting_t::first_id(l->values));
std::vector<art_leaf*> leaves;
- art_fuzzy_search(&t, (const unsigned char *) "aplication", strlen(key)-1, 0, 1, 10, FREQUENCY, true, nullptr, 0, leaves);
+ std::string term = "aplication";
+ art_fuzzy_search(&t, (const unsigned char *)(term.c_str()), term.size(), 0, 1, 10, FREQUENCY, true, nullptr, 0, leaves);
ASSERT_EQ(1, leaves.size());
leaves.clear();
- art_fuzzy_search(&t, (const unsigned char *) "aplication", strlen(key)-1, 0, 2, 10, FREQUENCY, true, nullptr, 0, leaves);
+ art_fuzzy_search(&t, (const unsigned char *)(term.c_str()), term.size(), 0, 2, 10, FREQUENCY, true, nullptr, 0, leaves);
ASSERT_EQ(1, leaves.size());
res = art_tree_destroy(&t);
ASSERT_TRUE(res == 0);
}
+TEST(ArtTest, test_art_fuzzy_search_single_leaf_qlen_greater_than_key) {
+ art_tree t;
+ int res = art_tree_init(&t);
+ ASSERT_TRUE(res == 0);
+
+ const char* key = "storka";
+ art_document doc = get_document((uint32_t) 1);
+ ASSERT_TRUE(NULL == art_insert(&t, (unsigned char*)key, strlen(key)+1, &doc));
+
+ std::string term = "starkbin";
+ std::vector<art_leaf*> leaves;
+ art_fuzzy_search(&t, (const unsigned char *)(term.c_str()), term.size(), 0, 2, 10, FREQUENCY, true, nullptr, 0, leaves);
+ ASSERT_EQ(0, leaves.size());
+}
+
TEST(ArtTest, test_art_fuzzy_search_single_leaf_non_prefix) {
art_tree t;
int res = art_tree_init(&t);
@@ -811,7 +827,7 @@ TEST(ArtTest, test_art_fuzzy_search) {
leaves.clear();
art_fuzzy_search(&t, (const unsigned char *) "antisocao", strlen("antisocao"), 0, 2, 10, FREQUENCY, true, nullptr, 0, leaves);
- ASSERT_EQ(9, leaves.size());
+ ASSERT_EQ(6, leaves.size());
long long int timeMillis = std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::high_resolution_clock::now() - begin).count();