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

Fixed a bug in fuzzy search.

Kishore Nallan45f0814a7a60
diff --git a/src/art.cpp b/src/art.cpp
index 4c6087d1..006ee65c 100644
--- a/src/art.cpp
+++ b/src/art.cpp
@@ -1085,12 +1085,12 @@ int art_iter_prefix(art_tree *t, const unsigned char *key, int key_len, art_call
new_current_row[0] = previous_row[0] + 1;\
row_min = levenshtein_score(child_char, term, term_len, previous_row, new_current_row);\
\
- printf("fuzzy_recurse - score: %d, child char: %c, cost: %d, max_cost: %d, row_min: %d, depth: %d, term_len: %d \n",\
- child->score, child_char, new_current_row[term_len], max_cost, row_min, depth, term_len);\
+ printf("fuzzy_recurse - score: %d, child char: %c, cost: %d, max_cost: %d, row_min: %d, depth: %d, term[depth]: %c \n",\
+ child->max_score, child_char, new_current_row[term_len], max_cost, row_min, depth, term[depth]);\
\
if(depth == term_len-1) {\
/* reached end of term, and cost is below threshold, print children of this node as matches*/\
- if(new_current_row[term_len] <= max_cost) {\
+ if(row_min <= max_cost) {\
printf("START RECURSIVE ITER\n");\
results.push_back(child);\
}\
@@ -1174,7 +1174,7 @@ static int art_iter_fuzzy_prefix_recurse(art_node *n, const unsigned char *term,
}
printf("START PARTIAL: score: %d, partial_len: %d, partial: %s, term_len: %d, depth: %d\n",
- n->score, n->partial_len, n->partial, term_len, depth);
+ n->max_score, n->partial_len, n->partial, term_len, depth);
// internal node - first we check partial (via path compression) and then child index
int partial_len = min(MAX_PREFIX_LEN, n->partial_len);
diff --git a/src/main.cpp b/src/main.cpp
index 34ab5eda..42c23119 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -110,11 +110,13 @@ void find_documents(art_tree & t, unordered_map<uint32_t, uint16_t>& docscores,
for(auto token: tokens) {
for(int max_cost=0; max_cost<2; max_cost++) {
vector<art_leaf*> leaves;
- art_iter_fuzzy_prefix(&t, (const unsigned char *) token.c_str(), (int) token.length(), max_cost, 20, leaves);
+ //cout << "For token: " << token << endl;
+ art_iter_fuzzy_prefix(&t, (const unsigned char *) token.c_str(), (int) token.length(), max_cost, 10, leaves);
if(!leaves.empty()) {
- for(auto i=0; i<leaves.size(); i++) {
- cout << leaves[i]->key << " / " << leaves[i]->token_count << endl;
- }
+ /*for(auto i=0; i<leaves.size(); i++) {
+ printf("%.*s", leaves[i]->key_len, leaves[i]->key);
+ printf(" - max_cost: %d, - score: %d\n", max_cost, leaves[i]->token_count);
+ }*/
token_leaves.push_back(leaves);
break;
}
@@ -233,6 +235,10 @@ int main() {
if(parts.size() != 2) continue;
+ /*if(doc_id == 108028 || doc_id == 592504 || doc_id == 51876) {
+ cout << "Doc " << doc_id << ": " << line << endl;
+ }*/
+
docscores[doc_id] = (uint16_t) stoi(parts[1]);
index_document(t, doc_id, tokens, stoi(parts[1]));
doc_id++;
@@ -261,9 +267,9 @@ int main() {
std::cout << ", Value: " << leaf->values->ids.at(0) << std::endl;
}*/
- ///find_documents(t, docscores, "app;e", 10);
+// find_documents(t, docscores, "thei nternet", 10);
- string token = "app;e";
+ string token = "nternet";
vector<art_leaf*> leaves;
auto begin = std::chrono::high_resolution_clock::now();