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

Add both sides drop tokens mode.

Kishore Nallan40a684619b29
diff --git a/include/collection.h b/include/collection.h
index 35cefee1..1a4780f2 100644
--- a/include/collection.h
+++ b/include/collection.h
@@ -201,6 +201,8 @@ private:
static Option<bool> parse_pinned_hits(const std::string& pinned_hits_str,
std::map<size_t, std::vector<std::string>>& pinned_hits);
+ static Option<drop_tokens_param_t> parse_drop_tokens_mode(const std::string& drop_tokens_mode);
+
Index* init_index();
static std::vector<char> to_char_array(const std::vector<std::string>& strs);
@@ -474,7 +476,7 @@ public:
const size_t remote_embedding_num_tries = 2,
const bool prioritize_num_matching_fields = true,
const bool group_missing_values = true,
- const drop_tokens_mode_t drop_tokens_mode = right_to_left) const;
+ const std::string& drop_tokens_mode = "right_to_left") const;
Option<bool> get_filter_ids(const std::string & filter_query, filter_result_t& filter_result) const;
diff --git a/include/index.h b/include/index.h
index b3487b39..ad38e00e 100644
--- a/include/index.h
+++ b/include/index.h
@@ -99,6 +99,18 @@ enum text_match_type_t {
enum drop_tokens_mode_t {
left_to_right,
right_to_left,
+ both_sides,
+};
+
+struct drop_tokens_param_t {
+ drop_tokens_mode_t mode = right_to_left;
+ size_t token_limit = 1000;
+
+ drop_tokens_param_t() {
+
+ }
+
+ drop_tokens_param_t(drop_tokens_mode_t mode, size_t token_limit) : mode(mode), token_limit(token_limit) {}
};
struct search_args {
@@ -151,7 +163,7 @@ struct search_args {
vector_query_t& vector_query;
size_t facet_sample_percent;
size_t facet_sample_threshold;
- drop_tokens_mode_t drop_tokens_mode;
+ drop_tokens_param_t drop_tokens_mode;
search_args(std::vector<query_tokens_t> field_query_tokens, std::vector<search_field_t> search_fields,
const text_match_type_t match_type,
@@ -168,7 +180,7 @@ struct search_args {
size_t min_len_1typo, size_t min_len_2typo, size_t max_candidates, const std::vector<enable_t>& infixes,
const size_t max_extra_prefix, const size_t max_extra_suffix, const size_t facet_query_num_typos,
const bool filter_curated_hits, const enable_t split_join_tokens, vector_query_t& vector_query,
- size_t facet_sample_percent, size_t facet_sample_threshold, drop_tokens_mode_t drop_tokens_mode) :
+ size_t facet_sample_percent, size_t facet_sample_threshold, drop_tokens_param_t drop_tokens_mode) :
field_query_tokens(field_query_tokens),
search_fields(search_fields), match_type(match_type), filter_tree_root(filter_tree_root), facets(facets),
included_ids(included_ids), excluded_ids(excluded_ids), sort_fields_std(sort_fields_std),
@@ -687,7 +699,7 @@ public:
const bool filter_curated_hits, enable_t split_join_tokens,
const vector_query_t& vector_query, size_t facet_sample_percent, size_t facet_sample_threshold,
const std::string& collection_name,
- const drop_tokens_mode_t drop_tokens_mode = right_to_left) const;
+ const drop_tokens_param_t drop_tokens_mode) const;
void remove_field(uint32_t seq_id, const nlohmann::json& document, const std::string& field_name,
const bool is_update);
diff --git a/src/collection.cpp b/src/collection.cpp
index eb9dee36..27275a4b 100644
--- a/src/collection.cpp
+++ b/src/collection.cpp
@@ -1117,7 +1117,7 @@ Option<nlohmann::json> Collection::search(std::string raw_query,
const size_t remote_embedding_num_tries,
const bool prioritize_num_matching_fields,
const bool group_missing_values,
- const drop_tokens_mode_t drop_tokens_mode) const {
+ const std::string& drop_tokens_mode) const {
std::shared_lock lock(mutex);
@@ -1475,6 +1475,13 @@ Option<nlohmann::json> Collection::search(std::string raw_query,
}
}
+ Option<drop_tokens_param_t> drop_tokens_param_op = parse_drop_tokens_mode(drop_tokens_mode);
+ if(!drop_tokens_param_op.ok()) {
+ return Option<nlohmann::json>(drop_tokens_param_op.code(), drop_tokens_param_op.error());
+ }
+
+ auto drop_tokens_param = drop_tokens_param_op.get();
+
std::vector<std::vector<KV*>> raw_result_kvs;
std::vector<std::vector<KV*>> override_result_kvs;
@@ -1632,7 +1639,7 @@ Option<nlohmann::json> Collection::search(std::string raw_query,
min_len_1typo, min_len_2typo, max_candidates, infixes,
max_extra_prefix, max_extra_suffix, facet_query_num_typos,
filter_curated_hits, split_join_tokens, vector_query,
- facet_sample_percent, facet_sample_threshold, drop_tokens_mode);
+ facet_sample_percent, facet_sample_threshold, drop_tokens_param);
std::unique_ptr<search_args> search_params_guard(search_params);
@@ -3687,6 +3694,35 @@ Option<bool> Collection::parse_pinned_hits(const std::string& pinned_hits_str,
return Option<bool>(true);
}
+Option<drop_tokens_param_t> Collection::parse_drop_tokens_mode(const std::string& drop_tokens_mode) {
+ drop_tokens_mode_t drop_tokens_mode_val = left_to_right;
+ size_t drop_tokens_token_limit = 1000;
+ auto drop_tokens_mode_op = magic_enum::enum_cast<drop_tokens_mode_t>(drop_tokens_mode);
+ if(drop_tokens_mode_op.has_value()) {
+ drop_tokens_mode_val = drop_tokens_mode_op.value();
+ } else {
+ std::vector<std::string> drop_token_parts;
+ StringUtils::split(drop_tokens_mode, drop_token_parts, ":");
+ if(drop_token_parts.size() == 2) {
+ if(!StringUtils::is_uint32_t(drop_token_parts[1])) {
+ return Option<drop_tokens_param_t>(400, "Invalid format for drop tokens mode.");
+ }
+
+ drop_tokens_mode_op = magic_enum::enum_cast<drop_tokens_mode_t>(drop_token_parts[0]);
+ if(drop_tokens_mode_op.has_value()) {
+ drop_tokens_mode_val = drop_tokens_mode_op.value();
+ }
+
+ drop_tokens_token_limit = std::stoul(drop_token_parts[1]);
+
+ } else {
+ return Option<drop_tokens_param_t>(400, "Invalid format for drop tokens mode.");
+ }
+ }
+
+ return Option<drop_tokens_param_t>(drop_tokens_param_t(drop_tokens_mode_val, drop_tokens_token_limit));
+}
+
Option<bool> Collection::add_synonym(const nlohmann::json& syn_json, bool write_to_store) {
std::shared_lock lock(mutex);
synonym_t synonym;
diff --git a/src/collection_manager.cpp b/src/collection_manager.cpp
index 5ad7d814..9019eabb 100644
--- a/src/collection_manager.cpp
+++ b/src/collection_manager.cpp
@@ -1072,12 +1072,6 @@ Option<bool> CollectionManager::do_search(std::map<std::string, std::string>& re
Index::NUM_CANDIDATES_DEFAULT_MIN);
}
- auto drop_tokens_mode_op = magic_enum::enum_cast<drop_tokens_mode_t>(drop_tokens_mode_str);
- drop_tokens_mode_t drop_tokens_mode;
- if(drop_tokens_mode_op.has_value()) {
- drop_tokens_mode = drop_tokens_mode_op.value();
- }
-
Option<nlohmann::json> result_op = collection->search(raw_query, search_fields, simple_filter_query, facet_fields,
sort_fields, num_typos,
per_page,
@@ -1125,7 +1119,7 @@ Option<bool> CollectionManager::do_search(std::map<std::string, std::string>& re
remote_embedding_num_tries,
prioritize_num_matching_fields,
group_missing_values,
- drop_tokens_mode);
+ drop_tokens_mode_str);
uint64_t timeMillis = std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::high_resolution_clock::now() - begin).count();
diff --git a/src/index.cpp b/src/index.cpp
index f22f4874..71f3e829 100644
--- a/src/index.cpp
+++ b/src/index.cpp
@@ -2759,7 +2759,7 @@ Option<bool> Index::search(std::vector<query_tokens_t>& field_query_tokens, cons
const vector_query_t& vector_query,
size_t facet_sample_percent, size_t facet_sample_threshold,
const std::string& collection_name,
- const drop_tokens_mode_t drop_tokens_mode) const {
+ const drop_tokens_param_t drop_tokens_mode) const {
std::shared_lock lock(mutex);
uint32_t filter_ids_length = 0;
@@ -3102,10 +3102,22 @@ Option<bool> Index::search(std::vector<query_tokens_t>& field_query_tokens, cons
for (size_t qi = 0; qi < all_queries.size(); qi++) {
auto& orig_tokens = all_queries[qi];
size_t num_tokens_dropped = 0;
- auto curr_direction = drop_tokens_mode;
size_t total_dirs_done = 0;
- while(exhaustive_search || all_result_ids_len < drop_tokens_threshold) {
+ // NOTE: when dropping both sides we will ignore exhaustive search
+
+ auto curr_direction = drop_tokens_mode.mode;
+ bool drop_both_sides = false;
+
+ if(drop_tokens_mode.mode == both_sides) {
+ if(orig_tokens.size() <= drop_tokens_mode.token_limit) {
+ drop_both_sides = true;
+ } else {
+ curr_direction = right_to_left;
+ }
+ }
+
+ while(exhaustive_search || all_result_ids_len < drop_tokens_threshold || drop_both_sides) {
// When atleast two tokens from the query are available we can drop one
std::vector<token_t> truncated_tokens;
std::vector<token_t> dropped_tokens;
diff --git a/test/collection_specific_more_test.cpp b/test/collection_specific_more_test.cpp
index fc5468eb..86d0de41 100644
--- a/test/collection_specific_more_test.cpp
+++ b/test/collection_specific_more_test.cpp
@@ -2403,7 +2403,7 @@ TEST_F(CollectionSpecificMoreTest, DropTokensLeftToRightFirst) {
spp::sparse_hash_set<std::string>(), 10, "", 30, 4, "title", 20, {}, {}, {}, 0,
"<mark>", "</mark>", {}, 1000, true, false, true, "", false, 10000,
4, 7, fallback, 4, {off}, 100, 100, 2, 2, false, "", true, 0, max_score, 100, 0,
- 0, 30000, 2, true, true, left_to_right).get();
+ 0, 30000, 2, true, true, "left_to_right").get();
ASSERT_EQ(1, res["hits"].size());
ASSERT_EQ("1", res["hits"][0]["document"]["id"].get<std::string>());
@@ -2413,10 +2413,48 @@ TEST_F(CollectionSpecificMoreTest, DropTokensLeftToRightFirst) {
spp::sparse_hash_set<std::string>(), 10, "", 30, 4, "title", 20, {}, {}, {}, 0,
"<mark>", "</mark>", {}, 1000, true, false, true, "", false, 10000,
4, 7, fallback, 4, {off}, 100, 100, 2, 2, false, "", true, 0, max_score, 100, 0,
- 0, 30000, 2, true, true, right_to_left).get();
+ 0, 30000, 2, true, true, "right_to_left").get();
ASSERT_EQ(1, res["hits"].size());
ASSERT_EQ("0", res["hits"][0]["document"]["id"].get<std::string>());
+
+ // search on both sides
+ res = coll1->search("alpha gamma", {"title"}, "", {}, {}, {0}, 3, 1, FREQUENCY, {false}, drop_tokens_threshold,
+ 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, 10000,
+ 4, 7, fallback, 4, {off}, 100, 100, 2, 2, false, "", true, 0, max_score, 100, 0,
+ 0, 30000, 2, true, true, "both_sides:3").get();
+ ASSERT_EQ(2, res["hits"].size());
+
+ // but must follow token limit
+ res = coll1->search("alpha gamma", {"title"}, "", {}, {}, {0}, 3, 1, FREQUENCY, {false}, drop_tokens_threshold,
+ 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, 10000,
+ 4, 7, fallback, 4, {off}, 100, 100, 2, 2, false, "", true, 0, max_score, 100, 0,
+ 0, 30000, 2, true, true, "both_sides:1").get();
+ ASSERT_EQ(1, res["hits"].size());
+ ASSERT_EQ("0", res["hits"][0]["document"]["id"].get<std::string>());
+
+ // validation checks
+ auto res_op = coll1->search("alpha gamma", {"title"}, "", {}, {}, {0}, 3, 1, FREQUENCY, {false}, drop_tokens_threshold,
+ 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, 10000,
+ 4, 7, fallback, 4, {off}, 100, 100, 2, 2, false, "", true, 0, max_score, 100, 0,
+ 0, 30000, 2, true, true, "all_sides");
+ ASSERT_FALSE(res_op.ok());
+ ASSERT_EQ("Invalid format for drop tokens mode.", res_op.error());
+
+ res_op = coll1->search("alpha gamma", {"title"}, "", {}, {}, {0}, 3, 1, FREQUENCY, {false}, drop_tokens_threshold,
+ 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, 10000,
+ 4, 7, fallback, 4, {off}, 100, 100, 2, 2, false, "", true, 0, max_score, 100, 0,
+ 0, 30000, 2, true, true, "both_sides:x");
+ ASSERT_FALSE(res_op.ok());
+ ASSERT_EQ("Invalid format for drop tokens mode.", res_op.error());
}
TEST_F(CollectionSpecificMoreTest, DoNotHighlightFieldsForSpecialCharacterQuery) {