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

Parallelize value faceting.

Kishore Nallan0c095fbc8823
diff --git a/src/index.cpp b/src/index.cpp
index 86b52a5e..6ef2fa70 100644
--- a/src/index.cpp
+++ b/src/index.cpp
@@ -3533,7 +3533,7 @@ Option<bool> Index::search(std::vector<query_tokens_t>& field_query_tokens, cons
max_candidates, facet_infos, facet_index_type);
std::vector<std::vector<facet>> facet_batches(num_threads);
- std::vector<facet> value_facets;
+ std::vector<std::vector<facet>> value_facets;
for(size_t i = 0; i < facets.size(); i++) {
const auto& this_facet = facets[i];
@@ -3543,7 +3543,8 @@ Option<bool> Index::search(std::vector<query_tokens_t>& field_query_tokens, cons
if(facet_infos[i].use_value_index) {
#endif
// value based faceting on a single thread
- value_facets.emplace_back(this_facet.field_name, this_facet.orig_index, this_facet.facet_range_map,
+ value_facets.emplace_back();
+ value_facets.back().emplace_back(this_facet.field_name, this_facet.orig_index, this_facet.facet_range_map,
this_facet.is_range_query, this_facet.is_sort_by_alpha,
this_facet.sort_order, this_facet.sort_field);
continue;
@@ -3617,17 +3618,47 @@ Option<bool> Index::search(std::vector<query_tokens_t>& field_query_tokens, cons
}
}
- // do value based faceting which must be single thread on the entire resultset (without batching)
- do_facets(value_facets, facet_query, estimate_facets, facet_sample_percent,
- facet_infos, group_limit, group_by_fields, group_missing_values,
- all_result_ids, all_result_ids_len, max_facet_values,
- is_wildcard_no_filter_query, facet_index_type);
+ lock_process.unlock();
+ num_processed = 0;
+ num_queued = 0;
- for(auto& this_facet : value_facets) {
- auto& acc_facet = facets[this_facet.orig_index];
- aggregate_facet(group_limit, this_facet, acc_facet);
+ // do value based faceting field-wise parallel but on the entire result set
+ for(size_t thread_id = 0; thread_id < value_facets.size(); thread_id++) {
+ num_queued++;
+
+ thread_pool->enqueue([this, thread_id, &facets, &value_facets, &facet_query, group_limit, group_by_fields,
+ all_result_ids, all_result_ids_len, &facet_infos, max_facet_values,
+ is_wildcard_no_filter_query, no_filters_provided, estimate_facets,
+ facet_sample_percent, group_missing_values,
+ &parent_search_begin, &parent_search_stop_ms, &parent_search_cutoff,
+ &num_processed, &m_process, &cv_process, facet_index_type]() {
+ search_begin_us = parent_search_begin;
+ search_stop_us = parent_search_stop_ms;
+ search_cutoff = false;
+
+ auto fq = facet_query;
+
+ do_facets({value_facets[thread_id]}, fq, estimate_facets, facet_sample_percent,
+ facet_infos, group_limit, group_by_fields, group_missing_values,
+ all_result_ids, all_result_ids_len, max_facet_values,
+ is_wildcard_no_filter_query, facet_index_type);
+
+ std::unique_lock<std::mutex> lock(m_process);
+
+ auto& this_facet = value_facets[thread_id][0];
+ auto& acc_facet = facets[this_facet.orig_index];
+ aggregate_facet(group_limit, this_facet, acc_facet);
+
+ num_processed++;
+ parent_search_cutoff = parent_search_cutoff || search_cutoff;
+ cv_process.notify_one();
+ });
}
+ lock_process.lock();
+ cv_process.wait(lock_process, [&](){ return num_processed == num_queued; });
+ search_cutoff = parent_search_cutoff;
+
for(auto & acc_facet: facets) {
for(auto& facet_kv: acc_facet.result_map) {
if(group_limit) {
diff --git a/test/collection_optimized_faceting_test.cpp b/test/collection_optimized_faceting_test.cpp
index 023c9956..634335a1 100644
--- a/test/collection_optimized_faceting_test.cpp
+++ b/test/collection_optimized_faceting_test.cpp
@@ -850,6 +850,46 @@ TEST_F(CollectionOptimizedFacetingTest, FacetCountOnSimilarStrings) {
collectionManager.drop_collection("coll1");
}
+TEST_F(CollectionOptimizedFacetingTest, ConcurrentValueFacetingOnMulFields) {
+ Collection *coll1;
+
+ std::vector<field> fields = {field("c1", field_types::STRING, true),
+ field("c2", field_types::STRING, true),
+ field("c3", field_types::STRING, true),
+ field("c4", field_types::STRING, true),
+ field("points", field_types::INT32, true)};
+
+ std::vector<sort_by> sort_fields = {sort_by("points", "DESC")};
+
+ coll1 = collectionManager.get_collection("coll1").get();
+ if (coll1 == nullptr) {
+ coll1 = collectionManager.create_collection("coll1", 4, fields, "points").get();
+ }
+
+ for(size_t i = 0; i < 1000; i++) {
+ nlohmann::json doc;
+ doc["c1"] = "c1_" + std::to_string(i % 40);
+ doc["c2"] = "c2_" + std::to_string(i % 40);
+ doc["c3"] = "c3_" + std::to_string(i % 40);
+ doc["c4"] = "c4_" + std::to_string(i % 40);
+ doc["points"] = 25;
+ coll1->add(doc.dump());
+ }
+
+ std::vector<std::string> facets = {"c1", "c2", "c3", "c4"};
+
+ nlohmann::json results = coll1->search("*", {}, "points:[25, 50]", facets, sort_fields, {0}, 10, 1,
+ token_ordering::FREQUENCY, {true}, 10, spp::sparse_hash_set<std::string>(),
+ spp::sparse_hash_set<std::string>(), 10, "", 30UL, 4UL,
+ "", 1UL, "", "", {}, 3UL, "<mark>", "</mark>", {},
+ 4294967295UL, true, false, true, "", false, 6000000UL, 4UL,
+ 7UL, fallback, 4UL, {off}, 32767UL, 32767UL, 2UL, 2UL, false,
+ "", true, 0UL, max_score, 100UL, 0UL, 4294967295UL, VALUE).get();
+
+ ASSERT_EQ(4, results["facet_counts"].size());
+ collectionManager.drop_collection("coll1");
+}
+
TEST_F(CollectionOptimizedFacetingTest, FacetByNestedIntField) {
nlohmann::json schema = R"({
"name": "coll1",