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

adding support for facet_query

krunal131345a698d505ac
diff --git a/include/facet_index.h b/include/facet_index.h
index d5a878c8..cf20cbcd 100644
--- a/include/facet_index.h
+++ b/include/facet_index.h
@@ -74,7 +74,8 @@ public:
size_t get_facet_count(const std::string& field);
size_t intersect(const std::string& val, const uint32_t* result_ids, int result_id_len,
- int max_facet_count, std::map<std::string, uint32_t>& found, bool is_wildcard_no_filter_query);
+ int max_facet_count, std::map<std::string, uint32_t>& found,
+ bool is_wildcard_no_filter_query);
size_t get_facet_indexes(const std::string& field, std::map<uint32_t, std::vector<uint32_t>>& seqid_index_map);
diff --git a/include/field.h b/include/field.h
index 8c2ea261..19b56736 100644
--- a/include/field.h
+++ b/include/field.h
@@ -637,7 +637,7 @@ struct facet {
const std::string field_name;
spp::sparse_hash_map<std::string, facet_count_t> result_map;
// used for facet value query
- spp::sparse_hash_map<uint32_t, std::vector<std::string>> hash_tokens;
+ spp::sparse_hash_map<std::string, std::vector<std::string>> hash_tokens;
// used for faceting grouped results
spp::sparse_hash_map<uint32_t, spp::sparse_hash_set<uint32_t>> hash_groups;
@@ -683,7 +683,7 @@ struct facet {
struct facet_info_t {
// facet hash => resolved tokens
- std::unordered_map<uint32_t, std::vector<std::string>> hashes;
+ std::unordered_map<std::string, std::vector<std::string>> hashes;
bool use_facet_query = false;
bool should_compute_stats = false;
field facet_field{"", "", false};
diff --git a/include/index.h b/include/index.h
index 3897c999..96ef876c 100644
--- a/include/index.h
+++ b/include/index.h
@@ -335,6 +335,9 @@ private:
StringUtils string_utils;
+ std::atomic_uint64_t aggragate_docs_count;
+
+ std::set<std::string> facet_migrated;
// used as sentinels
static spp::sparse_hash_map<uint32_t, int64_t> text_match_sentinel_value;
@@ -754,7 +757,7 @@ public:
const uint32_t* all_result_ids, const size_t& all_result_ids_len,
const std::vector<std::string>& group_by_fields,
size_t max_candidates,
- std::vector<facet_info_t>& facet_infos) const;
+ std::vector<facet_info_t>& facet_infos, bool use_facet_intersection) const;
void resolve_space_as_typos(std::vector<std::string>& qtokens, const std::string& field_name,
std::vector<std::vector<std::string>>& resolved_queries) const;
diff --git a/src/collection.cpp b/src/collection.cpp
index b46041bf..3194a3de 100644
--- a/src/collection.cpp
+++ b/src/collection.cpp
@@ -1922,7 +1922,25 @@ Option<nlohmann::json> Collection::search(std::string raw_query,
facet_result["counts"] = nlohmann::json::array();
std::vector<facet_value_t> facet_values;
+ std::vector<std::pair<std::string, facet_count_t>> facet_counts;
+
+ for (const auto & kv : a_facet.result_map) {
+ facet_counts.emplace_back(std::make_pair(kv.first, kv.second));
+ }
+ auto max_facets = std::min(max_facet_values, facet_counts.size());
+ auto nthElement = max_facets == facet_counts.size() ? max_facets - 1 : max_facets;
+ std::nth_element(facet_counts.begin(), facet_counts.begin() + nthElement,
+ facet_counts.end(), [&](const auto& kv1, const auto& kv2) {
+ size_t a_count = kv1.second.count;
+ size_t b_count = kv2.second.count;
+
+ size_t a_value_size = UINT64_MAX - kv1.first.size();
+ size_t b_value_size = UINT64_MAX - kv2.first.size();
+
+ return std::tie(a_count, a_value_size) > std::tie(b_count, b_value_size);
+ });
+
if(a_facet.is_range_query){
for(auto kv : a_facet.result_map){
@@ -1936,66 +1954,32 @@ Option<nlohmann::json> Collection::search(std::string raw_query,
LOG (ERROR) << "range_id not found in result map.";
}
}
- } else if(a_facet.is_intersected) {
- //LOG(INFO) << "used intersection";
- std::vector<std::pair<std::string, uint32_t>> facet_counts;
-
- for (const auto & kv : a_facet.result_map) {
- facet_counts.emplace_back(std::make_pair(kv.first, kv.second.count));
- }
-
- auto max_facets = std::min(max_facet_values, facet_counts.size());
- auto nthElement = max_facets == facet_counts.size() ? max_facets - 1 : max_facets;
-
- std::nth_element(facet_counts.begin(), facet_counts.begin() + nthElement,
- facet_counts.end(), [&](const auto& kv1, const auto& kv2) {
- size_t a_count = kv1.second;
- size_t b_count = kv2.second;
-
- size_t a_value_size = UINT64_MAX - kv1.first.size();
- size_t b_value_size = UINT64_MAX - kv2.first.size();
-
- return std::tie(a_count, a_value_size) > std::tie(b_count, b_value_size);
- });
-
- for(int i = 0; i < max_facets; ++i) {
- const auto& kv = facet_counts[i];
- facet_value_t facet_value = { kv.first, kv.first, kv.second};
- facet_values.emplace_back(facet_value);
- }
} else {
- //LOG(INFO) << "used hashes";
- std::vector<std::pair<uint32_t, facet_count_t>> facet_hash_counts;
-
- for (const auto & kv : a_facet.result_map) {
- facet_hash_counts.emplace_back(std::make_pair(std::stoul(kv.first), kv.second));
- }
-
auto the_field = search_schema.at(a_facet.field_name);
- // keep only top K facets
- auto max_facets = std::min(max_facet_values, facet_hash_counts.size());
-
- auto nthElement = max_facets == facet_hash_counts.size() ? max_facets - 1 : max_facets;
-
- std::nth_element(facet_hash_counts.begin(), facet_hash_counts.begin() + nthElement,
- facet_hash_counts.end(), Collection::facet_count_compare);
for(size_t fi = 0; fi < max_facets; fi++) {
// remap facet value hash with actual string
- auto & kv = facet_hash_counts[fi];
+ auto & kv = facet_counts[fi];
auto & facet_count = kv.second;
- // fetch actual facet value from representative doc id
- const std::string& seq_id_key = get_seq_id_key((uint32_t) facet_count.doc_id);
- nlohmann::json document;
- const Option<bool> & document_op = get_document_from_store(seq_id_key, document);
- if(!document_op.ok()) {
- LOG(ERROR) << "Facet fetch error. " << document_op.error();
- continue;
- }
+
std::string value;
- bool facet_found = facet_value_to_string(a_facet, facet_count, document, value);
- if(!facet_found) {
- continue;
+ if(a_facet.is_intersected) {
+ value = kv.first;
+ //LOG(INFO) << "used intersection";
+ } else {
+ // fetch actual facet value from representative doc id
+ //LOG(INFO) << "used hashes";
+ const std::string& seq_id_key = get_seq_id_key((uint32_t) facet_count.doc_id);
+ nlohmann::json document;
+ const Option<bool> & document_op = get_document_from_store(seq_id_key, document);
+ if(!document_op.ok()) {
+ LOG(ERROR) << "Facet fetch error. " << document_op.error();
+ continue;
+ }
+ bool facet_found = facet_value_to_string(a_facet, facet_count, document, value);
+ if(!facet_found) {
+ continue;
+ }
}
std::unordered_map<std::string, size_t> ftoken_pos;
std::vector<string>& ftokens = a_facet.hash_tokens[kv.first];
diff --git a/src/index.cpp b/src/index.cpp
index 41524890..784700b0 100644
--- a/src/index.cpp
+++ b/src/index.cpp
@@ -722,8 +722,11 @@ void Index::index_field_in_memory(const field& afield, std::vector<index_record>
#ifdef TEST_BUILD
facet_threshold_count = FACET_INDEX_THRESHOLD + 1;
is_migrated = true;
+ facet_migrated.emplace(afield.name);
#endif
- if(!is_migrated && (facet_threshold_count > FACET_INDEX_THRESHOLD)) {
+ const auto total_num_docs = aggragate_docs_count.load();
+ if(!is_migrated && (facet_threshold_count > FACET_INDEX_THRESHOLD)
+ && total_num_docs < 1000000) {
migrate_facet_to_new_index(afield.name);
if(afield.is_string()) {
@@ -734,6 +737,7 @@ void Index::index_field_in_memory(const field& afield, std::vector<index_record>
numerical_index_it->second->set_migrated(true);
}
}
+ facet_migrated.emplace(afield.name);
}
for(const auto& record: iter_batch) {
@@ -1117,6 +1121,9 @@ void Index::index_field_in_memory(const field& afield, std::vector<index_record>
}
}
}
+
+ const auto updated_count = aggragate_docs_count.load() + iter_batch.size();
+ aggragate_docs_count.store(updated_count);
}
void Index::tokenize_string(const std::string& text, bool is_facet, const field& a_field,
@@ -1336,10 +1343,11 @@ void Index::do_facets(std::vector<facet> & facets, facet_query_t & facet_query,
use_facet_intersection = false;
use_hashes = false;
#endif
+ bool is_wildcard_no_filter_query = is_wildcard_query && no_filters_provided;
+ bool old_index_exist = facet_migrated.find(facet_field.name) != facet_migrated.end();
- if(results_size && facet_records && ((facet_records <= FACET_INDEX_THRESHOLD
- || is_wildcard_query) && !use_facet_query && group_limit == 0)
- && !use_hashes || use_facet_intersection) {
+ if(results_size && facet_records && ((!old_index_exist || is_wildcard_no_filter_query)
+ && group_limit == 0) && !use_hashes || use_facet_intersection) {
//LOG(INFO) << "Using intersection to find facets";
a_facet.is_intersected = true;
@@ -1347,11 +1355,11 @@ void Index::do_facets(std::vector<facet> & facets, facet_query_t & facet_query,
if(!facet_field.name.empty()) {
if(facet_field.is_string()) {
facet_index_v4->intersect(a_facet.field_name, result_ids,
- results_size, max_facet_count, facet_results, is_wildcard_query & no_filters_provided);
+ results_size, max_facet_count, facet_results, is_wildcard_no_filter_query);
} else {
std::map<int64_t, uint32_t> facet_counts;
numerical_index.at(a_facet.field_name)->intersect(result_ids,
- results_size, max_facet_count, facet_counts, is_wildcard_query & no_filters_provided);
+ results_size, max_facet_count, facet_counts, is_wildcard_no_filter_query);
for(const auto& kv : facet_counts) {
std::string val;
@@ -1379,8 +1387,28 @@ void Index::do_facets(std::vector<facet> & facets, facet_query_t & facet_query,
facet_count.count = kv.second;
}
} else {
- facet_count_t& facet_count = a_facet.result_map[kv.first];
- facet_count.count = kv.second;
+ if(use_facet_query) {
+ const auto fquery_hashes_it = fquery_hashes.find(facet_field.name);
+ if(fquery_hashes_it != fquery_hashes.end()) {
+ const auto& searched_tokens = fquery_hashes_it->second;
+ auto facet_str = kv.first;
+ transform(facet_str.begin(), facet_str.end(), facet_str.begin(), ::tolower);
+
+ for(const auto& val : searched_tokens)
+ {
+ if(facet_str.find(val) != std::string::npos) {
+ facet_count_t& facet_count = a_facet.result_map[kv.first];
+ facet_count.count = kv.second;
+
+ a_facet.hash_tokens[kv.first] = fquery_hashes.at(facet_field.name);
+ }
+ }
+ }
+
+ } else {
+ facet_count_t& facet_count = a_facet.result_map[kv.first];
+ facet_count.count = kv.second;
+ }
}
if(should_compute_stats) {
@@ -1448,6 +1476,7 @@ void Index::do_facets(std::vector<facet> & facets, facet_query_t & facet_query,
compute_facet_stats(a_facet, fhash, facet_field.type);
}
+ std::string fhash_str = std::to_string(fhash);
if(a_facet.is_range_query) {
int64_t doc_val = get_doc_val_from_sort_index(sort_index_it, doc_seq_id);
@@ -1457,8 +1486,7 @@ void Index::do_facets(std::vector<facet> & facets, facet_query_t & facet_query,
facet_count_t& facet_count = a_facet.result_map[range_id];
facet_count.count += 1;
}
- } else if(!use_facet_query || fquery_hashes.find(fhash) != fquery_hashes.end()) {
- std::string fhash_str = std::to_string(fhash);
+ } else if(!use_facet_query || fquery_hashes.find(fhash_str) != fquery_hashes.end()) {
facet_count_t& facet_count = a_facet.result_map[fhash_str];
//LOG(INFO) << "field: " << a_facet.field_name << ", doc id: " << doc_seq_id << ", hash: " << fhash;
facet_count.doc_id = doc_seq_id;
@@ -1470,7 +1498,7 @@ void Index::do_facets(std::vector<facet> & facets, facet_query_t & facet_query,
}
if(use_facet_query) {
//LOG (INFO) << "adding hash tokens for hash " << fhash;
- a_facet.hash_tokens[fhash] = fquery_hashes.at(fhash);
+ a_facet.hash_tokens[fhash_str] = fquery_hashes.at(fhash_str);
}
}
}
@@ -3003,7 +3031,7 @@ Option<bool> Index::search(std::vector<query_tokens_t>& field_query_tokens, cons
std::vector<facet_info_t> facet_infos(facets.size());
compute_facet_infos(facets, facet_query, facet_query_num_typos, all_result_ids, all_result_ids_len,
- group_by_fields, max_candidates, facet_infos);
+ group_by_fields, max_candidates, facet_infos, use_facet_intersection);
std::vector<std::vector<facet>> facet_batches(num_threads);
for(size_t i = 0; i < num_threads; i++) {
@@ -3089,11 +3117,7 @@ Option<bool> Index::search(std::vector<query_tokens_t>& field_query_tokens, cons
acc_facet.result_map[facet_kv.first].array_pos = facet_kv.second.array_pos;
acc_facet.is_intersected = this_facet.is_intersected;
- if(!facet_query.query.empty()) {
- fhash = std::stoul(facet_kv.first);
- //LOG(INFO) << "copying hash tokens for hash " << fhash;
- acc_facet.hash_tokens[fhash] = this_facet.hash_tokens[fhash];
- }
+ acc_facet.hash_tokens[facet_kv.first] = this_facet.hash_tokens[facet_kv.first];
}
if(this_facet.stats.fvcount != 0) {
@@ -3129,7 +3153,7 @@ Option<bool> Index::search(std::vector<query_tokens_t>& field_query_tokens, cons
std::vector<facet_info_t> facet_infos(facets.size());
compute_facet_infos(facets, facet_query, facet_query_num_typos,
&included_ids_vec[0], included_ids_vec.size(), group_by_fields,
- max_candidates, facet_infos);
+ max_candidates, facet_infos, use_facet_intersection);
do_facets(facets, facet_query, estimate_facets, facet_sample_percent,
facet_infos, group_limit, group_by_fields, &included_ids_vec[0],
included_ids_vec.size(), max_facet_values, is_wildcard_query, no_filters_provided,
@@ -4535,7 +4559,7 @@ void Index::compute_facet_infos(const std::vector<facet>& facets, facet_query_t&
const uint32_t* all_result_ids, const size_t& all_result_ids_len,
const std::vector<std::string>& group_by_fields,
const size_t max_candidates,
- std::vector<facet_info_t>& facet_infos) const {
+ std::vector<facet_info_t>& facet_infos, bool use_facet_intersection) const {
if(all_result_ids_len == 0) {
return;
@@ -4547,11 +4571,6 @@ void Index::compute_facet_infos(const std::vector<facet>& facets, facet_query_t&
const auto field_facet_mapping_it = facet_index_v3.find(a_facet.field_name);
const auto field_single_val_facet_mapping_it = single_val_facet_index_v3.find(a_facet.field_name);
- if((field_facet_mapping_it == facet_index_v3.end())
- && (field_single_val_facet_mapping_it == single_val_facet_index_v3.end())) {
- continue;
- }
-
facet_infos[findex].use_facet_query = false;
const field &facet_field = search_schema.at(a_facet.field_name);
@@ -4644,39 +4663,49 @@ void Index::compute_facet_infos(const std::vector<facet>& facets, facet_query_t&
continue;
}
//LOG(INFO) << "seq_id matched : " << seq_id;
+#ifdef TEST_BUILD
+ if(!use_facet_intersection) {
+#else
+ //checking if facets is migrated to old index
+ if(facet_migrated.find(facet_field.name) != facet_migrated.end()) {
+#endif
+ if(facet_field.is_array()) {
+ const auto doc_fvalues_it = field_facet_mapping_it->second[seq_id % ARRAY_FACET_DIM]->find(seq_id);
+ if(doc_fvalues_it == field_facet_mapping_it->second[seq_id % ARRAY_FACET_DIM]->end()) {
+ continue;
+ }
- if(facet_field.is_array()) {
- const auto doc_fvalues_it = field_facet_mapping_it->second[seq_id % ARRAY_FACET_DIM]->find(seq_id);
- if(doc_fvalues_it == field_facet_mapping_it->second[seq_id % ARRAY_FACET_DIM]->end()) {
- continue;
- }
-
- std::vector<size_t> array_indices;
- posting_t::get_matching_array_indices(posting_lists, seq_id, array_indices);
+ std::vector<size_t> array_indices;
+ posting_t::get_matching_array_indices(posting_lists, seq_id, array_indices);
- for(size_t array_index: array_indices) {
- if(array_index < doc_fvalues_it->second.length) {
- uint32_t hash = doc_fvalues_it->second.hashes[array_index];
+ for(size_t array_index: array_indices) {
+ if(array_index < doc_fvalues_it->second.length) {
+ std::string hash = std::to_string(doc_fvalues_it->second.hashes[array_index]);
- /*LOG(INFO) << "seq_id: " << seq_id << ", hash: " << hash << ", array index: "
- << array_index;*/
+ /*LOG(INFO) << "seq_id: " << seq_id << ", hash: " << hash << ", array index: "
+ << array_index;*/
- if(facet_infos[findex].hashes.count(hash) == 0) {
- //LOG(INFO) << "adding searched_tokens for hash " << hash;
- facet_infos[findex].hashes.emplace(hash, searched_tokens);
+ if(facet_infos[findex].hashes.count(hash) == 0) {
+ //LOG(INFO) << "adding searched_tokens for hash " << hash;
+ facet_infos[findex].hashes.emplace(hash, searched_tokens);
+ }
}
}
+ } else {
+ const auto doc_single_fvalues_it = field_single_val_facet_mapping_it->second[seq_id % ARRAY_FACET_DIM]->find(seq_id);
+ if(doc_single_fvalues_it == field_single_val_facet_mapping_it->second[seq_id % ARRAY_FACET_DIM]->end()) {
+ continue;
+ }
+
+ std::string hash = std::to_string(doc_single_fvalues_it->second);
+ if(facet_infos[findex].hashes.count(hash) == 0) {
+ //LOG(INFO) << "adding searched_tokens for hash " << hash;
+ facet_infos[findex].hashes.emplace(hash, searched_tokens);
+ }
}
- } else {
- const auto doc_single_fvalues_it = field_single_val_facet_mapping_it->second[seq_id % ARRAY_FACET_DIM]->find(seq_id);
- if(doc_single_fvalues_it == field_single_val_facet_mapping_it->second[seq_id % ARRAY_FACET_DIM]->end()) {
- continue;
- }
-
- uint32_t hash = doc_single_fvalues_it->second;
- if(facet_infos[findex].hashes.count(hash) == 0) {
- //LOG(INFO) << "adding searched_tokens for hash " << hash;
- facet_infos[findex].hashes.emplace(hash, searched_tokens);
+ } else { //for new index adding search tokens
+ for(const auto& val : searched_tokens) {
+ facet_infos[findex].hashes[facet_field.name].emplace_back(val);
}
}
}
diff --git a/test/collection_grouping_test.cpp b/test/collection_grouping_test.cpp
index 527e7b17..f88d9551 100644
--- a/test/collection_grouping_test.cpp
+++ b/test/collection_grouping_test.cpp
@@ -452,10 +452,10 @@ TEST_F(CollectionGroupingTest, GroupingWithArrayFieldAndOverride) {
ASSERT_STREQ("brand", res["facet_counts"][0]["field_name"].get<std::string>().c_str());
ASSERT_EQ(2, (int) res["facet_counts"][0]["counts"][0]["count"]);
- ASSERT_STREQ("Beta", res["facet_counts"][0]["counts"][0]["value"].get<std::string>().c_str());
+ ASSERT_STREQ("Xorp", res["facet_counts"][0]["counts"][0]["value"].get<std::string>().c_str());
ASSERT_EQ(2, (int) res["facet_counts"][0]["counts"][1]["count"]);
- ASSERT_STREQ("Xorp", res["facet_counts"][0]["counts"][1]["value"].get<std::string>().c_str());
+ ASSERT_STREQ("Beta", res["facet_counts"][0]["counts"][1]["value"].get<std::string>().c_str());
ASSERT_EQ(2, (int) res["facet_counts"][0]["counts"][2]["count"]);
ASSERT_STREQ("Omega", res["facet_counts"][0]["counts"][2]["value"].get<std::string>().c_str());
diff --git a/test/collection_optimized_faceting_test.cpp b/test/collection_optimized_faceting_test.cpp
index 86501db4..f4c662c2 100644
--- a/test/collection_optimized_faceting_test.cpp
+++ b/test/collection_optimized_faceting_test.cpp
@@ -208,6 +208,117 @@ TEST_F(CollectionOptimizedFacetingTest, FacetCounts) {
ASSERT_STREQ("gold", results["facet_counts"][0]["counts"][1]["value"].get<std::string>().c_str());
ASSERT_STREQ("bronze", results["facet_counts"][0]["counts"][2]["value"].get<std::string>().c_str());
ASSERT_STREQ("FINE PLATINUM", results["facet_counts"][0]["counts"][3]["value"].get<std::string>().c_str());
+
+ // facet with facet filter query (allows typo correction!)
+ results = coll_array_fields->search("*", query_fields, "", facets, sort_fields, {0}, 10, 1, FREQUENCY,
+ {false}, Index::DROP_TOKENS_THRESHOLD,
+ spp::sparse_hash_set<std::string>(),
+ spp::sparse_hash_set<std::string>(), 10, " tags : sliver", 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, true).get();
+
+ ASSERT_EQ(5, results["hits"].size());
+ ASSERT_EQ(1, results["facet_counts"].size());
+ ASSERT_STREQ("tags", results["facet_counts"][0]["field_name"].get<std::string>().c_str());
+ ASSERT_EQ(3, (int) results["facet_counts"][0]["counts"][0]["count"]);
+ ASSERT_STREQ("silver", results["facet_counts"][0]["counts"][0]["value"].get<std::string>().c_str());
+
+ // facet with facet filter query matching 2 tokens
+ results = coll_array_fields->search("*", query_fields, "", facets, sort_fields, {0}, 10, 1, FREQUENCY,
+ {false}, Index::DROP_TOKENS_THRESHOLD,
+ spp::sparse_hash_set<std::string>(),
+ spp::sparse_hash_set<std::string>(), 10, "tags: fxne platim", 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, true).get();
+
+ ASSERT_EQ(5, results["hits"].size());
+ ASSERT_EQ(1, results["facet_counts"].size());
+ ASSERT_STREQ("tags", results["facet_counts"][0]["field_name"].get<std::string>().c_str());
+ ASSERT_EQ(1, (int) results["facet_counts"][0]["counts"][0]["count"]);
+ ASSERT_STREQ("FINE PLATINUM", results["facet_counts"][0]["counts"][0]["value"].get<std::string>().c_str());
+ ASSERT_STREQ("<mark>FINE</mark> <mark>PLATIN</mark>UM", results["facet_counts"][0]["counts"][0]["highlighted"].get<std::string>().c_str());
+
+ // facet with facet filter query matching first token of an array
+ results = coll_array_fields->search("*", query_fields, "", facets, sort_fields, {0}, 10, 1, FREQUENCY,
+ {false}, Index::DROP_TOKENS_THRESHOLD,
+ spp::sparse_hash_set<std::string>(),
+ spp::sparse_hash_set<std::string>(), 10, "tags: fine", 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, true).get();
+
+ ASSERT_EQ(5, results["hits"].size());
+ ASSERT_EQ(1, results["facet_counts"].size());
+ ASSERT_STREQ("tags", results["facet_counts"][0]["field_name"].get<std::string>().c_str());
+ ASSERT_EQ(1, (int) results["facet_counts"][0]["counts"][0]["count"]);
+ ASSERT_STREQ("FINE PLATINUM", results["facet_counts"][0]["counts"][0]["value"].get<std::string>().c_str());
+
+ // facet with facet filter query matching second token of an array
+ results = coll_array_fields->search("*", query_fields, "", facets, sort_fields, {0}, 10, 1, FREQUENCY,
+ {false}, Index::DROP_TOKENS_THRESHOLD,
+ spp::sparse_hash_set<std::string>(),
+ spp::sparse_hash_set<std::string>(), 10, "tags: pltinum", 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, true).get();
+
+ ASSERT_EQ(5, results["hits"].size());
+ ASSERT_EQ(1, results["facet_counts"].size());
+ ASSERT_STREQ("tags", results["facet_counts"][0]["field_name"].get<std::string>().c_str());
+ ASSERT_EQ(1, (int) results["facet_counts"][0]["counts"][0]["count"]);
+ ASSERT_STREQ("FINE PLATINUM", results["facet_counts"][0]["counts"][0]["value"].get<std::string>().c_str());
+
+ // facet query on an integer field
+ results = coll_array_fields->search("*", query_fields, "", {"age"}, sort_fields, {0}, 10, 1, FREQUENCY,
+ {false}, Index::DROP_TOKENS_THRESHOLD,
+ spp::sparse_hash_set<std::string>(),
+ spp::sparse_hash_set<std::string>(), 10, "age: 2",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, true).get();
+
+ ASSERT_EQ(5, results["hits"].size());
+ ASSERT_EQ(1, results["facet_counts"].size());
+ ASSERT_STREQ("age", results["facet_counts"][0]["field_name"].get<std::string>().c_str());
+
+ ASSERT_EQ(1, (int) results["facet_counts"][0]["counts"][0]["count"]);
+ ASSERT_STREQ("24", results["facet_counts"][0]["counts"][0]["value"].get<std::string>().c_str());
+ ASSERT_STREQ("<mark>2</mark>4", results["facet_counts"][0]["counts"][0]["highlighted"].get<std::string>().c_str());
+
+ ASSERT_EQ(1, (int) results["facet_counts"][0]["counts"][1]["count"]);
+ ASSERT_STREQ("21", results["facet_counts"][0]["counts"][1]["value"].get<std::string>().c_str());
+ ASSERT_STREQ("<mark>2</mark>1", results["facet_counts"][0]["counts"][1]["highlighted"].get<std::string>().c_str());
+
+ // facet query on a float field
+ results = coll_array_fields->search("*", query_fields, "", {"rating"}, sort_fields, {0}, 10, 1, FREQUENCY,
+ {false}, Index::DROP_TOKENS_THRESHOLD,
+ spp::sparse_hash_set<std::string>(),
+ spp::sparse_hash_set<std::string>(), 10, "rating: 7",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, true).get();
+
+ ASSERT_EQ(5, results["hits"].size());
+ ASSERT_EQ(1, results["facet_counts"].size());
+ ASSERT_STREQ("rating", results["facet_counts"][0]["field_name"].get<std::string>().c_str());
+ ASSERT_EQ(1, (int) results["facet_counts"][0]["counts"][0]["count"]);
+ ASSERT_STREQ("7.812", results["facet_counts"][0]["counts"][0]["value"].get<std::string>().c_str());
+ ASSERT_STREQ("<mark>7</mark>.812", results["facet_counts"][0]["counts"][0]["highlighted"].get<std::string>().c_str());
+
+ ASSERT_EQ(5, results["facet_counts"][0]["stats"].size());
+ ASSERT_FLOAT_EQ(4.880199885368347, results["facet_counts"][0]["stats"]["avg"].get<double>());
+ ASSERT_FLOAT_EQ(0.0, results["facet_counts"][0]["stats"]["min"].get<double>());
+ ASSERT_FLOAT_EQ(9.99899959564209, results["facet_counts"][0]["stats"]["max"].get<double>());
+ ASSERT_FLOAT_EQ(24.400999426841736, results["facet_counts"][0]["stats"]["sum"].get<double>());
+ ASSERT_FLOAT_EQ(1, results["facet_counts"][0]["stats"]["total_values"].get<size_t>());
// facet with wildcard
results = coll_array_fields->search("Jeremy", query_fields, "", {"ag*"}, sort_fields, {0}, 10, 1, FREQUENCY,