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
C++ algoliadatastoreelasticsearchenterprise-searchfacetingfull-text-searchfuzzy-searchgeosearchin-memoryinstantsearchmerchandisingpineconesearchsearch-enginesemantic-searchsimilarity-searchsite-searchsynonymstypo-tolerancevector-search
Prefix search must return exact matching string first.
Kishore Nallan35a2a0570997
diff --git a/src/art.cpp b/src/art.cppindex a1feb28b..72e06d7f 100644--- a/src/art.cpp+++ b/src/art.cpp@@ -73,7 +73,7 @@ bool compare_art_node_frequency(const art_node *a, const art_node *b) {}bool compare_art_node_score(const art_node* a, const art_node* b) {- int64_t a_value = 0, b_value = 0;+ int64_t a_value = std::numeric_limits<int64_t>::min(), b_value = std::numeric_limits<int64_t>::min();if(IS_LEAF(a)) {art_leaf* al = (art_leaf *) LEAF_RAW(a);@@ -942,7 +942,9 @@ void* art_delete(art_tree *t, const unsigned char *key, int key_len) {int art_topk_iter(const art_node *root, token_ordering token_order, size_t max_results,const uint32_t* filter_ids, size_t filter_ids_length,- const std::set<art_leaf*>& exclude_leaves, std::vector<art_leaf *> &results) {+ const std::set<art_leaf*>& exclude_leaves, const art_leaf* exact_leaf,+ std::vector<art_leaf *> &results) {+printf("INSIDE art_topk_iter: root->type: %d\n", root->type);std::priority_queue<const art_node *, std::vector<const art_node *>,@@ -973,7 +975,7 @@ int art_topk_iter(const art_node *root, token_ordering token_order, size_t max_rart_leaf *l = (art_leaf *) LEAF_RAW(n);//LOG(INFO) << "END LEAF SCORE: " << l->max_score;- if(exclude_leaves.count(l) != 0) {+ if(exclude_leaves.count(l) != 0 || l == exact_leaf) {continue;}@@ -1517,8 +1519,12 @@ int art_fuzzy_search(art_tree *t, const unsigned char *term, const int term_len,//auto begin = std::chrono::high_resolution_clock::now();+ size_t key_len = prefix ? term_len + 1 : term_len;+ art_leaf* exact_leaf = (art_leaf *) art_search(t, term, key_len);+ //LOG(INFO) << "exact_leaf: " << exact_leaf << ", term: " << term << ", term_len: " << term_len;+for(auto node: nodes) {- art_topk_iter(node, token_order, max_words, filter_ids, filter_ids_length, exclude_leaves, results);+ art_topk_iter(node, token_order, max_words, filter_ids, filter_ids_length, exclude_leaves, exact_leaf, results);}if(token_order == FREQUENCY) {@@ -1527,6 +1533,10 @@ int art_fuzzy_search(art_tree *t, const unsigned char *term, const int term_len,std::sort(results.begin(), results.end(), compare_art_leaf_score);}+ if(exact_leaf) {+ results.insert(results.begin(), exact_leaf);+ }+if(results.size() > max_words) {results.resize(max_words);}diff --git a/test/art_test.cpp b/test/art_test.cppindex 4682d5f2..98193b1c 100644--- a/test/art_test.cpp+++ b/test/art_test.cpp@@ -655,6 +655,38 @@ TEST(ArtTest, test_art_fuzzy_search_single_leaf_non_prefix) {ASSERT_TRUE(res == 0);}+TEST(ArtTest, test_art_fuzzy_search_prefix_token_ordering) {+ art_tree t;+ int res = art_tree_init(&t);+ ASSERT_TRUE(res == 0);++ // the last "e" should be returned first because of exact match+ std::vector<const char*> keys = {+ "enter", "elephant", "enamel", "ercot", "enyzme", "energy",+ "epoch", "epyc", "express", "everest", "end", "e"+ };++ for(size_t i = 0; i < keys.size(); i++) {+ art_document doc(i, keys.size() - i, {0});+ ASSERT_TRUE(NULL == art_insert(&t, (unsigned char*)keys[i], strlen(keys[i])+1, &doc));+ }++ std::vector<art_leaf*> leaves;+ art_fuzzy_search(&t, (const unsigned char *) "e", 1, 0, 0, 3, MAX_SCORE, true, nullptr, 0, leaves);++ std::string first_key(reinterpret_cast<char*>(leaves[0]->key), leaves[0]->key_len - 1);+ ASSERT_EQ("e", first_key);++ std::string second_key(reinterpret_cast<char*>(leaves[1]->key), leaves[1]->key_len - 1);+ ASSERT_EQ("enter", second_key);++ std::string third_key(reinterpret_cast<char*>(leaves[2]->key), leaves[2]->key_len - 1);+ ASSERT_EQ("elephant", third_key);++ res = art_tree_destroy(&t);+ ASSERT_TRUE(res == 0);+}+TEST(ArtTest, test_art_fuzzy_search) {art_tree t;int res = art_tree_init(&t);