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

Limit number of prefix candidates to a constant.

Kishore Nallana7479171b1ed
diff --git a/TODO.md b/TODO.md
index 44b2f0d2..2b18da8a 100644
--- a/TODO.md
+++ b/TODO.md
@@ -73,6 +73,7 @@
- ~~during index_in_memory() validations should be front loaded~~
- ~~Support token ranking field being a float~~
- get collection should show schema
+- highlight of string arrays?
- NOT operator support
- test for token ranking on float field
- test for float int field deletion during doc deletion
diff --git a/src/api.cpp b/src/api.cpp
index 6f05d123..cb7a3dc1 100644
--- a/src/api.cpp
+++ b/src/api.cpp
@@ -207,6 +207,7 @@ void get_search(http_req & req, http_res & res) {
nlohmann::json result = result_op.get();
result["took_ms"] = timeMillis;
+ result["page"] = std::stoi(req.params[PAGE]);
const std::string & results_json_str = result.dump();
//struct rusage r_usage;
diff --git a/src/collection.cpp b/src/collection.cpp
index 89bfb340..a77e2806 100644
--- a/src/collection.cpp
+++ b/src/collection.cpp
@@ -460,6 +460,8 @@ void Collection::search_candidates(uint32_t* filter_ids, size_t filter_ids_lengt
size_t & total_results, uint32_t** all_result_ids, size_t & all_result_ids_len,
const size_t & max_results, const bool prefix) {
const size_t combination_limit = 10;
+ const size_t prefix_candidates_limit = 5;
+
auto product = []( long long a, std::vector<art_leaf*>& b ) { return a*b.size(); };
long long int N = std::accumulate(token_to_candidates.begin(), token_to_candidates.end(), 1LL, product);
@@ -531,7 +533,7 @@ void Collection::search_candidates(uint32_t* filter_ids, size_t filter_ids_lengt
break;
}
- if(prefix && candidate_rank >= max_results) {
+ if(prefix && candidate_rank >= prefix_candidates_limit) {
break;
}
}
diff --git a/test/collection_test.cpp b/test/collection_test.cpp
index f835fff6..233e3888 100644
--- a/test/collection_test.cpp
+++ b/test/collection_test.cpp
@@ -440,7 +440,7 @@ TEST_F(CollectionTest, PrefixSearching) {
results = collection->search("t", query_fields, "", facets, sort_fields, 0, 2, 1, FREQUENCY, true).get();
ASSERT_EQ(2, results["hits"].size());
- ids = {"1", "6"};
+ ids = {"1", "foo"};
for(size_t i = 0; i < results["hits"].size(); i++) {
nlohmann::json result = results["hits"].at(i);