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 pagination.

Kishore Nallan9cc3e7e5ea2a
diff --git a/include/art.h b/include/art.h
index f9768bfc..9a53b5a4 100644
--- a/include/art.h
+++ b/include/art.h
@@ -239,7 +239,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 max_cost,
const int max_words, const token_ordering token_order, const bool prefix, std::vector<art_leaf *> &results);
-static int topk_iter(art_node *root, int term_len, int k, std::vector<art_leaf*> & results);
+static int topk_iter(const art_node *root, token_ordering token_order, const int max_results, std::vector<art_leaf*> & results);
#ifdef __cplusplus
}
diff --git a/src/art.cpp b/src/art.cpp
index 2b652fe4..a9850310 100644
--- a/src/art.cpp
+++ b/src/art.cpp
@@ -57,7 +57,7 @@ bool compare_art_node_frequency(const art_node *a, const art_node *b) {
b_value = b->max_token_count;
}
- return a_value > b_value;
+ return a_value < b_value;
}
bool compare_art_node_score(const art_node* a, const art_node* b) {
@@ -77,7 +77,7 @@ bool compare_art_node_score(const art_node* a, const art_node* b) {
b_value = b->max_score;
}
- return a_value > b_value;
+ return a_value < b_value;
}
/**
@@ -883,10 +883,19 @@ static uint32_t get_score(art_node* child) {
return child->max_token_count;
}
-static int topk_iter(const art_node *root, int max_results, std::vector<art_leaf*> & results) {
+static int topk_iter(const art_node *root, token_ordering token_order, const int max_results, std::vector<art_leaf*> & results) {
printf("INSIDE topk_iter: root->type: %d\n", root->type);
+
std::priority_queue<art_node *, std::vector<const art_node *>,
- std::function<bool(const art_node*, const art_node*)>> q(compare_art_node_frequency);
+ std::function<bool(const art_node*, const art_node*)>> q;
+
+ if(token_order == FREQUENCY) {
+ q = std::priority_queue<art_node *, std::vector<const art_node *>,
+ std::function<bool(const art_node*, const art_node*)>>(compare_art_node_frequency);
+ } else {
+ q = std::priority_queue<art_node *, std::vector<const art_node *>,
+ std::function<bool(const art_node*, const art_node*)>>(compare_art_node_score);
+ }
q.push(root);
@@ -1320,7 +1329,7 @@ int art_fuzzy_search(art_tree *t, const unsigned char *term, const int term_len,
begin = std::chrono::high_resolution_clock::now();
for(auto node: nodes) {
- topk_iter(node, max_words, results);
+ topk_iter(node, token_order, max_words, results);
}
if(token_order == FREQUENCY) {
diff --git a/src/collection.cpp b/src/collection.cpp
index a3508ab1..021daf10 100644
--- a/src/collection.cpp
+++ b/src/collection.cpp
@@ -119,11 +119,10 @@ void Collection::search_candidates(std::vector<std::vector<art_leaf*>> & token_l
// go through each matching document id and calculate match score
Topster<100> topster;
score_results(topster, query_suggestion, result_ids, result_size);
- total_results += result_size;
delete[] result_ids;
topster.sort();
- for (uint32_t i = 0; i < topster.size; i++) {
+ for (uint32_t i = 0; i < topster.size && total_results < max_results; i++) {
uint64_t seq_id = topster.getKeyAt(i);
if(dedup_seq_ids.count(seq_id) == 0) {
std::string value;
@@ -131,6 +130,7 @@ void Collection::search_candidates(std::vector<std::vector<art_leaf*>> & token_l
nlohmann::json document = nlohmann::json::parse(value);
results.push_back(document);
dedup_seq_ids.emplace(seq_id);
+ total_results++;
}
}
diff --git a/test/collection_test.cpp b/test/collection_test.cpp
index a6d9f351..818b96e7 100644
--- a/test/collection_test.cpp
+++ b/test/collection_test.cpp
@@ -61,6 +61,16 @@ TEST_F(CollectionTest, ExactPhraseSearch) {
std::string result_id = result["id"];
ASSERT_STREQ(id.c_str(), result_id.c_str());
}
+
+ // Check pagination
+ results = collection->search("rocket launch", 0, 3);
+ ASSERT_EQ(3, results.size());
+ for(size_t i = 0; i < 3; i++) {
+ nlohmann::json result = results.at(i);
+ std::string id = ids.at(i);
+ std::string result_id = result["id"];
+ ASSERT_STREQ(id.c_str(), result_id.c_str());
+ }
}
TEST_F(CollectionTest, SkipUnindexedTokensDuringPhraseSearch) {
@@ -160,6 +170,15 @@ TEST_F(CollectionTest, TypoTokenRankedByScoreAndFrequency) {
ASSERT_STREQ(id.c_str(), result_id.c_str());
}
+ // Check pagination
+ results = collection->search("loox", 1, 1, FREQUENCY, false);
+ ASSERT_EQ(1, results.size());
+ std::string solo_id = results.at(0)["id"];
+ ASSERT_STREQ("3", solo_id.c_str());
+
+ results = collection->search("loox", 1, 2, FREQUENCY, false);
+ ASSERT_EQ(2, results.size());
+
// Check total ordering
results = collection->search("loox", 1, 10, FREQUENCY, false);