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

Unify string facet indices + move counter to linked list.

Kishore Nallan938b3f3b4595
diff --git a/include/facet_index.h b/include/facet_index.h
index fb9541d3..38aa5a54 100644
--- a/include/facet_index.h
+++ b/include/facet_index.h
@@ -3,94 +3,139 @@
#include "ids_t.h"
#include "tsl/htrie_map.h"
#include <unordered_set>
+#include <posting_list.h>
+#include <num_tree.h>
+#include <list>
+
+struct facet_value_id_t {
+ std::string facet_value;
+ uint32_t facet_id = UINT32_MAX;
+
+ bool operator==(const facet_value_id_t& other) const {
+ return facet_value == other.facet_value;
+ }
+
+ // Hash function for the struct
+ struct Hash {
+ std::size_t operator()(const facet_value_id_t& value) const {
+ return std::hash<std::string>{}(value.facet_value);
+ }
+ };
+
+ facet_value_id_t() = default;
+
+ facet_value_id_t(const std::string& fvalue, const uint32_t fid): facet_value(fvalue), facet_id(fid) {
+
+ }
+
+ facet_value_id_t(const std::string& fvalue): facet_value(fvalue), facet_id(UINT32_MAX) {
+
+ }
+};
class facet_index_t {
private:
- struct count_list {
- count_list() = delete;
+ struct facet_count_t {
+ facet_count_t() = delete;
- ~count_list () = default;
+ ~facet_count_t () = default;
- count_list(const std::string& sv, uint32_t facet_count, uint32_t facet_index) {
+ facet_count_t(const std::string& sv, uint32_t facet_count, uint32_t this_facet_id) {
facet_value = sv;
count = facet_count;
- index = facet_index;
+ facet_id = this_facet_id;
}
- count_list& operator=(count_list& obj) {
+ facet_count_t& operator=(facet_count_t& obj) {
facet_value = obj.facet_value;
count = obj.count;
- index = obj.index;
+ facet_id = obj.facet_id;
return *this;
}
std::string facet_value;
uint32_t count;
- uint32_t index;
+ uint32_t facet_id;
};
- struct facet_index_struct {
- void* id_list_ptr;
- uint32_t index;
- uint32_t count_list_index;
+ struct facet_id_seq_ids_t {
+ void* seq_ids;
+ uint32_t facet_id;
+ std::list<facet_count_t>::iterator facet_count_it;
- facet_index_struct() {
- id_list_ptr = nullptr;
- index = UINT32_MAX;
- count_list_index = UINT32_MAX;
+ facet_id_seq_ids_t() {
+ seq_ids = nullptr;
+ facet_id = UINT32_MAX;
}
- ~facet_index_struct() {};
+ ~facet_id_seq_ids_t() {};
};
- struct facet_index_counter {
- tsl::htrie_map<char, facet_index_struct> facet_index_map;
- std::vector<count_list*> counter_list;
-
- facet_index_counter() {
- facet_index_map.clear();
- counter_list.clear();
+ struct facet_doc_ids_list_t {
+ tsl::htrie_map<char, facet_id_seq_ids_t> fvalue_seq_ids;
+ std::list<facet_count_t> counts;
+ posting_list_t* seq_id_hashes;
+
+ bool has_value_index = true;
+ bool has_hash_index = false;
+
+ facet_doc_ids_list_t() {
+ fvalue_seq_ids.clear();
+ counts.clear();
+ seq_id_hashes = nullptr;
}
- ~facet_index_counter() {
- for(auto it = facet_index_map.begin(); it != facet_index_map.end(); ++it) {
- ids_t::destroy_list(it.value().id_list_ptr);
+ ~facet_doc_ids_list_t() {
+ for(auto it = fvalue_seq_ids.begin(); it != fvalue_seq_ids.end(); ++it) {
+ if(it.value().seq_ids) {
+ ids_t::destroy_list(it.value().seq_ids);
+ }
}
- facet_index_map.clear();
-
- for(auto val : counter_list) {
- delete val;
- }
- counter_list.clear();
+ fvalue_seq_ids.clear();
+ counts.clear();
+
+ delete seq_id_hashes;
}
};
- std::map<std::string, facet_index_counter> facet_field_map;
- uint32_t count_index = 0;
+ // field -> facet_index
+ std::unordered_map<std::string, facet_doc_ids_list_t> facet_field_map;
+
+ // auto incrementing ID that is assigned to each unique facet value string
+ uint32_t next_facet_id = 0;
+
public:
facet_index_t() = default;
~facet_index_t();
- uint32_t insert(const std::string& field, const std::string& value,
- const std::vector<uint32_t>& ids, bool is_string=false);
+ void insert(const std::string& field_name, bool is_string,
+ std::unordered_map<facet_value_id_t, std::vector<uint32_t>, facet_value_id_t::Hash>& fvalue_to_seq_ids,
+ std::unordered_map<uint32_t, std::vector<facet_value_id_t>>& seq_id_to_fvalues);
- void erase(const std::string& field);
+ void erase(const std::string& field_name);
- void remove(const std::string& field, const uint32_t seq_id);
+ void remove(const std::string& field_name, const uint32_t seq_id);
- bool contains(const std::string& field);
+ bool contains(const std::string& field_name);
- size_t get_facet_count(const std::string& field);
+ size_t get_facet_count(const std::string& field_name);
- 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);
+ size_t intersect(const std::string& val, const uint32_t* result_ids, size_t result_id_len,
+ size_t 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_countIndexes);
void initialize(const std::string& field);
+
+ void handle_index_change(const std::string& field_name, size_t total_num_docs, size_t facet_index_threshold,
+ size_t facet_count, spp::sparse_hash_map<std::string, num_tree_t*>& numerical_index);
+
+ bool has_hash_index(const std::string& field_name);
+
+ posting_list_t* get_facet_hash_index(const std::string& field_name);
};
\ No newline at end of file
diff --git a/include/index.h b/include/index.h
index a3aaa9e6..da706763 100644
--- a/include/index.h
+++ b/include/index.h
@@ -188,6 +188,11 @@ struct search_args {
};
};
+struct offsets_facet_hashes_t {
+ // token to offsets
+ std::unordered_map<std::string, std::vector<uint32_t>> offsets;
+};
+
struct index_record {
size_t position; // position of record in the original request
uint32_t seq_id;
@@ -201,12 +206,9 @@ struct index_record {
bool is_update;
// pre-processed data primed for indexing
- std::unordered_map<std::string,
- std::unordered_map<std::string, std::vector<uint32_t>>> field_index;
+ std::unordered_map<std::string, offsets_facet_hashes_t> field_index;
int64_t points;
- std::map<std::string, std::vector<uint32_t>> facet_hashes;
-
Option<bool> indexed; // indicates if the indexing operation was a success
DIRTY_VALUES dirty_values;
@@ -306,8 +308,6 @@ private:
// geo_array_field => (seq_id => values) used for exact filtering of geo array records
spp::sparse_hash_map<std::string, spp::sparse_hash_map<uint32_t, int64_t*>*> geo_array_index;
- spp::sparse_hash_map<std::string, posting_list_t*> facet_hash_index;
-
facet_index_t* facet_index_v4 = nullptr;
// sort_field => (seq_id => value)
@@ -333,8 +333,6 @@ private:
StringUtils string_utils;
- std::atomic_uint64_t aggragate_docs_count;
-
// used as sentinels
static spp::sparse_hash_map<uint32_t, int64_t> text_match_sentinel_value;
@@ -489,12 +487,13 @@ private:
void insert_doc(const int64_t score, art_tree *t, uint32_t seq_id,
const std::unordered_map<std::string, std::vector<uint32_t>> &token_to_offsets) const;
- static void tokenize_string(const std::string& text, bool is_facet, const field& a_field,
+ static void tokenize_string(const std::string& text,
+ const field& a_field,
const std::vector<char>& symbols_to_index,
const std::vector<char>& token_separators,
std::unordered_map<std::string, std::vector<uint32_t>>& token_to_offsets);
- static void tokenize_string_array(const std::vector<std::string>& strings, bool is_facet,
+ static void tokenize_string_array(const std::vector<std::string>& strings,
const field& a_field,
const std::vector<char>& symbols_to_index,
const std::vector<char>& token_separators,
@@ -522,8 +521,7 @@ private:
void initialize_facet_indexes(const field& facet_field);
- void create_facet_hash_index(const field& facet_field);
- static Option<bool> batch_embed_fields(std::vector<nlohmann::json*>& documents,
+ static Option<bool> batch_embed_fields(std::vector<nlohmann::json*>& documents,
const tsl::htrie_map<char, field>& embedding_fields,
const tsl::htrie_map<char, field> & search_schema);
@@ -609,8 +607,7 @@ public:
static void compute_token_offsets_facets(index_record& record,
const tsl::htrie_map<char, field>& search_schema,
const std::vector<char>& local_token_separators,
- const std::vector<char>& local_symbols_to_index,
- std::map<std::string, std::vector<uint32_t>>& facet_hashes);
+ const std::vector<char>& local_symbols_to_index);
static void scrub_reindex_doc(const tsl::htrie_map<char, field>& search_schema,
nlohmann::json& update_doc, nlohmann::json& del_doc, const nlohmann::json& old_doc);
@@ -676,8 +673,7 @@ public:
const std::vector<char>& symbols_to_index,
const bool do_validation);
- void index_field_in_memory(const field& afield, std::vector<index_record>& iter_batch,
- std::map<std::string, std::vector<uint32_t>>& facet_hashes);
+ void index_field_in_memory(const field& afield, std::vector<index_record>& iter_batch);
template<class T>
void iterate_and_index_numerical_field(std::vector<index_record>& iter_batch, const field& afield, T func);
diff --git a/include/num_tree.h b/include/num_tree.h
index c31f0b99..24a169a3 100644
--- a/include/num_tree.h
+++ b/include/num_tree.h
@@ -78,9 +78,9 @@ public:
size_t& result_ids_len,
uint32_t*& result_ids) const;
- size_t intersect(const uint32_t* result_ids, int result_id_len,
- int max_facet_count, std::map<int64_t, uint32_t>& found,
- bool is_wildcard_no_filter_query);
+ size_t intersect(const uint32_t* result_ids, size_t result_id_len,
+ size_t max_facet_count, std::map<int64_t, uint32_t>& found,
+ bool is_wildcard_no_filter_query);
size_t counter_list_size() const;
diff --git a/src/facet_index.cpp b/src/facet_index.cpp
index b180b18d..2a796221 100644
--- a/src/facet_index.cpp
+++ b/src/facet_index.cpp
@@ -6,82 +6,93 @@
void facet_index_t::initialize(const std::string& field) {
const auto facet_field_map_it = facet_field_map.find(field);
if(facet_field_map_it == facet_field_map.end()) {
- facet_field_map.emplace(field, facet_index_counter{});
+ facet_doc_ids_list_t facet_doc_ids_list;
+ facet_field_map.emplace(field, std::move(facet_doc_ids_list));
}
}
-uint32_t facet_index_t::insert(const std::string& field, const std::string& value,
- const std::vector<uint32_t>& ids, bool is_string) {
+void facet_index_t::insert(const std::string& field_name, bool is_string,
+ std::unordered_map<facet_value_id_t, std::vector<uint32_t>, facet_value_id_t::Hash>& fvalue_to_seq_ids,
+ std::unordered_map<uint32_t, std::vector<facet_value_id_t>>& seq_id_to_fvalues) {
- const auto facet_field_map_it = facet_field_map.find(field);
+ const auto facet_field_map_it = facet_field_map.find(field_name);
if(facet_field_map_it == facet_field_map.end()) {
- return 0; //field is not initialized or dropped
+ return; // field is not initialized or dropped
}
-
- uint32_t index = 0;
-
- auto& facet_index_map = facet_field_map_it->second.facet_index_map;
- const auto sv = value.substr(0, 100);
- const auto facet_index_map_it = facet_index_map.find(sv);
- bool inserted_newly = false;
-
- if(facet_index_map_it == facet_index_map.end()) {
- index = ++count_index;
- facet_index_struct fis{};
- fis.index = index;
- if(is_string) {
- fis.id_list_ptr = SET_COMPACT_IDS(compact_id_list_t::create(ids.size(), ids));
- }
- facet_index_map.emplace(sv, fis);
- inserted_newly = true;
- } else {
- index = facet_index_map_it->index;
- if(is_string) {
- auto ids_ptr = facet_index_map_it->id_list_ptr;
- for(const auto& id : ids) {
- if (!ids_t::contains(ids_ptr, id)) {
- ids_t::upsert(ids_ptr, id);
- facet_index_map[sv].id_list_ptr = ids_ptr;
- }
+
+ auto& facet_index = facet_field_map_it->second;
+ tsl::htrie_map<char, facet_id_seq_ids_t>& fvalue_index = facet_index.fvalue_seq_ids;
+ auto fhash_index = facet_index.seq_id_hashes;
+
+ for(const auto& seq_id_fvalues: seq_id_to_fvalues) {
+ auto seq_id = seq_id_fvalues.first;
+ std::vector<uint32_t> real_facet_ids;
+ real_facet_ids.reserve(seq_id_fvalues.second.size());
+
+ for(const auto& fvalue: seq_id_fvalues.second) {
+ uint32_t facet_id = fvalue.facet_id;
+ const auto& fvalue_index_it = fvalue_index.find(fvalue.facet_value);
+
+ if(fvalue.facet_id == UINT32_MAX) {
+ // float, int32 & bool will provide facet_id as their own numerical values
+ facet_id = (fvalue_index_it == fvalue_index.end()) ? ++next_facet_id : fvalue_index_it->facet_id;
}
- }
- }
- if(is_string) {
- const auto facet_count = ids_t::num_ids(facet_index_map.at(sv).id_list_ptr);
- //LOG(INFO) << "Facet count in facet " << sv << " : " << facet_count;
- auto& counter_list = facet_field_map_it->second.counter_list;
+ real_facet_ids.push_back(facet_id);
- if(inserted_newly) {
- count_list* node = new count_list(sv, facet_count, index);
- counter_list.emplace_back(node);
- facet_index_map.at(sv).count_list_index = counter_list.size()-1;
- } else {
- auto ind = facet_index_map_it->count_list_index;
-
- if(counter_list[ind]->index == index) {
- counter_list[ind]->count = facet_count;
- if(ind > 1) {
- auto curr = ind;
- while (curr && (counter_list[curr-1]->count < counter_list[curr]->count)) {
- std::swap(counter_list[curr-1], counter_list[curr]);
- facet_index_map.at(counter_list[curr-1]->facet_value).count_list_index = curr-1;
- facet_index_map.at(counter_list[curr]->facet_value).count_list_index = curr;
- --curr;
+ auto seq_ids_it = fvalue_to_seq_ids.find(fvalue);
+ if(seq_ids_it == fvalue_to_seq_ids.end()) {
+ continue;
+ }
+
+ auto& seq_ids = seq_ids_it->second;
+ std::list<facet_count_t>& count_list = facet_index.counts;
+
+ if(fvalue_index_it == fvalue_index.end()) {
+ facet_id_seq_ids_t fis;
+ fis.facet_id = facet_id;
+
+ if(facet_index.has_value_index) {
+ count_list.emplace_back(fvalue.facet_value, seq_ids.size(), facet_id);
+ fis.facet_count_it = std::prev(count_list.end());
+ if(is_string) {
+ fis.seq_ids = SET_COMPACT_IDS(compact_id_list_t::create(seq_ids.size(), seq_ids));
+ }
+ }
+
+ fvalue_index.emplace(fvalue.facet_value, fis);
+ } else if(facet_index.has_value_index) {
+ if(is_string) {
+ for(const auto id : seq_ids) {
+ ids_t::upsert(fvalue_index_it->seq_ids, id);
+ }
+
+ auto facet_count_it = fvalue_index_it->facet_count_it;
+
+ if(facet_count_it->facet_id == facet_id) {
+ facet_count_it->count = ids_t::num_ids(fvalue_index_it->seq_ids);
+ auto curr = facet_count_it;
+ while (curr != count_list.begin() && std::prev(curr)->count < curr->count) {
+ count_list.splice(curr, count_list, std::prev(curr)); // swaps list nodes
+ curr--;
+ }
+ } else {
+ LOG(ERROR) << "Wrong reference stored for facet " << fvalue.facet_value << " with facet_id " << facet_id;
}
}
- } else {
- LOG(ERROR) << "Wrong count_index stored for facet " << sv << " with index " << index;
}
+
+ fvalue_to_seq_ids.erase(fvalue);
}
- }
- return index;
+ if(facet_index.has_hash_index && fhash_index != nullptr) {
+ fhash_index->upsert(seq_id, real_facet_ids);
+ }
+ }
}
-bool facet_index_t::contains(const std::string& field) {
-
- const auto& facet_field_it = facet_field_map.find(field);
+bool facet_index_t::contains(const std::string& field_name) {
+ const auto& facet_field_it = facet_field_map.find(field_name);
if(facet_field_it == facet_field_map.end()) {
return false;
}
@@ -89,44 +100,58 @@ bool facet_index_t::contains(const std::string& field) {
return true;
}
-void facet_index_t::erase(const std::string& field) {
- const auto it = facet_field_map.find(field);
- if(it != facet_field_map.end()) {
- facet_field_map.erase(field);
- }
+void facet_index_t::erase(const std::string& field_name) {
+ facet_field_map.erase(field_name);
}
-void facet_index_t::remove(const std::string& field, const uint32_t seq_id) {
- const auto facet_field_it = facet_field_map.find(field);
+void facet_index_t::remove(const std::string& field_name, const uint32_t seq_id) {
+ const auto facet_field_it = facet_field_map.find(field_name);
if(facet_field_it != facet_field_map.end()) {
- auto facet_index_map = facet_field_it->second.facet_index_map;
- auto facet_index_map_it = facet_index_map.begin();
-
- for(; facet_index_map_it != facet_index_map.end(); ++facet_index_map_it) {
- void* ids = facet_index_map_it.value().id_list_ptr;
- if(ids && ids_t::contains(ids, seq_id)) {
- // ids_t::erase(ids, seq_id);
- // facet_index_map_it.value().id_list_ptr = ids;
- break;
+ auto& facet_index_map = facet_field_it->second.fvalue_seq_ids;
+ std::vector<std::string> dead_fvalues;
+
+ for(auto facet_ids_seq_ids = facet_index_map.begin(); facet_ids_seq_ids != facet_index_map.end(); facet_ids_seq_ids++) {
+ void*& ids = facet_ids_seq_ids.value().seq_ids;
+ if(ids && ids_t::contains(ids, seq_id)) {
+ ids_t::erase(ids, seq_id);
+ auto& count_list = facet_field_it->second.counts;
+ auto key = facet_ids_seq_ids.key();
+ auto& facet_id_seq_ids = facet_ids_seq_ids.value();
+ facet_ids_seq_ids.value().facet_count_it->count--;
+
+ if(ids_t::num_ids(ids) == 0) {
+ ids_t::destroy_list(ids);
+ std::string dead_fvalue;
+ facet_ids_seq_ids.key(dead_fvalue);
+ dead_fvalues.push_back(dead_fvalue);
+ count_list.erase(facet_ids_seq_ids.value().facet_count_it);
}
+ }
}
+
+ for(auto& dead_fvalue: dead_fvalues) {
+ facet_index_map.erase(dead_fvalue);
+ }
+
+ auto& seq_id_hashes = facet_field_it->second.seq_id_hashes;
+ seq_id_hashes->erase(seq_id);
}
}
-size_t facet_index_t::get_facet_count(const std::string& field) {
- const auto it = facet_field_map.find(field);
+size_t facet_index_t::get_facet_count(const std::string& field_name) {
+ const auto it = facet_field_map.find(field_name);
if(it == facet_field_map.end()) {
return 0;
}
- return it->second.counter_list.size();
+ return has_hash_index(field_name) ? it->second.seq_id_hashes->num_ids() : it->second.counts.size();
}
//returns the count of matching seq_ids from result array
-size_t facet_index_t::intersect(const std::string& field, const uint32_t* result_ids,
- int result_ids_len, int max_facet_count,
- std::map<std::string, uint32_t>& found, bool is_wildcard_no_filter_query) {
+size_t facet_index_t::intersect(const std::string& field, const uint32_t* result_ids, size_t result_ids_len,
+ size_t max_facet_count, std::map<std::string, uint32_t>& found,
+ bool is_wildcard_no_filter_query) {
//LOG (INFO) << "intersecting field " << field;
const auto& facet_field_it = facet_field_map.find(field);
@@ -134,26 +159,25 @@ size_t facet_index_t::intersect(const std::string& field, const uint32_t* result
return 0;
}
- const auto facet_index_map = facet_field_it->second.facet_index_map;
- const auto& counter_list = facet_field_it->second.counter_list;
+ const auto& facet_index_map = facet_field_it->second.fvalue_seq_ids;
+ const auto& counter_list = facet_field_it->second.counts;
- // LOG (INFO) << "facet_index_map size " << facet_index_map.size()
- // << " , counter_list size " << counter_list.size();
+ // LOG (INFO) << "fvalue_seq_ids size " << fvalue_seq_ids.size()
+ // << " , counts size " << counts.size();
size_t max_facets = std::min((size_t)2 * max_facet_count, counter_list.size());
std::vector<uint32_t> id_list;
- for(const auto& counter_list_it : counter_list) {
+ for(const auto& facet_count : counter_list) {
// LOG (INFO) << "checking ids in facet_value " << counter_list_it.facet_value
// << " having total count " << counter_list_it.count;
uint32_t count = 0;
if(is_wildcard_no_filter_query) {
- count = counter_list_it->count;
+ count = facet_count.count;
} else {
- auto ids = facet_index_map.at(counter_list_it->facet_value).id_list_ptr;
+ auto ids = facet_index_map.at(facet_count.facet_value).seq_ids;
ids_t::uncompress(ids, id_list);
- const auto ids_len = id_list.size();
- for(int i = 0; i < result_ids_len; ++i) {
+ for(size_t i = 0; i < result_ids_len; ++i) {
uint32_t* out = nullptr;
count = ArrayUtils::and_scalar(id_list.data(), id_list.size(),
result_ids, result_ids_len, &out);
@@ -163,7 +187,7 @@ size_t facet_index_t::intersect(const std::string& field, const uint32_t* result
}
if(count) {
- found[counter_list_it->facet_value] = count;
+ found[facet_count.facet_value] = count;
if(found.size() == max_facets) {
break;
}
@@ -177,28 +201,26 @@ facet_index_t::~facet_index_t() {
facet_field_map.clear();
}
- //used for migrating string and int64 facets
-size_t facet_index_t::get_facet_indexes(const std::string& field,
+// used for migrating string and int64 facets
+size_t facet_index_t::get_facet_indexes(const std::string& field_name,
std::map<uint32_t, std::vector<uint32_t>>& seqid_countIndexes) {
- const auto& facet_field_it = facet_field_map.find(field);
+ const auto& facet_field_it = facet_field_map.find(field_name);
if(facet_field_it == facet_field_map.end()) {
return 0;
}
- auto& facet_index_map = facet_field_it->second.facet_index_map;
+ auto& facet_index_map = facet_field_it->second.fvalue_seq_ids;
std::vector<uint32_t> id_list;
- for(auto facet_index_map_it = facet_index_map.begin();
- facet_index_map_it != facet_index_map.end(); ++facet_index_map_it) {
-
- auto ids = facet_index_map_it->id_list_ptr;
+ for(auto facet_index_map_it = facet_index_map.begin(); facet_index_map_it != facet_index_map.end(); ++facet_index_map_it) {
+ auto ids = facet_index_map_it->seq_ids;
ids_t::uncompress(ids, id_list);
- //emplacing seq_id=>count_index
+ // emplacing seq_id => next_facet_id
for(const auto& id : id_list) {
- seqid_countIndexes[id].emplace_back(facet_index_map_it->index);
+ seqid_countIndexes[id].emplace_back(facet_index_map_it->facet_id);
}
id_list.clear();
@@ -206,3 +228,66 @@ size_t facet_index_t::get_facet_indexes(const std::string& field,
return seqid_countIndexes.size();
}
+
+void facet_index_t::handle_index_change(const std::string& field_name, size_t total_num_docs,
+ size_t facet_index_threshold, size_t facet_count,
+ spp::sparse_hash_map<std::string, num_tree_t*>& numerical_index) {
+
+ // Low cardinality fields will have only value based facet index. Once a field becomes a high cardinality
+ // field (exceeding FACET_INDEX_THRESHOLD), we will create a hash based index and populate it.
+ // If a field is an id-like field (cardinality_ratio < 5) we will then remove value based index.
+
+ auto& facet_index = facet_field_map.at(field_name);
+ posting_list_t*& fhash_index = facet_index.seq_id_hashes;
+
+ if(fhash_index == nullptr && (facet_count > facet_index_threshold) && total_num_docs < 1000000) {
+ fhash_index = new posting_list_t(256);
+ std::map<uint32_t, std::vector<uint32_t>> seq_id_index_map;
+
+ if(get_facet_indexes(field_name, seq_id_index_map)) {
+ for(const auto& kv : seq_id_index_map) {
+ fhash_index->upsert(kv.first, kv.second);
+ }
+ }
+
+ seq_id_index_map.clear();
+
+ auto numerical_index_it = numerical_index.find(field_name);
+ if(numerical_index_it != numerical_index.end()) {
+ auto num_tree = numerical_index_it->second;
+ if(num_tree->get_facet_indexes(seq_id_index_map)) {
+ for(const auto& kv : seq_id_index_map) {
+ fhash_index->upsert(kv.first, kv.second);
+ }
+ }
+ }
+
+ facet_index.has_hash_index = true;
+
+ auto cardinality_ratio = total_num_docs / facet_count;
+
+ if(cardinality_ratio != 0 && cardinality_ratio < 5) {
+ // drop the value index for this field
+ auto& fvalue_seq_ids = facet_index.fvalue_seq_ids;
+ for(auto it = fvalue_seq_ids.begin(); it != fvalue_seq_ids.end(); ++it) {
+ ids_t::destroy_list(it.value().seq_ids);
+ }
+ fvalue_seq_ids.clear();
+ facet_index.counts.clear();
+ facet_index.has_value_index = false;
+ }
+ }
+}
+
+bool facet_index_t::has_hash_index(const std::string &field_name) {
+ auto facet_index_it = facet_field_map.find(field_name);
+ return facet_index_it != facet_field_map.end() && facet_index_it->second.has_hash_index;
+}
+
+posting_list_t* facet_index_t::get_facet_hash_index(const std::string &field_name) {
+ auto facet_index_it = facet_field_map.find(field_name);
+ if(facet_index_it != facet_field_map.end()) {
+ return facet_index_it->second.seq_id_hashes;
+ }
+ return nullptr;
+}
diff --git a/src/index.cpp b/src/index.cpp
index 47e97945..d268a173 100644
--- a/src/index.cpp
+++ b/src/index.cpp
@@ -45,16 +45,6 @@ spp::sparse_hash_map<uint32_t, int64_t> Index::eval_sentinel_value;
spp::sparse_hash_map<uint32_t, int64_t> Index::geo_sentinel_value;
spp::sparse_hash_map<uint32_t, int64_t> Index::str_sentinel_value;
-struct token_posting_t {
- uint32_t token_id;
- const posting_list_t::iterator_t& posting;
-
- token_posting_t(uint32_t token_id, const posting_list_t::iterator_t& posting):
- token_id(token_id), posting(posting) {
-
- }
-};
-
Index::Index(const std::string& name, const uint32_t collection_id, const Store* store,
SynonymIndex* synonym_index, ThreadPool* thread_pool,
const tsl::htrie_map<char, field> & search_schema,
@@ -187,11 +177,6 @@ Index::~Index() {
str_sort_index.clear();
- for(const auto& facet_hash_index_it : facet_hash_index) {
- delete facet_hash_index_it.second;
- }
- facet_hash_index.clear();
-
delete facet_index_v4;
delete seq_ids;
@@ -246,8 +231,7 @@ float Index::int64_t_to_float(int64_t n) {
void Index::compute_token_offsets_facets(index_record& record,
const tsl::htrie_map<char, field>& search_schema,
const std::vector<char>& local_token_separators,
- const std::vector<char>& local_symbols_to_index,
- std::map<std::string, std::vector<uint32_t>>& facet_hashes) {
+ const std::vector<char>& local_symbols_to_index) {
const auto& document = record.doc;
@@ -257,7 +241,7 @@ void Index::compute_token_offsets_facets(index_record& record,
continue;
}
- std::unordered_map<std::string, std::vector<uint32_t>> offsets;
+ offsets_facet_hashes_t offset_facet_hashes;
bool is_facet = search_schema.at(field_name).facet;
@@ -269,32 +253,28 @@ void Index::compute_token_offsets_facets(index_record& record,
if(the_field.type == field_types::INT32_ARRAY) {
for(int32_t value: document[field_name]){
auto str = std::to_string(value);
- facet_hashes[str].emplace_back(record.seq_id);
strings.emplace_back(std::move(str));
}
} else if(the_field.type == field_types::INT64_ARRAY) {
for(int64_t value: document[field_name]){
auto str = std::to_string(value);
- facet_hashes[str].emplace_back(record.seq_id);
strings.emplace_back(std::move(str));
}
} else if(the_field.type == field_types::FLOAT_ARRAY) {
for(float value: document[field_name]){
auto str = StringUtils::float_to_str(value);
- facet_hashes[str].emplace_back(record.seq_id);
strings.emplace_back(std::move(str));
}
} else if(the_field.type == field_types::BOOL_ARRAY) {
for(bool value: document[field_name]){
auto str = std::to_string(value);
- facet_hashes[str].emplace_back(record.seq_id);
strings.emplace_back(std::move(str));
}
}
- tokenize_string_array(strings, is_facet, the_field,
+ tokenize_string_array(strings, the_field,
local_symbols_to_index, local_token_separators,
- offsets);
+ offset_facet_hashes.offsets);
} else {
std::string text;
@@ -312,39 +292,26 @@ void Index::compute_token_offsets_facets(index_record& record,
text = std::to_string(val);
}
- facet_hashes[text].emplace_back(record.seq_id);
-
- tokenize_string(text, is_facet, the_field,
+ tokenize_string(text, the_field,
local_symbols_to_index, local_token_separators,
- offsets);
+ offset_facet_hashes.offsets);
}
}
if(the_field.is_string()) {
-
if(the_field.type == field_types::STRING) {
- tokenize_string(document[field_name], is_facet, the_field,
+ tokenize_string(document[field_name], the_field,
local_symbols_to_index, local_token_separators,
- offsets);
- std::string val = document[field_name];
- if(!val.empty()) {
- facet_hashes[val].emplace_back(record.seq_id);
- }
+ offset_facet_hashes.offsets);
} else {
- tokenize_string_array(document[field_name], is_facet, the_field,
+ tokenize_string_array(document[field_name], the_field,
local_symbols_to_index, local_token_separators,
- offsets);
-
- for(std::string val : document[field_name]) {
- if(!val.empty()) {
- facet_hashes[val].emplace_back(record.seq_id);
- }
- }
+ offset_facet_hashes.offsets);
}
}
- if(!offsets.empty()) {
- record.field_index.emplace(field_name, std::move(offsets));
+ if(!offset_facet_hashes.offsets.empty()) {
+ record.field_index.emplace(field_name, std::move(offset_facet_hashes));
}
}
}
@@ -425,7 +392,8 @@ bool validate_object_field(nlohmann::json& doc, const field& a_field) {
return false;
}
-void Index::validate_and_preprocess(Index *index, std::vector<index_record>& iter_batch,
+void Index::validate_and_preprocess(Index *index,
+ std::vector<index_record>& iter_batch,
const size_t batch_start_index, const size_t batch_size,
const std::string& default_sorting_field,
const tsl::htrie_map<char, field>& search_schema,
@@ -440,7 +408,6 @@ void Index::validate_and_preprocess(Index *index, std::vector<index_record>& ite
for(size_t i = 0; i < batch_size; i++) {
index_record& index_rec = iter_batch[batch_start_index + i];
- auto& facet_hashes = index_rec.facet_hashes;
try {
if(!index_rec.indexed.ok()) {
@@ -492,7 +459,7 @@ void Index::validate_and_preprocess(Index *index, std::vector<index_record>& ite
docs_to_embed.push_back(&index_rec.doc);
}
- compute_token_offsets_facets(index_rec, search_schema, token_separators, symbols_to_index, facet_hashes);
+ compute_token_offsets_facets(index_rec, search_schema, token_separators, symbols_to_index);
int64_t points = 0;
@@ -529,7 +496,8 @@ void Index::validate_and_preprocess(Index *index, std::vector<index_record>& ite
}
}
-size_t Index::batch_memory_index(Index *index, std::vector<index_record>& iter_batch,
+size_t Index::batch_memory_index(Index *index,
+ std::vector<index_record>& iter_batch,
const std::string & default_sorting_field,
const tsl::htrie_map<char, field> & search_schema,
const tsl::htrie_map<char, field> & embedding_fields,
@@ -583,7 +551,6 @@ size_t Index::batch_memory_index(Index *index, std::vector<index_record>& iter_b
}
std::unordered_set<std::string> found_fields;
- std::map<std::string, std::vector<uint32_t>> facet_hashes;
for(size_t i = 0; i < iter_batch.size(); i++) {
auto& index_rec = iter_batch[i];
@@ -602,14 +569,6 @@ size_t Index::batch_memory_index(Index *index, std::vector<index_record>& iter_b
for(const auto& kv: index_rec.doc.items()) {
found_fields.insert(kv.key());
}
-
- for(auto& kv : index_rec.facet_hashes) {
- auto end = facet_hashes[kv.first].end();
-
- facet_hashes[kv.first].insert(end, std::make_move_iterator(kv.second.begin()),
- std::make_move_iterator(kv.second.end()));
- kv.second.clear();
- }
}
num_queued = num_processed = 0;
@@ -628,7 +587,7 @@ size_t Index::batch_memory_index(Index *index, std::vector<index_record>& iter_b
const field& f = (field_name == "id") ?
field("id", field_types::STRING, false) : search_schema.at(field_name);
try {
- index->index_field_in_memory(f, iter_batch, facet_hashes);
+ index->index_field_in_memory(f, iter_batch);
} catch(std::exception& e) {
LOG(ERROR) << "Unhandled Typesense error: " << e.what();
for(auto& record: iter_batch) {
@@ -650,33 +609,7 @@ size_t Index::batch_memory_index(Index *index, std::vector<index_record>& iter_b
return num_indexed;
}
-void Index::create_facet_hash_index(const field& facet_field) {
-
- //LOG(INFO) << "migrating facet " << field << " to old index";
- std::map<uint32_t, std::vector<uint32_t>> seqid_index_map;
- auto facet_index = facet_hash_index[facet_field.name];
-
- if(facet_index_v4->get_facet_indexes(facet_field.name, seqid_index_map)) {
- for(const auto& kv : seqid_index_map) {
- facet_index->upsert(kv.first, std::move(kv.second));
- }
- }
-
- seqid_index_map.clear();
-
- auto numerical_index_it = numerical_index.find(facet_field.name);
- if(numerical_index_it != numerical_index_it) {
- auto num_tree = numerical_index_it->second;
- if(num_tree->get_facet_indexes(seqid_index_map)) {
- for(const auto& kv : seqid_index_map) {
- facet_index->upsert(kv.first, std::move(kv.second));
- }
- }
- }
-}
-
-void Index::index_field_in_memory(const field& afield, std::vector<index_record>& iter_batch,
- std::map<std::string, std::vector<uint32_t>>& facet_hashes) {
+void Index::index_field_in_memory(const field& afield, std::vector<index_record>& iter_batch) {
// indexes a given field of all documents in the batch
if(afield.name == "id") {
@@ -709,40 +642,29 @@ void Index::index_field_in_memory(const field& afield, std::vector<index_record>
std::unordered_map<std::string, std::vector<art_document>> token_to_doc_offsets;
int64_t max_score = INT64_MIN;
+ std::unordered_map<facet_value_id_t, std::vector<uint32_t>, facet_value_id_t::Hash> fvalue_to_seq_ids;
+ std::unordered_map<uint32_t, std::vector<facet_value_id_t>> seq_id_to_fvalues;
- auto facet_threshold_count = 0;
+ auto facet_count = 0;
if(afield.is_string()) {
- facet_threshold_count = facet_index_v4->get_facet_count(afield.name);
+ facet_count = facet_index_v4->get_facet_count(afield.name);
} else {
auto numerical_index_it = numerical_index.find(afield.name);
if(numerical_index_it != numerical_index.end()) {
- facet_threshold_count = numerical_index_it->second->counter_list_size();
+ facet_count = numerical_index_it->second->counter_list_size();
}
}
#ifdef TEST_BUILD
- facet_threshold_count = FACET_INDEX_THRESHOLD + 1;
+ facet_count = FACET_INDEX_THRESHOLD + 1;
#endif
- const auto total_num_docs = aggragate_docs_count.load();
- bool facet_hash_index_exist = facet_hash_index.find(afield.name) != facet_hash_index.end();
-
- if(!facet_hash_index_exist && (facet_threshold_count > FACET_INDEX_THRESHOLD)
- && total_num_docs < 1000000) {
- create_facet_hash_index(afield);
- auto cardinality_ratio = total_num_docs / facet_threshold_count;
- if(cardinality_ratio < 5) {
- //drop the field
- facet_index_v4->erase(afield.name);
- }
+ if(afield.facet) {
+ size_t total_num_docs = seq_ids->num_ids();
+ facet_index_v4->handle_index_change(afield.name, total_num_docs, FACET_INDEX_THRESHOLD, facet_count,
+ numerical_index);
}
- posting_list_t* facet_index = nullptr;
- auto facet_index_it = facet_hash_index.find(afield.name);
- if(facet_index_it != facet_hash_index.end()) {
- facet_index = facet_index_it->second;
- }
-
for(const auto& record: iter_batch) {
if(!record.indexed.ok()) {
// some records could have been invalidated upstream
@@ -760,78 +682,75 @@ void Index::index_field_in_memory(const field& afield, std::vector<index_record>
if(field_index_it == record.field_index.end()) {
continue;
}
-
- std::string value="";
- uint32_t fhash = 0;
if(afield.facet) {
if(afield.is_array()) {
- std::vector<uint32_t> fhashvalues;
- for(size_t i = 0; i < document[afield.name].size(); ++i) {
+ const auto& field_values = document[afield.name];
+ for(size_t i = 0; i < field_values.size(); i++) {
if(afield.type == field_types::INT32_ARRAY) {
- int32_t raw_val = document[afield.name][i].get<int32_t>();
- fhash = reinterpret_cast<uint32_t&>(raw_val);
+ int32_t raw_val = field_values[i].get<int32_t>();
+ auto fhash = reinterpret_cast<uint32_t&>(raw_val);
+ facet_value_id_t facet_value_id(std::to_string(raw_val), fhash);
+ fvalue_to_seq_ids[facet_value_id].push_back(seq_id);
+ seq_id_to_fvalues[seq_id].push_back(facet_value_id);
} else if(afield.type == field_types::INT64_ARRAY) {
- int64_t raw_val = document[afield.name][i].get<int64_t>();
- value = std::to_string(raw_val);
- fhash = facet_index_v4->insert(afield.name, value, facet_hashes[value]);
+ int64_t raw_val = field_values[i].get<int64_t>();
+ facet_value_id_t facet_value_id(std::to_string(raw_val));
+ fvalue_to_seq_ids[facet_value_id].push_back(seq_id);
+ seq_id_to_fvalues[seq_id].push_back(facet_value_id);
} else if(afield.type == field_types::STRING_ARRAY) {
- value = document[afield.name][i];
- fhash = facet_index_v4->insert(afield.name, value, facet_hashes[value], true);
+ const std::string& raw_val = field_values[i].get<std::string>().substr(0, 100);
+ facet_value_id_t facet_value_id(raw_val);
+ fvalue_to_seq_ids[facet_value_id].push_back(seq_id);
+ seq_id_to_fvalues[seq_id].push_back(facet_value_id);
} else if(afield.type == field_types::FLOAT_ARRAY) {
- float raw_val = document[afield.name][i].get<float>();
- fhash = reinterpret_cast<uint32_t&>(raw_val);
+ float raw_val = field_values[i].get<float>();
+ auto fhash = reinterpret_cast<uint32_t&>(raw_val);
+ facet_value_id_t facet_value_id(std::to_string(raw_val), fhash);
+ fvalue_to_seq_ids[facet_value_id].push_back(seq_id);
+ seq_id_to_fvalues[seq_id].push_back(facet_value_id);
} else if(afield.type == field_types::BOOL_ARRAY) {
- bool raw_val = document[afield.name][i].get<bool>();
- fhash = (uint32_t)raw_val;
- }
-
- if(facet_threshold_count > FACET_INDEX_THRESHOLD) {
- fhashvalues.emplace_back(fhash);
+ bool raw_val = field_values[i].get<bool>();
+ auto fhash = (uint32_t)raw_val;
+ facet_value_id_t facet_value_id(std::to_string(raw_val), fhash);
+ fvalue_to_seq_ids[facet_value_id].push_back(seq_id);
+ seq_id_to_fvalues[seq_id].push_back(facet_value_id);
}
}
-
- if(facet_index!=nullptr) {
- if (facet_threshold_count > FACET_INDEX_THRESHOLD) {
- facet_index->upsert(seq_id, std::move(fhashvalues));
- fhashvalues.clear();
- }
- } else {
- LOG(ERROR) << "facet_index was null while inserting for facet " << afield.name;
- }
} else {
- uint32_t fhash;
-
if(afield.type == field_types::INT32) {
int32_t raw_val = document[afield.name].get<int32_t>();
- fhash = reinterpret_cast<uint32_t&>(raw_val);
+ auto fhash = reinterpret_cast<uint32_t&>(raw_val);
+ facet_value_id_t facet_value_id(std::to_string(raw_val), fhash);
+ fvalue_to_seq_ids[facet_value_id].push_back(seq_id);
+ seq_id_to_fvalues[seq_id].push_back(facet_value_id);
}
else if(afield.type == field_types::INT64) {
int64_t raw_val = document[afield.name].get<int64_t>();
- value = std::to_string(raw_val);
- fhash = facet_index_v4->insert(afield.name, value, facet_hashes[value]);
+ facet_value_id_t facet_value_id(std::to_string(raw_val));
+ fvalue_to_seq_ids[facet_value_id].push_back(seq_id);
+ seq_id_to_fvalues[seq_id].push_back(facet_value_id);
}
else if(afield.type == field_types::STRING) {
- value = document[afield.name];
- fhash = facet_index_v4->insert(afield.name, value, facet_hashes[value], true);
+ const std::string& raw_val = document[afield.name].get<std::string>().substr(0, 100);
+ facet_value_id_t facet_value_id(raw_val);
+ fvalue_to_seq_ids[facet_value_id].push_back(seq_id);
+ seq_id_to_fvalues[seq_id].push_back(facet_value_id);
}
else if(afield.type == field_types::FLOAT) {
float raw_val = document[afield.name].get<float>();
- fhash = reinterpret_cast<uint32_t&>(raw_val);
+ auto fhash = reinterpret_cast<uint32_t&>(raw_val);
+ facet_value_id_t facet_value_id(std::to_string(raw_val), fhash);
+ fvalue_to_seq_ids[facet_value_id].push_back(seq_id);
+ seq_id_to_fvalues[seq_id].push_back(facet_value_id);
}
else if(afield.type == field_types::BOOL) {
bool raw_val = document[afield.name].get<bool>();
- fhash = (uint32_t)raw_val;
+ auto fhash = (uint32_t)raw_val;
+ facet_value_id_t facet_value_id(std::to_string(raw_val), fhash);
+ fvalue_to_seq_ids[facet_value_id].push_back(seq_id);
+ seq_id_to_fvalues[seq_id].push_back(facet_value_id);
}
-
- if(facet_index!=nullptr) {
- if (facet_threshold_count > FACET_INDEX_THRESHOLD) {
- facet_index->upsert(seq_id, {fhash});
- }
- } else {
- LOG(ERROR) << "facet_index was null while inserting for facet " << afield.name;
- }
-
}
}
@@ -839,7 +758,7 @@ void Index::index_field_in_memory(const field& afield, std::vector<index_record>
max_score = record.points;
}
- for(auto &token_offsets: field_index_it->second) {
+ for(auto& token_offsets: field_index_it->second.offsets) {
token_to_doc_offsets[token_offsets.first].emplace_back(seq_id, record.points, token_offsets.second);
if(afield.infix) {
@@ -850,6 +769,8 @@ void Index::index_field_in_memory(const field& afield, std::vector<index_record>
}
}
+ facet_index_v4->insert(afield.name, afield.is_string(), fvalue_to_seq_ids, seq_id_to_fvalues);
+
auto tree_it = search_index.find(afield.faceted_name());
if(tree_it == search_index.end()) {
return;
@@ -1121,12 +1042,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,
+void Index::tokenize_string(const std::string& text, const field& a_field,
const std::vector<char>& symbols_to_index,
const std::vector<char>& token_separators,
std::unordered_map<std::string, std::vector<uint32_t>>& token_to_offsets) {
@@ -1155,7 +1073,7 @@ void Index::tokenize_string(const std::string& text, bool is_facet, const field&
}
}
-void Index::tokenize_string_array(const std::vector<std::string>& strings, bool is_facet,
+void Index::tokenize_string_array(const std::vector<std::string>& strings,
const field& a_field,
const std::vector<char>& symbols_to_index,
const std::vector<char>& token_separators,
@@ -1201,15 +1119,7 @@ void Index::tokenize_string_array(const std::vector<std::string>& strings, bool
}
}
void Index::initialize_facet_indexes(const field& facet_field) {
-
- if(facet_field.is_string() || facet_field.is_int64()) {
- facet_index_v4->initialize(facet_field.name);
- }
-
- //LOG(INFO) << "emplacing facet " << facet_field.name;
- if(facet_hash_index.find(facet_field.name) == facet_hash_index.end()) {
- facet_hash_index.emplace(facet_field.name, new posting_list_t(256));
- }
+ facet_index_v4->initialize(facet_field.name);
}
void Index::compute_facet_stats(facet &a_facet, const std::string& raw_value, const std::string & field_type) {
@@ -1301,6 +1211,11 @@ void Index::do_facets(std::vector<facet> & facets, facet_query_t & facet_query,
const uint32_t* result_ids, size_t results_size,
int max_facet_count, bool is_wildcard_query, bool no_filters_provided,
bool use_facet_intersection) const {
+
+ if(results_size == 0) {
+ return ;
+ }
+
// assumed that facet fields have already been validated upstream
for(size_t findex=0; findex < facets.size(); findex++) {
auto& a_facet = facets[findex];
@@ -1315,16 +1230,14 @@ void Index::do_facets(std::vector<facet> & facets, facet_query_t & facet_query,
auto num_facet_values = 0;
- if(!facet_field.name.empty()) {
- if(facet_field.is_string()) {
- num_facet_values = facet_index_v4->get_facet_count(a_facet.field_name);
+ if(facet_field.is_string()) {
+ num_facet_values = facet_index_v4->get_facet_count(a_facet.field_name);
+ } else {
+ auto numerical_index_it = numerical_index.find(a_facet.field_name);
+ if(numerical_index_it != numerical_index.end()) {
+ num_facet_values = numerical_index_it->second->counter_list_size();
} else {
- auto numerical_index_it = numerical_index.find(a_facet.field_name);
- if(numerical_index_it != numerical_index.end()) {
- num_facet_values = numerical_index_it->second->counter_list_size();
- } else {
- LOG(ERROR) << "facet " << a_facet.field_name << " not found in numerical index";
- }
+ LOG(ERROR) << "facet " << a_facet.field_name << " not found in numerical index";
}
}
@@ -1339,38 +1252,35 @@ void Index::do_facets(std::vector<facet> & facets, facet_query_t & facet_query,
use_facet_intersection = false;
use_hashes = false;
#endif
- const auto& facet_hash_index_it = facet_hash_index.find(a_facet.field_name);
-
bool is_wildcard_no_filter_query = is_wildcard_query && no_filters_provided;
- bool facet_hash_index_exists = facet_hash_index_it != facet_hash_index.end();
+ bool facet_hash_index_exists = facet_index_v4->has_hash_index(a_facet.field_name);
- if(results_size && num_facet_values && ((!facet_hash_index_exists || is_wildcard_no_filter_query
- || num_facet_values > 50000) && group_limit == 0) && !use_hashes || use_facet_intersection) {
+ if((num_facet_values && ((!facet_hash_index_exists || is_wildcard_no_filter_query
+ || num_facet_values > 50000) && group_limit == 0) && !use_hashes) || use_facet_intersection) {
//LOG(INFO) << "Using intersection to find facets";
a_facet.is_intersected = true;
std::map<std::string, uint32_t> facet_results;
- 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_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_no_filter_query);
-
- for(const auto& kv : facet_counts) {
- std::string val;
- if(facet_field.is_float()) {
- val = StringUtils::float_to_str(int64_t_to_float(kv.first));
- } else if(facet_field.is_bool()) {
- val = kv.first == 1 ? "true" : "false";
- } else {
- val = std::to_string(kv.first);
- }
- facet_results[val] = kv.second;
+ if(facet_field.is_string()) {
+ facet_index_v4->intersect(a_facet.field_name, result_ids,
+ 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_no_filter_query);
+
+ for(const auto& kv : facet_counts) {
+ std::string val;
+ if(facet_field.is_float()) {
+ val = StringUtils::float_to_str(int64_t_to_float(kv.first));
+ } else if(facet_field.is_bool()) {
+ val = kv.first == 1 ? "true" : "false";
+ } else {
+ val = std::to_string(kv.first);
}
+
+ facet_results[val] = kv.second;
}
}
@@ -1411,7 +1321,7 @@ void Index::do_facets(std::vector<facet> & facets, facet_query_t & facet_query,
if(should_compute_stats) {
//LOG(INFO) << "Computing facet stas for facet " << a_facet.field_name;
- for(int i = 0; i < kv.second; ++i) {
+ for(size_t i = 0; i < kv.second; ++i) {
compute_facet_stats(a_facet, kv.first, facet_field.type);
}
}
@@ -1421,15 +1331,15 @@ void Index::do_facets(std::vector<facet> & facets, facet_query_t & facet_query,
single_val_facet_map_t::iterator single_facet_map_it;
if(!facet_hash_index_exists) {
- continue;
+ continue;
}
auto sort_index_it = sort_index.find(a_facet.field_name);
const auto facet_field_is_array = facet_field.is_array();
- const auto& facet_index = facet_hash_index_it->second;
- std::vector<uint32_t> facet_hashes;
+ const auto& facet_index = facet_index_v4->get_facet_hash_index(facet_field.name);
posting_list_t::iterator_t facet_index_it = facet_index->new_iterator();
+ std::vector<uint32_t> facet_hashes;
for(size_t i = 0; i < results_size; i++) {
// if sampling is enabled, we will skip a portion of the results to speed up things
@@ -4559,7 +4469,7 @@ void Index::compute_facet_infos(const std::vector<facet>& facets, facet_query_t&
for(size_t findex=0; findex < facets.size(); findex++) {
const auto& a_facet = facets[findex];
- bool facet_hash_index_exist = facet_hash_index.find(a_facet.field_name) != facet_hash_index.end();
+ bool has_hash_index = facet_index_v4->has_hash_index(a_facet.field_name);
facet_infos[findex].use_facet_query = false;
@@ -4657,11 +4567,11 @@ void Index::compute_facet_infos(const std::vector<facet>& facets, facet_query_t&
if(!use_facet_intersection) {
#else
//checking if facets is migrated to old index
- if(facet_hash_index_exist) {
+ if(has_hash_index) {
#endif
std::vector<uint32_t> facet_hashes;
- auto facet_index = facet_hash_index.at(a_facet.field_name);
+ auto facet_index = facet_index_v4->get_facet_hash_index(a_facet.field_name);
posting_list_t::iterator_t facet_index_it = facet_index->new_iterator();
facet_index_it.skip_to(seq_id);
@@ -5425,22 +5335,22 @@ void Index::score_results(const std::vector<sort_by> & sort_fields, const uint16
uint64_t Index::get_distinct_id(const std::vector<std::string>& group_by_fields,
const uint32_t seq_id) const {
uint32_t distinct_id = 1; // some constant initial value
+
// calculate hash from group_by_fields
- for(const auto& field: group_by_fields) {
- const auto& facet_hash_index_it = facet_hash_index.find(field);
- if(facet_hash_index_it == facet_hash_index.end()) {
+ for(const auto& field_name: group_by_fields) {
+ if(!facet_index_v4->has_hash_index(field_name)) {
continue;
}
- auto facet_index = facet_hash_index_it->second;
+ posting_list_t* facet_hash_index = facet_index_v4->get_facet_hash_index(field_name);
std::vector<uint32_t> facet_hashes;
- posting_list_t::iterator_t facet_index_it = facet_index->new_iterator();
+ posting_list_t::iterator_t facet_index_it = facet_hash_index->new_iterator();
facet_index_it.skip_to(seq_id);
if(facet_index_it.valid()) {
posting_list_t::get_offsets(facet_index_it, facet_hashes);
- if(search_schema.at(field).is_array()) {
+ if(search_schema.at(field_name).is_array()) {
//LOG(INFO) << "combining hashes for facet array ";
for(size_t i = 0; i < facet_hashes.size(); i++) {
distinct_id = StringUtils::hash_combine(distinct_id, facet_hashes[i]);
@@ -5686,15 +5596,6 @@ void Index::remove_field(uint32_t seq_id, const nlohmann::json& document, const
}
// remove facets
- const auto& facet_hash_index_it = facet_hash_index.find(field_name);
- if(facet_hash_index_it != facet_hash_index.end()) {
- auto facet_index = facet_hash_index_it->second;
- if(facet_index) {
- facet_index->erase(seq_id);
- }
- }
-
-
facet_index_v4->remove(field_name, seq_id);
// remove sort field
@@ -5897,9 +5798,6 @@ void Index::refresh_schemas(const std::vector<field>& new_fields, const std::vec
}
if(del_field.is_facet()) {
- delete facet_hash_index.at(del_field.name);
- facet_hash_index.erase(del_field.name);
-
facet_index_v4->erase(del_field.name);
if(!del_field.is_string()) {
@@ -6346,4 +6244,3 @@ void Index::transform_for_180th_meridian(GeoCoord &point, double offset) {
point.lon = point.lon < 0.0 ? point.lon + offset : point.lon;
}
*/
-
diff --git a/src/num_tree.cpp b/src/num_tree.cpp
index 9b2fc866..30c2c754 100644
--- a/src/num_tree.cpp
+++ b/src/num_tree.cpp
@@ -24,7 +24,7 @@ void num_tree_t::insert(int64_t value, uint32_t id, bool is_facet) {
count_list node(value, facet_count);
auto ind = 0;
-
+ // FIXME
for(; ind < counter_list.size(); ++ind) {
if(counter_list[ind].facet_value == value) {
counter_list[ind].count = facet_count;
@@ -392,12 +392,12 @@ num_tree_t::~num_tree_t() {
}
}
-size_t num_tree_t::intersect(const uint32_t* result_ids, int result_ids_len, int max_facet_count,
- std::map<int64_t, uint32_t>& found, bool is_wildcard_no_filter_query) {
+size_t num_tree_t::intersect(const uint32_t* result_ids, size_t result_ids_len, size_t max_facet_count,
+ std::map<int64_t, uint32_t>& found, bool is_wildcard_no_filter_query) {
//LOG (INFO) << "intersecting field " << field;
// LOG (INFO) << "int64map size " << int64map.size()
- // << " , counter_list size " << counter_list.size();
+ // << " , counts size " << counts.size();
std::vector<uint32_t> id_list;
for(const auto& counter_list_it : counter_list) {
@@ -411,7 +411,7 @@ size_t num_tree_t::intersect(const uint32_t* result_ids, int result_ids_len, int
auto ids = int64map.at(counter_list_it.facet_value);
ids_t::uncompress(ids, id_list);
const auto ids_len = id_list.size();
- for(int i = 0; i < result_ids_len; ++i) {
+ for(size_t i = 0; i < result_ids_len; ++i) {
uint32_t* out = nullptr;
count = ArrayUtils::and_scalar(id_list.data(), id_list.size(),
result_ids, result_ids_len, &out);
@@ -445,7 +445,7 @@ size_t num_tree_t::get_facet_indexes(std::map<uint32_t, std::vector<uint32_t>>&
auto ids = int64map_it->second;
ids_t::uncompress(ids, id_list);
- //emplacing seq_id=>count_index
+ // emplacing seq_id => facet_id
for(const auto& id : id_list) {
seqid_countIndexes[id].emplace_back(int64map_it->first);
}