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

Ensure that search does not fetch existing tokens.

Kishore Nallan793e21a1c27d
diff --git a/include/art.h b/include/art.h
index 976a47c5..7f9858da 100644
--- a/include/art.h
+++ b/include/art.h
@@ -4,6 +4,7 @@
#include <stdint.h>
#include <stdbool.h>
#include <vector>
+#include <set>
#include "array.h"
#include "sorted_array.h"
@@ -251,10 +252,7 @@ int art_iter_prefix(art_tree *t, const unsigned char *prefix, int prefix_len, ar
int art_fuzzy_search(art_tree *t, const unsigned char *term, const int term_len, const int min_cost, const int max_cost,
const int max_words, const token_ordering token_order, const bool prefix,
const uint32_t *filter_ids, size_t filter_ids_length,
- std::vector<art_leaf *> &results);
-
-int art_topk_iter(const art_node *root, token_ordering token_order, size_t max_results,
- std::vector<art_leaf *> &results);
+ std::vector<art_leaf *> &results, const std::set<art_leaf *>& exclude_leaves = {});
void encode_int32(int32_t n, unsigned char *chars);
diff --git a/src/art.cpp b/src/art.cpp
index 094c7ecc..95b1f14b 100644
--- a/src/art.cpp
+++ b/src/art.cpp
@@ -883,7 +883,7 @@ 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,
- std::vector<art_leaf *> &results) {
+ const std::set<art_leaf*>& exclude_leaves, 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 *>,
@@ -912,6 +912,9 @@ int art_topk_iter(const art_node *root, token_ordering token_order, size_t max_r
art_leaf *l = (art_leaf *) LEAF_RAW(n);
//LOG(INFO) << "END LEAF SCORE: " << l->max_score;
+ if(exclude_leaves.count(l) != 0) {
+ continue;
+ }
if(filter_ids_length == 0) {
results.push_back(l);
@@ -1416,7 +1419,7 @@ static void art_fuzzy_recurse(unsigned char p, unsigned char c, const art_node *
int art_fuzzy_search(art_tree *t, const unsigned char *term, const int term_len, const int min_cost, const int max_cost,
const int max_words, const token_ordering token_order, const bool prefix,
const uint32_t *filter_ids, size_t filter_ids_length,
- std::vector<art_leaf *> &results) {
+ std::vector<art_leaf *> &results, const std::set<art_leaf *>& exclude_leaves) {
std::vector<const art_node*> nodes;
int irow[term_len + 1];
@@ -1445,7 +1448,7 @@ int art_fuzzy_search(art_tree *t, const unsigned char *term, const int term_len,
//auto begin = std::chrono::high_resolution_clock::now();
for(auto node: nodes) {
- art_topk_iter(node, token_order, max_words, filter_ids, filter_ids_length, results);
+ art_topk_iter(node, token_order, max_words, filter_ids, filter_ids_length, exclude_leaves, results);
}
if(token_order == FREQUENCY) {
diff --git a/src/index.cpp b/src/index.cpp
index 3ff5aad3..266b7072 100644
--- a/src/index.cpp
+++ b/src/index.cpp
@@ -1905,6 +1905,7 @@ void Index::search_field(const uint8_t & field_id,
// stores candidates for each token, i.e. i-th index would have all possible tokens with a cost of "c"
std::vector<token_candidates> token_candidates_vec;
+ std::set<art_leaf*> unique_tokens;
auto product = []( long long a, std::vector<int>& b ) { return a*b.size(); };
long long n = 0;
@@ -1942,10 +1943,13 @@ void Index::search_field(const uint8_t & field_id,
// need less candidates for filtered searches since we already only pick tokens with results
art_fuzzy_search(search_index.at(field), (const unsigned char *) token.c_str(), token_len,
costs[token_index], costs[token_index], combination_limit, token_order, prefix_search,
- filter_ids, filter_ids_length, leaves);
+ filter_ids, filter_ids_length, leaves, unique_tokens);
if(!leaves.empty()) {
token_cost_cache.emplace(token_cost_hash, leaves);
+ for(auto leaf: leaves) {
+ unique_tokens.emplace(leaf);
+ }
}
}
diff --git a/test/collection_specific_test.cpp b/test/collection_specific_test.cpp
index d7d650b0..af9e4d8a 100644
--- a/test/collection_specific_test.cpp
+++ b/test/collection_specific_test.cpp
@@ -785,7 +785,7 @@ TEST_F(CollectionSpecificTest, HighlightWithDropTokensAndPrefixSearch) {
"", {}, {}, {2, 2}, 10,
1, FREQUENCY, {true, true},
1, spp::sparse_hash_set<std::string>(),
- spp::sparse_hash_set<std::string>(), 10, "", 30, 4, "description", 1, {}, {}, {}, 0,
+ spp::sparse_hash_set<std::string>(), 10, "", 30, 4, "", 1, {}, {}, {}, 0,
"<mark>", "</mark>").get();
ASSERT_EQ(2, results["hits"][0]["highlights"].size());
@@ -828,3 +828,36 @@ TEST_F(CollectionSpecificTest, PrefixSearchOnlyOnLastToken) {
collectionManager.drop_collection("coll1");
}
+
+TEST_F(CollectionSpecificTest, TokenStartingWithSameLetterAsPrevToken) {
+ std::vector<field> fields = {field("name", 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["name"] = "John Jack";
+ doc1["points"] = 100;
+
+ nlohmann::json doc2;
+ doc2["id"] = "1";
+ doc2["name"] = "John Williams";
+ doc2["points"] = 100;
+
+ ASSERT_TRUE(coll1->add(doc1.dump()).ok());
+ ASSERT_TRUE(coll1->add(doc2.dump()).ok());
+
+ auto results = coll1->search("john j", {"name"},
+ "", {}, {}, {2}, 10,
+ 1, FREQUENCY, {true},
+ 10, spp::sparse_hash_set<std::string>(),
+ spp::sparse_hash_set<std::string>(), 10, "", 30, 4, "", 10, {}, {}, {}, 0,
+ "<mark>", "</mark>").get();
+
+ ASSERT_EQ(2, results["hits"].size());
+ ASSERT_EQ("0", results["hits"][0]["document"]["id"].get<std::string>());
+ ASSERT_EQ("1", results["hits"][1]["document"]["id"].get<std::string>());
+
+ collectionManager.drop_collection("coll1");
+}
\ No newline at end of file