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

Fix max_candidates behavior.

Kishore Nallan0e2257dc2977
diff --git a/include/index.h b/include/index.h
index 27aa6a16..eb8c0316 100644
--- a/include/index.h
+++ b/include/index.h
@@ -583,6 +583,7 @@ private:
const std::vector<bool>& prefixes,
bool prioritize_exact_match,
const bool exhaustive_search,
+ const size_t max_candidates,
int syn_orig_num_tokens,
const int* sort_order,
std::array<spp::sparse_hash_map<uint32_t, int64_t>*, 3>& field_values,
diff --git a/src/collection_manager.cpp b/src/collection_manager.cpp
index 2adbc7b2..aa7b0ddd 100644
--- a/src/collection_manager.cpp
+++ b/src/collection_manager.cpp
@@ -866,7 +866,7 @@ Option<bool> CollectionManager::do_search(std::map<std::string, std::string>& re
}
if(!max_candidates) {
- max_candidates = exhaustive_search ? 10000 : 4;
+ max_candidates = exhaustive_search ? Index::COMBINATION_MAX_LIMIT : Index::COMBINATION_MIN_LIMIT;
}
Option<nlohmann::json> result_op = collection->search(raw_query, search_fields, simple_filter_query, facet_fields,
diff --git a/src/index.cpp b/src/index.cpp
index 03a928fe..3580fe30 100644
--- a/src/index.cpp
+++ b/src/index.cpp
@@ -1156,6 +1156,7 @@ void Index::search_all_candidates(const size_t num_search_fields,
const std::vector<bool>& prefixes,
bool prioritize_exact_match,
const bool exhaustive_search,
+ const size_t max_candidates,
int syn_orig_num_tokens,
const int* sort_order,
std::array<spp::sparse_hash_map<uint32_t, int64_t>*, 3>& field_values,
@@ -1166,7 +1167,7 @@ void Index::search_all_candidates(const size_t num_search_fields,
auto product = []( long long a, tok_candidates & b ) { return a*b.candidates.size(); };
long long int N = std::accumulate(token_candidates_vec.begin(), token_candidates_vec.end(), 1LL, product);
- long long combination_limit = exhaustive_search ? Index::COMBINATION_MAX_LIMIT : Index::COMBINATION_MIN_LIMIT;
+ long long combination_limit = std::max<size_t>(Index::COMBINATION_MIN_LIMIT, max_candidates);
for(long long n = 0; n < N && n < combination_limit; ++n) {
RETURN_CIRCUIT_BREAKER
@@ -2647,7 +2648,7 @@ void Index::fuzzy_search_fields(const std::vector<search_field_t>& the_fields,
long long n = 0;
long long int N = std::accumulate(token_to_costs.begin(), token_to_costs.end(), 1LL, product);
- const long long combination_limit = exhaustive_search ? Index::COMBINATION_MAX_LIMIT : Index::COMBINATION_MIN_LIMIT;
+ const long long combination_limit = std::max<size_t>(Index::COMBINATION_MIN_LIMIT, max_candidates);
while(n < N && n < combination_limit) {
RETURN_CIRCUIT_BREAKER
@@ -2764,7 +2765,7 @@ void Index::fuzzy_search_fields(const std::vector<search_field_t>& the_fields,
sort_fields, token_candidates_vec, searched_queries, qtoken_set, topster,
groups_processed, all_result_ids, all_result_ids_len,
typo_tokens_threshold, group_limit, group_by_fields, query_tokens,
- num_typos, prefixes, prioritize_exact_match, exhaustive_search,
+ num_typos, prefixes, prioritize_exact_match, exhaustive_search, max_candidates,
syn_orig_num_tokens, sort_order, field_values, geopoint_indices,
query_hashes, id_buff);
diff --git a/test/collection_specific_more_test.cpp b/test/collection_specific_more_test.cpp
new file mode 100644
index 00000000..c75859fc
--- /dev/null
+++ b/test/collection_specific_more_test.cpp
@@ -0,0 +1,57 @@
+#include <gtest/gtest.h>
+#include <string>
+#include <vector>
+#include <fstream>
+#include <algorithm>
+#include <collection_manager.h>
+#include "collection.h"
+
+class CollectionSpecificMoreTest : public ::testing::Test {
+protected:
+ Store *store;
+ CollectionManager & collectionManager = CollectionManager::get_instance();
+ std::atomic<bool> quit = false;
+
+ std::vector<std::string> query_fields;
+ std::vector<sort_by> sort_fields;
+
+ void setupCollection() {
+ std::string state_dir_path = "/tmp/typesense_test/collection_specific_more";
+ LOG(INFO) << "Truncating and creating: " << state_dir_path;
+ system(("rm -rf "+state_dir_path+" && mkdir -p "+state_dir_path).c_str());
+
+ store = new Store(state_dir_path);
+ collectionManager.init(store, 1.0, "auth_key", quit);
+ collectionManager.load(8, 1000);
+ }
+
+ virtual void SetUp() {
+ setupCollection();
+ }
+
+ virtual void TearDown() {
+ collectionManager.dispose();
+ delete store;
+ }
+};
+
+TEST_F(CollectionSpecificMoreTest, MaxCandidatesShouldBeRespected) {
+ std::vector<field> fields = {field("company", field_types::STRING, true)};
+ Collection* coll1 = collectionManager.create_collection("coll1", 1, fields).get();
+
+ for (size_t i = 0; i < 200; i++) {
+ nlohmann::json doc;
+ doc["id"] = std::to_string(i);
+ doc["company"] = "prefix"+std::to_string(i);
+ ASSERT_TRUE(coll1->add(doc.dump()).ok());
+ }
+
+ auto results = coll1->search("prefix", {"company"}, "", {}, {}, {0}, 10, 1, FREQUENCY, {true}, 0,
+ spp::sparse_hash_set<std::string>(),
+ spp::sparse_hash_set<std::string>(), 10, "", 30, 4, "title", 20, {}, {}, {}, 0,
+ "<mark>", "</mark>", {}, 1000, true, false, true, "", false, 6000 * 1000, 4, 7,
+ false, 1000).get();
+
+ ASSERT_EQ(200, results["found"].get<size_t>());
+ collectionManager.drop_collection("coll1");
+}