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

Rank prefix match below exact match.

kishorenc435476df5d5e
diff --git a/include/index.h b/include/index.h
index 48aa9687..f3d0ad63 100644
--- a/include/index.h
+++ b/include/index.h
@@ -148,7 +148,7 @@ private:
StringUtils string_utils;
- static inline void next_suggestion(const std::vector<token_candidates> &token_candidates_vec,
+ static inline uint32_t next_suggestion(const std::vector<token_candidates> &token_candidates_vec,
long long int n,
std::vector<art_leaf *>& actual_query_suggestion,
std::vector<art_leaf *>& query_suggestion);
diff --git a/src/index.cpp b/src/index.cpp
index 8d7d0305..8565ab68 100644
--- a/src/index.cpp
+++ b/src/index.cpp
@@ -842,12 +842,12 @@ void Index::search_candidates(const uint8_t & field_id,
// actual query suggestion preserves original order of tokens in query
std::vector<art_leaf*> actual_query_suggestion(token_candidates_vec.size());
- next_suggestion(token_candidates_vec, n, actual_query_suggestion, query_suggestion);
+ uint32_t total_cost = next_suggestion(token_candidates_vec, n, actual_query_suggestion, query_suggestion);
/*LOG(INFO) << "n: " << n;
for(size_t i=0; i < query_suggestion.size(); i++) {
LOG(INFO) << "i: " << i << " - " << query_suggestion[i]->key << ", ids: "
- << query_suggestion[i]->values->ids.getLength();
+ << query_suggestion[i]->values->ids.getLength() << ", total_cost: " << total_cost;
}*/
// initialize results with the starting element (for further intersection)
@@ -856,13 +856,8 @@ void Index::search_candidates(const uint8_t & field_id,
continue;
}
- uint32_t total_cost = 0;
uint32_t* result_ids = query_suggestion[0]->values->ids.uncompress();
- for(const auto& tc: token_candidates_vec) {
- total_cost += tc.cost;
- }
-
// intersect the document ids for each token to find docs that contain all the tokens (stored in `result_ids`)
for(size_t i=1; i < query_suggestion.size(); i++) {
uint32_t* out = nullptr;
@@ -897,10 +892,10 @@ void Index::search_candidates(const uint8_t & field_id,
}
//LOG(INFO) << "n: " << n;
- std::stringstream log_query;
+ /*std::stringstream log_query;
for(size_t i=0; i < query_suggestion.size(); i++) {
log_query << query_suggestion[i]->key << " ";
- }
+ }*/
if(filter_ids != nullptr) {
// intersect once again with filter ids
@@ -1541,7 +1536,6 @@ void Index::search(Option<uint32_t> & outcome,
leaves[0]->values->offset_index.at(doc_index+1);
words_present += (end_offset - start_offset);
- int k = 10;
/*if(!leaves.empty()) {
LOG(INFO) << "tok: " << leaves[0]->key;
@@ -1986,16 +1980,24 @@ void Index::populate_token_positions(const std::vector<art_leaf *>& query_sugges
}
}
-inline void Index::next_suggestion(const std::vector<token_candidates> &token_candidates_vec,
+inline uint32_t Index::next_suggestion(const std::vector<token_candidates> &token_candidates_vec,
long long int n,
std::vector<art_leaf *>& actual_query_suggestion,
std::vector<art_leaf *>& query_suggestion) {
+ uint32_t total_cost = 0;
+
// generate the next combination from `token_leaves` and store it in `query_suggestion`
ldiv_t q { n, 0 };
for(long long i = 0 ; i < (long long) token_candidates_vec.size(); i++) {
+ size_t token_size = token_candidates_vec[i].token.size();
q = ldiv(q.quot, token_candidates_vec[i].candidates.size());
actual_query_suggestion[i] = token_candidates_vec[i].candidates[q.rem];
query_suggestion[i] = token_candidates_vec[i].candidates[q.rem];
+ total_cost += token_candidates_vec[i].cost;
+
+ if(actual_query_suggestion[i]->key_len != token_size+1) {
+ total_cost++;
+ }
}
// Sort ascending based on matched documents for each token for faster intersection.
@@ -2003,6 +2005,8 @@ inline void Index::next_suggestion(const std::vector<token_candidates> &token_ca
sort(query_suggestion.begin(), query_suggestion.end(), [](const art_leaf* left, const art_leaf* right) {
return left->values->ids.getLength() < right->values->ids.getLength();
});
+
+ return total_cost;
}
void Index::remove_and_shift_offset_index(sorted_array& offset_index, const uint32_t* indices_sorted,
diff --git a/test/collection_test.cpp b/test/collection_test.cpp
index 8e8fa060..ba33d71d 100644
--- a/test/collection_test.cpp
+++ b/test/collection_test.cpp
@@ -410,7 +410,7 @@ TEST_F(CollectionTest, TextContainingAnActualTypo) {
ASSERT_EQ(4, results["hits"].size());
ASSERT_EQ(13, results["found"].get<uint32_t>());
- std::vector<std::string> ids = {"8", "19", "6", "21"};
+ std::vector<std::string> ids = {"19", "6", "21", "8"};
for(size_t i = 0; i < results["hits"].size(); i++) {
nlohmann::json result = results["hits"].at(i);
@@ -424,7 +424,7 @@ TEST_F(CollectionTest, TextContainingAnActualTypo) {
ASSERT_EQ(8, results["hits"].size());
ASSERT_EQ(8, results["found"].get<uint32_t>());
- ids = {"20", "19", "6", "4", "3", "10", "8", "21"};
+ ids = {"20", "19", "6", "3", "21", "4", "10", "8", "21"};
for(size_t i = 0; i < results["hits"].size(); i++) {
nlohmann::json result = results["hits"].at(i);
@@ -2790,6 +2790,46 @@ TEST_F(CollectionTest, MultiFieldMatchRankingOnFieldOrder) {
collectionManager.drop_collection("coll1");
}
+TEST_F(CollectionTest, PrefixRankedAfterExactMatch) {
+ Collection *coll1;
+
+ std::vector<field> fields = {field("title", field_types::STRING, false),
+ field("points", field_types::INT32, false),};
+
+ coll1 = collectionManager.get_collection("coll1");
+ if(coll1 == nullptr) {
+ coll1 = collectionManager.create_collection("coll1", 1, fields, "points").get();
+ }
+
+ std::vector<std::vector<std::string>> records = {
+ {"Rotini Puttanesca"},
+ {"Poulet Roti Tout Simple"},
+ {"Chapatis (Roti)"},
+ {"School Days Rotini Pasta Salad"},
+ };
+
+ for(size_t i=0; i<records.size(); i++) {
+ nlohmann::json doc;
+
+ doc["id"] = std::to_string(i);
+ doc["title"] = records[i][0];
+ doc["points"] = i;
+
+ ASSERT_TRUE(coll1->add(doc.dump()).ok());
+ }
+
+ auto results = coll1->search("roti", {"title"}, "", {}, {}, 0, 3, 1, FREQUENCY, true, 5).get();
+
+ ASSERT_EQ(4, results["found"].get<size_t>());
+ ASSERT_EQ(3, results["hits"].size());
+
+ ASSERT_STREQ("2", results["hits"][0]["document"]["id"].get<std::string>().c_str());
+ ASSERT_STREQ("1", results["hits"][1]["document"]["id"].get<std::string>().c_str());
+ ASSERT_STREQ("3", results["hits"][2]["document"]["id"].get<std::string>().c_str());
+
+ collectionManager.drop_collection("coll1");
+}
+
TEST_F(CollectionTest, HighlightWithAccentedCharacters) {
Collection *coll1;