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
C++ algoliadatastoreelasticsearchenterprise-searchfacetingfull-text-searchfuzzy-searchgeosearchin-memoryinstantsearchmerchandisingpineconesearchsearch-enginesemantic-searchsimilarity-searchsite-searchsynonymstypo-tolerancevector-search
Add flag prioritize_num_matching_fields.
Kishore Nallan4dd35835e399
diff --git a/include/collection.h b/include/collection.hindex e9ca2b2a..38187eeb 100644--- a/include/collection.h+++ b/include/collection.h@@ -467,7 +467,8 @@ public:const size_t facet_sample_threshold = 0,const size_t page_offset = 0,const size_t remote_embedding_timeout_ms = 30000,- const size_t remote_embedding_num_tries = 2) const;+ const size_t remote_embedding_num_tries = 2,+ const bool prioritize_num_matching_fields = true) 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.hindex 66cf3365..8e165e74 100644--- a/include/index.h+++ b/include/index.h@@ -119,6 +119,7 @@ struct search_args {std::string default_sorting_field;bool prioritize_exact_match;bool prioritize_token_position;+ bool prioritize_num_matching_fields;size_t all_result_ids_len;bool exhaustive_search;size_t concurrency;@@ -154,7 +155,7 @@ struct search_args {const std::vector<bool>& prefixes, size_t drop_tokens_threshold, size_t typo_tokens_threshold,const std::vector<std::string>& group_by_fields, size_t group_limit,const string& default_sorting_field, bool prioritize_exact_match,- const bool prioritize_token_position, bool exhaustive_search,+ const bool prioritize_token_position, const bool prioritize_num_matching_fields, bool exhaustive_search,size_t concurrency, size_t search_cutoff_ms,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,@@ -168,6 +169,7 @@ struct search_args {drop_tokens_threshold(drop_tokens_threshold), typo_tokens_threshold(typo_tokens_threshold),group_by_fields(group_by_fields), group_limit(group_limit), default_sorting_field(default_sorting_field),prioritize_exact_match(prioritize_exact_match), prioritize_token_position(prioritize_token_position),+ prioritize_num_matching_fields(prioritize_num_matching_fields),all_result_ids_len(0), exhaustive_search(exhaustive_search), concurrency(concurrency),search_cutoff_ms(search_cutoff_ms),min_len_1typo(min_len_1typo), min_len_2typo(min_len_2typo), max_candidates(max_candidates),@@ -436,6 +438,7 @@ private:bool prioritize_exact_match,const bool prioritize_token_position,const bool exhaustive_search,+ const bool prioritize_num_matching_fields,const size_t max_candidates,int syn_orig_num_tokens,const int* sort_order,@@ -657,7 +660,8 @@ public:const size_t typo_tokens_threshold, const size_t group_limit,const std::vector<std::string>& group_by_fields,const string& default_sorting_field, bool prioritize_exact_match,- const bool prioritize_token_position, bool exhaustive_search,+ const bool prioritize_token_position, const bool prioritize_num_matching_fields,+ bool exhaustive_search,size_t concurrency, size_t search_cutoff_ms, 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,@@ -802,6 +806,7 @@ public:const size_t typo_tokens_threshold, const size_t group_limit,const std::vector<std::string>& group_by_fields, bool prioritize_exact_match,const bool prioritize_token_position,+ const bool prioritize_num_matching_fields,const bool exhaustive_search, const size_t concurrency,const std::vector<bool>& prefixes,size_t min_len_1typo,@@ -860,6 +865,7 @@ public:const size_t group_limit, const std::vector<std::string>& group_by_fields,bool prioritize_exact_match,const bool prioritize_token_position,+ const bool prioritize_num_matching_fields,std::set<uint64>& query_hashes,const token_ordering token_order,const std::vector<bool>& prefixes,@@ -899,6 +905,7 @@ public:const std::vector<std::string>& group_by_fields,bool prioritize_exact_match,const bool search_all_candidates,+ const bool prioritize_num_matching_fields,const uint32_t* filter_ids, uint32_t filter_ids_length,const uint32_t total_cost,const int syn_orig_num_tokens,diff --git a/src/collection.cpp b/src/collection.cppindex 28907b33..c0b3e6f3 100644--- a/src/collection.cpp+++ b/src/collection.cpp@@ -1114,7 +1114,8 @@ Option<nlohmann::json> Collection::search(std::string raw_query,const size_t facet_sample_threshold,const size_t page_offset,const size_t remote_embedding_timeout_ms,- const size_t remote_embedding_num_tries) const {+ const size_t remote_embedding_num_tries,+ const bool prioritize_num_matching_fields) const {std::shared_lock lock(mutex);@@ -1608,6 +1609,7 @@ Option<nlohmann::json> Collection::search(std::string raw_query,drop_tokens_threshold, typo_tokens_threshold,group_by_fields, group_limit, default_sorting_field,prioritize_exact_match, prioritize_token_position,+ prioritize_num_matching_fields,exhaustive_search, 4,search_stop_millis,min_len_1typo, min_len_2typo, max_candidates, infixes,diff --git a/src/index.cpp b/src/index.cppindex de4b38f9..e2eb5c99 100644--- a/src/index.cpp+++ b/src/index.cpp@@ -1316,6 +1316,7 @@ void Index::search_all_candidates(const size_t num_search_fields,const std::vector<bool>& prefixes,bool prioritize_exact_match,const bool prioritize_token_position,+ const bool prioritize_num_matching_fields,const bool exhaustive_search,const size_t max_candidates,int syn_orig_num_tokens,@@ -1365,7 +1366,7 @@ void Index::search_all_candidates(const size_t num_search_fields,search_across_fields(query_suggestion, num_typos, prefixes, the_fields, num_search_fields, match_type,sort_fields, topster,groups_processed,searched_queries, qtoken_set, dropped_tokens, group_limit, group_by_fields,- prioritize_exact_match, prioritize_token_position,+ prioritize_exact_match, prioritize_token_position, prioritize_num_matching_fields,filter_ids, filter_ids_length, total_cost, syn_orig_num_tokens,exclude_token_ids, exclude_token_ids_size, excluded_group_ids,sort_order, field_values, geopoint_indices,@@ -2274,6 +2275,7 @@ Option<bool> Index::run_search(search_args* search_params, const std::string& cosearch_params->default_sorting_field,search_params->prioritize_exact_match,search_params->prioritize_token_position,+ search_params->prioritize_num_matching_fields,search_params->exhaustive_search,search_params->concurrency,search_params->search_cutoff_ms,@@ -2733,7 +2735,7 @@ Option<bool> Index::search(std::vector<query_tokens_t>& field_query_tokens, consconst size_t typo_tokens_threshold, const size_t group_limit,const std::vector<std::string>& group_by_fields,const string& default_sorting_field, bool prioritize_exact_match,- const bool prioritize_token_position, bool exhaustive_search,+ const bool prioritize_token_position, const bool prioritize_num_matching_fields, bool exhaustive_search,size_t concurrency, size_t search_cutoff_ms, 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,@@ -3014,7 +3016,7 @@ Option<bool> Index::search(std::vector<query_tokens_t>& field_query_tokens, consexcluded_group_ids,sort_fields_std, num_typos, searched_queries, qtoken_set, topster, groups_processed,all_result_ids, all_result_ids_len, group_limit, group_by_fields, prioritize_exact_match,- prioritize_token_position, query_hashes, token_order, prefixes,+ prioritize_token_position, prioritize_num_matching_fields, query_hashes, token_order, prefixes,typo_tokens_threshold, exhaustive_search,max_candidates, min_len_1typo, min_len_2typo, syn_orig_num_tokens, sort_order,field_values, geopoint_indices);@@ -3052,8 +3054,10 @@ Option<bool> Index::search(std::vector<query_tokens_t>& field_query_tokens, consexcluded_group_ids,sort_fields_std, num_typos, searched_queries, qtoken_set, topster, groups_processed,all_result_ids, all_result_ids_len, group_limit, group_by_fields, prioritize_exact_match,- prioritize_token_position, query_hashes, token_order, prefixes, typo_tokens_threshold, exhaustive_search,- max_candidates, min_len_1typo, min_len_2typo, syn_orig_num_tokens, sort_order, field_values, geopoint_indices);+ prioritize_token_position, prioritize_num_matching_fields,+ query_hashes, token_order, prefixes, typo_tokens_threshold, exhaustive_search,+ max_candidates, min_len_1typo, min_len_2typo, syn_orig_num_tokens, sort_order,+ field_values, geopoint_indices);}}@@ -3061,6 +3065,7 @@ Option<bool> Index::search(std::vector<query_tokens_t>& field_query_tokens, consdo_synonym_search(the_fields, match_type, filter_tree_root, included_ids_map, sort_fields_std,curated_topster, token_order,0, group_limit, group_by_fields, prioritize_exact_match, prioritize_token_position,+ prioritize_num_matching_fields,exhaustive_search, concurrency, prefixes,min_len_1typo, min_len_2typo, max_candidates, curated_ids, curated_ids_sorted,excluded_result_ids, excluded_result_ids_size, excluded_group_ids,@@ -3121,8 +3126,8 @@ Option<bool> Index::search(std::vector<query_tokens_t>& field_query_tokens, conscurated_ids_sorted, excluded_group_ids, sort_fields_std, num_typos, searched_queries,qtoken_set, topster, groups_processed,all_result_ids, all_result_ids_len, group_limit, group_by_fields,- prioritize_exact_match, prioritize_token_position, query_hashes,- token_order, prefixes, typo_tokens_threshold,+ prioritize_exact_match, prioritize_token_position, prioritize_num_matching_fields,+ query_hashes, token_order, prefixes, typo_tokens_threshold,exhaustive_search, max_candidates, min_len_1typo,min_len_2typo, -1, sort_order, field_values, geopoint_indices);@@ -3528,6 +3533,7 @@ void Index::fuzzy_search_fields(const std::vector<search_field_t>& the_fields,const size_t group_limit, const std::vector<std::string>& group_by_fields,bool prioritize_exact_match,const bool prioritize_token_position,+ const bool prioritize_num_matching_fields,std::set<uint64>& query_hashes,const token_ordering token_order,const std::vector<bool>& prefixes,@@ -3773,7 +3779,7 @@ void Index::fuzzy_search_fields(const std::vector<search_field_t>& the_fields,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, prioritize_token_position,- exhaustive_search, max_candidates,+ prioritize_num_matching_fields, exhaustive_search, max_candidates,syn_orig_num_tokens, sort_order, field_values, geopoint_indices,query_hashes, id_buff);@@ -3928,6 +3934,7 @@ void Index::search_across_fields(const std::vector<token_t>& query_tokens,const std::vector<std::string>& group_by_fields,const bool prioritize_exact_match,const bool prioritize_token_position,+ const bool prioritize_num_matching_fields,const uint32_t* filter_ids, uint32_t filter_ids_length,const uint32_t total_cost, const int syn_orig_num_tokens,const uint32_t* exclude_token_ids, size_t exclude_token_ids_size,@@ -4163,6 +4170,10 @@ void Index::search_across_fields(const std::vector<token_t>& query_tokens,auto max_field_weight = std::min<size_t>(FIELD_MAX_WEIGHT, best_field_weight);num_matching_fields = std::min<size_t>(7, num_matching_fields);+ if(!prioritize_num_matching_fields) {+ num_matching_fields = 0;+ }+uint64_t aggregated_score = match_type == max_score ?((int64_t(query_len) << 59) |(int64_t(best_field_match_score) << 11) |@@ -4641,6 +4652,7 @@ void Index::do_synonym_search(const std::vector<search_field_t>& the_fields,const size_t typo_tokens_threshold, const size_t group_limit,const std::vector<std::string>& group_by_fields, bool prioritize_exact_match,const bool prioritize_token_position,+ const bool prioritize_num_matching_fields,const bool exhaustive_search, const size_t concurrency,const std::vector<bool>& prefixes,size_t min_len_1typo,@@ -4667,7 +4679,8 @@ void Index::do_synonym_search(const std::vector<search_field_t>& the_fields,exclude_token_ids_size, filter_ids, filter_ids_length, curated_ids_sorted, excluded_group_ids,sort_fields_std, {0}, searched_queries, qtoken_set, actual_topster, groups_processed,all_result_ids, all_result_ids_len, group_limit, group_by_fields, prioritize_exact_match,- prioritize_token_position, query_hashes, token_order, prefixes, typo_tokens_threshold,+ prioritize_token_position, prioritize_num_matching_fields,+ query_hashes, token_order, prefixes, typo_tokens_threshold,exhaustive_search, max_candidates, min_len_1typo,min_len_2typo, syn_orig_num_tokens, sort_order, field_values, geopoint_indices);}diff --git a/test/collection_specific_more_test.cpp b/test/collection_specific_more_test.cppindex 4e7347e6..266eb0a6 100644--- a/test/collection_specific_more_test.cpp+++ b/test/collection_specific_more_test.cpp@@ -1846,6 +1846,62 @@ TEST_F(CollectionSpecificMoreTest, ConsiderDroppedTokensDuringTextMatchScoring2)ASSERT_EQ("0", res["hits"][1]["document"]["id"].get<std::string>());}+TEST_F(CollectionSpecificMoreTest, DisableFieldCountForScoring) {+ nlohmann::json schema = R"({+ "name": "coll1",+ "fields": [+ {"name": "name", "type": "string"},+ {"name": "brand", "type": "string"}+ ]+ })"_json;++ Collection *coll1 = collectionManager.create_collection(schema).get();++ nlohmann::json doc;+ doc["id"] = "0";+ doc["name"] = "Alpha beta gamma";+ doc["brand"] = "Alpha beta gamma";+ ASSERT_TRUE(coll1->add(doc.dump()).ok());++ doc["id"] = "1";+ doc["name"] = "Alpha beta gamma";+ doc["brand"] = "Theta";+ ASSERT_TRUE(coll1->add(doc.dump()).ok());++ auto res_op = coll1->search("beta", {"name", "brand"}, "", {}, {}, {2}, 10, 1, FREQUENCY, {true}, 5,+ spp::sparse_hash_set<std::string>(),+ spp::sparse_hash_set<std::string>(), 10, "", 30, 4, "", 20, {}, {}, {}, 0,+ "<mark>", "</mark>", {3,3}, 1000, true, false, true, "", false, 6000 * 1000, 4, 7, fallback,+ 4, {off}, 0, 0, 0, 2, false, "", true, 0, max_score,+ 100, 0, 0, 30000, 2, true);+++ auto res = coll1->search("beta", {"name", "brand"}, "", {}, {}, {2}, 10, 1, FREQUENCY, {true}, 5,+ spp::sparse_hash_set<std::string>(),+ spp::sparse_hash_set<std::string>(), 10, "", 30, 4, "", 20, {}, {}, {}, 0,+ "<mark>", "</mark>", {3,3}, 1000, true, false, true, "", false, 6000 * 1000, 4, 7, fallback,+ 4, {off}, 0, 0, 0, 2, false, "", true, 0, max_score,+ 100, 0, 0, 30000, 2, false).get();++ size_t score1 = std::stoul(res["hits"][0]["text_match_info"]["score"].get<std::string>());+ size_t score2 = std::stoul(res["hits"][1]["text_match_info"]["score"].get<std::string>());+ ASSERT_TRUE(score1 == score2);++ res = coll1->search("beta", {"name", "brand"}, "", {}, {}, {2}, 10, 1, FREQUENCY, {true}, 5,+ spp::sparse_hash_set<std::string>(),+ spp::sparse_hash_set<std::string>(), 10, "", 30, 4, "", 20, {}, {}, {}, 0,+ "<mark>", "</mark>", {3,3}, 1000, true, false, true, "", false, 6000 * 1000, 4, 7, fallback,+ 4, {off}, 0, 0, 0, 2, false, "", true, 0, max_score,+ 100, 0, 0, 30000, 2, true).get();++ ASSERT_EQ("0", res["hits"][0]["document"]["id"].get<std::string>());+ ASSERT_EQ("1", res["hits"][1]["document"]["id"].get<std::string>());++ score1 = std::stoul(res["hits"][0]["text_match_info"]["score"].get<std::string>());+ score2 = std::stoul(res["hits"][1]["text_match_info"]["score"].get<std::string>());+ ASSERT_TRUE(score1 > score2);+}+TEST_F(CollectionSpecificMoreTest, NonNestedFieldNameWithDot) {nlohmann::json schema = R"({"name": "coll1",