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

Parameterize the num_typos from the query end-point.

Kishore Nallanc25f7ccfdb38
diff --git a/src/art.cpp b/src/art.cpp
index 9f3ff098..3ca14e2d 100644
--- a/src/art.cpp
+++ b/src/art.cpp
@@ -1235,7 +1235,8 @@ static void art_fuzzy_recurse(char c, const art_node *n, int depth, const unsign
depth++;
}
- if(cost <= max_cost) {
+ // current_row[columns-1] holds the final cost
+ if(current_row[columns-1] <= max_cost) {
results.push_back(n);
}
@@ -1257,7 +1258,7 @@ static void art_fuzzy_recurse(char c, const art_node *n, int depth, const unsign
depth += n->partial_len;
printf("\ncost: %d", cost);
- if(depth >= term_len-1 && cost <= max_cost) {
+ if(depth >= term_len-1 && current_row[columns-1] <= max_cost) {
results.push_back(n);
return ;
}
diff --git a/src/collection.cpp b/src/collection.cpp
index 21b2083c..426da486 100644
--- a/src/collection.cpp
+++ b/src/collection.cpp
@@ -9,7 +9,7 @@
#include "sole.hpp"
#include "json.hpp"
-Collection::Collection(std::string state_dir_path) {
+Collection::Collection(std::string state_dir_path): seq_id(0) {
store = new Store(state_dir_path);
art_tree_init(&t);
}
@@ -85,14 +85,17 @@ void Collection::add(std::string json_str) {
4. Intersect the lists to find docs that match each phrase
5. Sort the docs based on some ranking criteria
*/
-std::vector<nlohmann::json> Collection::search(std::string query, size_t max_results) {
+std::vector<nlohmann::json> Collection::search(std::string query, const int num_typos, const size_t max_results) {
std::vector<std::string> tokens;
StringUtils::tokenize(query, tokens, " ", true);
+ const int max_cost = (num_typos < 0 || num_typos > 2) ? 2 : num_typos;
+
+ std::cout << "Searching with max_cost=" << max_cost << std::endl;
+
std::vector<std::vector<art_leaf*>> token_leaves;
for(std::string token: tokens) {
std::vector<art_leaf*> leaves;
- int max_cost = 2;
art_fuzzy_results(&t, (const unsigned char *) token.c_str(), (int) token.length() + 1, max_cost, 10, leaves);
if(!leaves.empty()) {
for(auto i=0; i<leaves.size(); i++) {
diff --git a/src/collection.h b/src/collection.h
index 7e0ef291..55be6b2c 100644
--- a/src/collection.h
+++ b/src/collection.h
@@ -25,7 +25,7 @@ public:
Collection(std::string state_dir_path);
~Collection();
void add(std::string json_str);
- std::vector<nlohmann::json> search(std::string query, size_t max_results);
+ std::vector<nlohmann::json> search(std::string query, const int num_typos, const size_t max_results);
static inline std::vector<art_leaf *> _next_suggestion(const std::vector<std::vector<art_leaf *>> &token_leaves,
long long int n);
diff --git a/src/main.cpp b/src/main.cpp
index 71ff72fc..84079b58 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -26,7 +26,7 @@ int main() {
cout << "FINISHED INDEXING!" << endl << flush;
auto begin = std::chrono::high_resolution_clock::now();
- collection->search("plant", 100);
+ collection->search("plant", 2, 100);
long long int timeMillis = std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::high_resolution_clock::now() - begin).count();
cout << "Time taken: " << timeMillis << "us" << endl;
delete collection;
diff --git a/src/server.cpp b/src/server.cpp
index a67a2cb0..6a323a3a 100644
--- a/src/server.cpp
+++ b/src/server.cpp
@@ -65,12 +65,16 @@ static int chunked_test(h2o_handler_t *self, h2o_req_t *req) {
std::string query_str(query.base, query.len);
std::map<std::string, std::string> query_map = parse_query(query_str);
+ const char *NUM_TYPOS = "num_typos";
- printf("Query: %s\n", query_map["q"].c_str());
+ if(query_map.count(NUM_TYPOS) == 0) {
+ query_map[NUM_TYPOS] = "2";
+ }
+ printf("Query: %s\n", query_map["q"].c_str());
auto begin = std::chrono::high_resolution_clock::now();
- std::vector<nlohmann::json> results = collection->search(query_map["q"], 100);
+ std::vector<nlohmann::json> results = collection->search(query_map["q"], std::stoi(query_map[NUM_TYPOS]), 100);
nlohmann::json json_array = nlohmann::json::array();
for(nlohmann::json& result: results) {
json_array.push_back(result);