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
Include/Exclude reference fields.
Harpreet Sangar0334f5e53461
diff --git a/include/collection.h b/include/collection.hindex dc6744ab..417b6e89 100644--- a/include/collection.h+++ b/include/collection.h@@ -373,7 +373,7 @@ public:static Option<bool> prune_doc(nlohmann::json& doc, const tsl::htrie_set<char>& include_names,const tsl::htrie_set<char>& exclude_names, const std::string& parent_name = "", size_t depth = 0,- const reference_filter_result_t* reference_filter_result = nullptr);+ const std::map<std::string, reference_filter_result_t>& reference_filter_results = {});const Index* _get_index() const;diff --git a/include/filter_result_iterator.h b/include/filter_result_iterator.hindex 55f6db94..b705b122 100644--- a/include/filter_result_iterator.h+++ b/include/filter_result_iterator.h@@ -2,6 +2,7 @@#include <string>#include <map>+#include <utility>#include <vector>#include "option.h"#include "posting_list.h"@@ -29,6 +30,17 @@ struct reference_filter_result_t {}};+struct single_filter_result_t {+ uint32_t seq_id = 0;+ // Collection name -> Reference filter result+ std::map<std::string, reference_filter_result_t> reference_filter_results = {};++ single_filter_result_t() = default;++ single_filter_result_t(uint32_t seq_id, std::map<std::string, reference_filter_result_t> reference_filter_results) :+ seq_id(seq_id), reference_filter_results(std::move(reference_filter_results)) {}+};+struct filter_result_t {uint32_t count = 0;uint32_t* docs = nullptr;@@ -39,6 +51,24 @@ struct filter_result_t {filter_result_t(uint32_t count, uint32_t* docs) : count(count), docs(docs) {}+ filter_result_t(const filter_result_t& obj) {+ if (&obj == this)+ return;++ count = obj.count;+ docs = new uint32_t[count];+ memcpy(docs, obj.docs, count * sizeof(uint32_t));++ // Copy every collection's references.+ for (const auto &item: obj.reference_filter_results) {+ auto& ref_coll_name = item.first;+ reference_filter_results[ref_coll_name] = new reference_filter_result_t[count];+ for (uint32_t i = 0; i < count; i++) {+ reference_filter_results[ref_coll_name][i] = item.second[i];+ }+ }+ }+filter_result_t& operator=(const filter_result_t& obj) noexcept {if (&obj == this)return *this;@@ -131,6 +161,9 @@ private:explicit filter_result_iterator_t(uint32_t approx_filter_ids_length);+ /// Collects n doc ids while advancing the iterator. The iterator may become invalid during this operation.+ void get_n_ids(const uint32_t& n, filter_result_t& result);+public:uint32_t seq_id = 0;/// Collection name -> references@@ -174,15 +207,12 @@ public:/// operation.void next();- /// Collects n doc ids while advancing the iterator. The iterator may become invalid during this operation.- void get_n_ids(const uint32_t& n, std::vector<uint32_t>& results);-/// Collects n doc ids while advancing the iterator. The ids present in excluded_result_ids are ignored. The/// iterator may become invalid during this operation.- void get_n_ids(const uint32_t &n,+ void get_n_ids(const uint32_t& n,uint32_t& excluded_result_index,uint32_t const* const excluded_result_ids, const size_t& excluded_result_ids_size,- std::vector<uint32_t> &results);+ filter_result_t& result);/// Advances the iterator until the doc value reaches or just overshoots id. The iterator may become invalid during/// this operation.diff --git a/include/or_iterator.h b/include/or_iterator.hindex b10ceccf..8f1fc56b 100644--- a/include/or_iterator.h+++ b/include/or_iterator.h@@ -1,6 +1,7 @@#pragma once#include <vector>+#include <filter_result_iterator.h>#include "posting_list.h"/*@@ -48,6 +49,9 @@ public:static bool take_id(result_iter_state_t& istate, uint32_t id, bool& is_excluded);+ static bool take_id(result_iter_state_t& istate, uint32_t id, bool& is_excluded,+ single_filter_result_t& filter_result);+template<class T>static bool intersect(std::vector<or_iterator_t>& its, result_iter_state_t& istate, T func);@@ -77,8 +81,9 @@ bool or_iterator_t::intersect(std::vector<or_iterator_t>& its, result_iter_state}auto id = its[0].id();- if(take_id(istate, id, is_excluded)) {- func(id, its);+ single_filter_result_t filter_result;+ if(take_id(istate, id, is_excluded, filter_result)) {+ func(filter_result, its);}if(istate.is_filter_provided() && !is_excluded) {@@ -109,8 +114,9 @@ bool or_iterator_t::intersect(std::vector<or_iterator_t>& its, result_iter_stateif(equals2(its)) {auto id = its[0].id();- if(take_id(istate, id, is_excluded)) {- func(id, its);+ single_filter_result_t filter_result;+ if(take_id(istate, id, is_excluded, filter_result)) {+ func(filter_result, its);}if(istate.is_filter_provided() != 0 && !is_excluded) {@@ -146,8 +152,9 @@ bool or_iterator_t::intersect(std::vector<or_iterator_t>& its, result_iter_stateif(equals(its)) {auto id = its[0].id();- if(take_id(istate, id, is_excluded)) {- func(id, its);+ single_filter_result_t filter_result;+ if(take_id(istate, id, is_excluded, filter_result)) {+ func(filter_result, its);}if(istate.is_filter_provided() && !is_excluded) {diff --git a/include/string_utils.h b/include/string_utils.hindex 288068a6..7a3c9fd3 100644--- a/include/string_utils.h+++ b/include/string_utils.h@@ -10,6 +10,7 @@#include <queue>#include "wyhash_v5.h"#include <unicode/normalizer2.h>+#include <set>#include "option.h"struct StringUtils {@@ -334,4 +335,7 @@ struct StringUtils {static Option<bool> tokenize_filter_query(const std::string& filter_query, std::queue<std::string>& tokens);static Option<bool> split_include_fields(const std::string& include_fields, std::vector<std::string>& tokens);++ static void get_reference_collection_names(const std::string& filter_query,+ std::set<std::string>& reference_collection_names);};diff --git a/include/topster.h b/include/topster.hindex 2ca2363a..d934a0a0 100644--- a/include/topster.h+++ b/include/topster.h@@ -15,20 +15,20 @@ struct KV {uint64_t key{};uint64_t distinct_key{};int64_t scores[3]{}; // match score + 2 custom attributes-+// only to be used in hybrid searchfloat vector_distance = 0.0f;int64_t text_match_score = 0;- reference_filter_result_t* reference_filter_result = nullptr;-// to be used only in final aggregationuint64_t* query_indices = nullptr;+ std::map<std::string, reference_filter_result_t> reference_filter_results;+KV(uint16_t queryIndex, uint64_t key, uint64_t distinct_key, uint8_t match_score_index, const int64_t *scores,- reference_filter_result_t* reference_filter_result = nullptr):+ std::map<std::string, reference_filter_result_t> reference_filter_results = {}):match_score_index(match_score_index), query_index(queryIndex), array_index(0), key(key),- distinct_key(distinct_key), reference_filter_result(reference_filter_result) {+ distinct_key(distinct_key), reference_filter_results(std::move(reference_filter_results)) {this->scores[0] = scores[0];this->scores[1] = scores[1];this->scores[2] = scores[2];@@ -51,6 +51,8 @@ struct KV {vector_distance = kv.vector_distance;text_match_score = kv.text_match_score;++ reference_filter_results = std::move(kv.reference_filter_results);}KV& operator=(KV&& kv) noexcept {@@ -71,6 +73,8 @@ struct KV {vector_distance = kv.vector_distance;text_match_score = kv.text_match_score;++ reference_filter_results = std::move(kv.reference_filter_results);}return *this;@@ -94,6 +98,12 @@ struct KV {vector_distance = kv.vector_distance;text_match_score = kv.text_match_score;++ reference_filter_results.clear();++ for (const auto& item: kv.reference_filter_results) {+ reference_filter_results[item.first] = item.second;+ }}return *this;diff --git a/src/collection.cpp b/src/collection.cppindex 7e925865..e9e96d93 100644--- a/src/collection.cpp+++ b/src/collection.cpp@@ -1926,7 +1926,7 @@ Option<nlohmann::json> Collection::search(std::string raw_query,exclude_fields_full,"",0,- field_order_kv->reference_filter_result);+ field_order_kv->reference_filter_results);if (!prune_op.ok()) {return Option<nlohmann::json>(prune_op.code(), prune_op.error());}@@ -3956,7 +3956,7 @@ Option<bool> Collection::prune_doc(nlohmann::json& doc,const tsl::htrie_set<char>& include_names,const tsl::htrie_set<char>& exclude_names,const std::string& parent_name, size_t depth,- const reference_filter_result_t* reference_filter_result) {+ const std::map<std::string, reference_filter_result_t>& reference_filter_results) {// doc can only be an objectauto it = doc.begin();while(it != doc.end()) {@@ -4032,12 +4032,12 @@ Option<bool> Collection::prune_doc(nlohmann::json& doc,it++;}- if (reference_filter_result == nullptr) {+ if (reference_filter_results.empty()) {return Option<bool>(true);}- auto reference_it = include_names.equal_prefix_range("$");- for (auto reference = reference_it.first; reference != reference_it.second; reference++) {+ auto include_reference_it = include_names.equal_prefix_range("$");+ for (auto reference = include_reference_it.first; reference != include_reference_it.second; reference++) {auto ref = reference.key();size_t parenthesis_index = ref.find('(');@@ -4045,45 +4045,80 @@ Option<bool> Collection::prune_doc(nlohmann::json& doc,auto reference_fields = ref.substr(parenthesis_index + 1, ref.size() - parenthesis_index - 2);auto& cm = CollectionManager::get_instance();- auto collection = cm.get_collection(ref_collection_name);- if (collection == nullptr) {- return Option<bool>(400, "Referenced collection `" + ref_collection_name + "` not found.");+ auto ref_collection = cm.get_collection(ref_collection_name);+ if (ref_collection == nullptr) {+ return Option<bool>(400, "Referenced collection `" + ref_collection_name + "` in include_fields not found.");+ }++ std::vector<std::string> ref_include_fields_vec, ref_exclude_fields_vec;+ StringUtils::split(reference_fields, ref_include_fields_vec, ",");+ auto exclude_reference_it = exclude_names.equal_prefix_range("$" + ref_collection_name);+ if (exclude_reference_it.first != exclude_reference_it.second) {+ ref = exclude_reference_it.first.key();+ parenthesis_index = ref.find('(');+ reference_fields = ref.substr(parenthesis_index + 1, ref.size() - parenthesis_index - 2);+ StringUtils::split(reference_fields, ref_exclude_fields_vec, ",");+ }++ spp::sparse_hash_set<std::string> ref_include_fields, ref_exclude_fields;+ ref_include_fields.insert(ref_include_fields_vec.begin(), ref_include_fields_vec.end());+ ref_exclude_fields.insert(ref_exclude_fields_vec.begin(), ref_exclude_fields_vec.end());++ tsl::htrie_set<char> ref_include_fields_full, ref_exclude_fields_full;+ auto include_exclude_op = ref_collection->populate_include_exclude_fields(ref_include_fields,+ ref_exclude_fields,+ ref_include_fields_full,+ ref_exclude_fields_full);+ auto error_prefix = "Referenced collection `" + ref_collection_name + "`: ";+ if (!include_exclude_op.ok()) {+ return Option<bool>(include_exclude_op.code(), error_prefix + include_exclude_op.error());}- std::vector<std::string> include_fields_vec;- StringUtils::split(reference_fields, include_fields_vec, ",");-- spp::sparse_hash_set<std::string> include_fields, exclude_fields;- include_fields.insert(include_fields_vec.begin(), include_fields_vec.end());-- tsl::htrie_set<char> include_fields_full, exclude_fields_full;- auto include_exclude_op = collection->populate_include_exclude_fields(include_fields, exclude_fields,- include_fields_full, exclude_fields_full);- if (!include_exclude_op.ok()) {- return include_exclude_op;+ if (reference_filter_results.count(ref_collection_name) == 0 ||+ reference_filter_results.at(ref_collection_name).count == 0) {+ // doc has no references.+ continue;}- std::vector<nlohmann::json> reference_docs;- reference_docs.reserve(reference_filter_result->count);- for (size_t i = 0; i < reference_filter_result->count; i++) {- auto doc_seq_id = reference_filter_result->docs[i];+ auto const& reference_filter_result = reference_filter_results.at(ref_collection_name);+ // One-to-one relation.+ if (reference_filter_result.count == 1) {+ auto ref_doc_seq_id = reference_filter_result.docs[0];nlohmann::json ref_doc;- auto get_doc_op = collection->get_document_from_store(doc_seq_id, ref_doc);+ auto get_doc_op = ref_collection->get_document_from_store(ref_doc_seq_id, ref_doc);if (!get_doc_op.ok()) {- return get_doc_op;+ return Option<bool>(get_doc_op.code(), error_prefix + get_doc_op.error());}- auto prune_op = prune_doc(ref_doc, include_fields_full, exclude_fields_full);+ auto prune_op = prune_doc(ref_doc, ref_include_fields_full, ref_exclude_fields_full);if (!prune_op.ok()) {- return prune_op;+ return Option<bool>(prune_op.code(), error_prefix + prune_op.error());}- reference_docs.push_back(ref_doc);+ doc.update(ref_doc);+ continue;}- for (const auto &ref_doc: reference_docs) {- doc.update(ref_doc);+ // One-to-many relation.+ for (uint32_t i = 0; i < reference_filter_result.count; i++) {+ auto ref_doc_seq_id = reference_filter_result.docs[i];++ nlohmann::json ref_doc;+ auto get_doc_op = ref_collection->get_document_from_store(ref_doc_seq_id, ref_doc);+ if (!get_doc_op.ok()) {+ return Option<bool>(get_doc_op.code(), error_prefix + get_doc_op.error());+ }++ auto prune_op = prune_doc(ref_doc, ref_include_fields_full, ref_exclude_fields_full);+ if (!prune_op.ok()) {+ return Option<bool>(prune_op.code(), error_prefix + prune_op.error());+ }++ for (auto ref_doc_it = ref_doc.begin(); ref_doc_it != ref_doc.end(); ref_doc_it++) {+ // Add the values of ref_doc as JSON array into doc.+ doc[ref_doc_it.key()] += ref_doc_it.value();+ }}}diff --git a/src/collection_manager.cpp b/src/collection_manager.cppindex 11f59528..7b48f7d3 100644--- a/src/collection_manager.cpp+++ b/src/collection_manager.cpp@@ -654,6 +654,50 @@ Option<bool> add_unsigned_int_list_param(const std::string& param_name, const streturn Option<bool>(true);}+void initialize_include_fields_vec(const std::string& filter_query, std::vector<std::string>& include_fields_vec) {+ if (filter_query.empty()) {+ return;+ }++ std::set<std::string> reference_collection_names;+ StringUtils::get_reference_collection_names(filter_query, reference_collection_names);+ if (reference_collection_names.empty()) {+ return;+ }++ bool non_reference_include_found = false;+ for (auto include_field: include_fields_vec) {+ if (include_field[0] != '$') {+ non_reference_include_found = true;+ continue;+ }++ auto open_paren_pos = include_field.find('(');+ if (open_paren_pos == std::string::npos) {+ continue;+ }++ auto reference_collection_name = include_field.substr(1, open_paren_pos - 1);+ StringUtils::trim(reference_collection_name);+ if (reference_collection_name.empty()) {+ continue;+ }++ // Referenced collection in filter_query is already mentioned in include_fields.+ reference_collection_names.erase(reference_collection_name);+ }++ // Get all the fields of the referenced collection in the filter but not mentioned in include_fields.+ for (const auto &reference_collection_name: reference_collection_names) {+ include_fields_vec.emplace_back("$" + reference_collection_name + "(*)");+ }++ // Since no field of the collection is mentioned in include_fields, get all the fields.+ if (!include_fields_vec.empty() && !non_reference_include_found) {+ include_fields_vec.emplace_back("*");+ }+}+Option<bool> CollectionManager::do_search(std::map<std::string, std::string>& req_params,nlohmann::json& embedded_params,std::string& results_json_str,@@ -792,7 +836,7 @@ Option<bool> CollectionManager::do_search(std::map<std::string, std::string>& resize_t typo_tokens_threshold = Index::TYPO_TOKENS_THRESHOLD;std::vector<std::string> search_fields;- std::string simple_filter_query;+ std::string filter_query;std::vector<std::string> facet_fields;std::vector<sort_by> sort_fields;size_t per_page = 10;@@ -872,7 +916,7 @@ Option<bool> CollectionManager::do_search(std::map<std::string, std::string>& re};std::unordered_map<std::string, std::string*> str_values = {- {FILTER, &simple_filter_query},+ {FILTER, &filter_query},{VECTOR_QUERY, &vector_query},{FACET_QUERY, &simple_facet_query},{HIGHLIGHT_FIELDS, &highlight_fields},@@ -997,6 +1041,8 @@ Option<bool> CollectionManager::do_search(std::map<std::string, std::string>& reper_page = 0;}+ initialize_include_fields_vec(filter_query, include_fields_vec);+include_fields.insert(include_fields_vec.begin(), include_fields_vec.end());exclude_fields.insert(exclude_fields_vec.begin(), exclude_fields_vec.end());@@ -1039,7 +1085,7 @@ Option<bool> CollectionManager::do_search(std::map<std::string, std::string>& reIndex::NUM_CANDIDATES_DEFAULT_MIN);}- Option<nlohmann::json> result_op = collection->search(raw_query, search_fields, simple_filter_query, facet_fields,+ Option<nlohmann::json> result_op = collection->search(raw_query, search_fields, filter_query, facet_fields,sort_fields, num_typos,per_page,page,diff --git a/src/filter_result_iterator.cpp b/src/filter_result_iterator.cppindex 55890d26..dcc2dbdf 100644--- a/src/filter_result_iterator.cpp+++ b/src/filter_result_iterator.cpp@@ -1488,52 +1488,66 @@ filter_result_iterator_t &filter_result_iterator_t::operator=(filter_result_iterreturn *this;}-void filter_result_iterator_t::get_n_ids(const uint32_t& n, std::vector<uint32_t>& results) {- if (is_filter_result_initialized) {- for (uint32_t count = 0; count < n && result_index < filter_result.count; count++) {- results.push_back(filter_result.docs[result_index++]);- }-- is_valid = result_index < filter_result.count;+void filter_result_iterator_t::get_n_ids(const uint32_t& n, filter_result_t& result) {+ if (!is_filter_result_initialized) {return;}- for (uint32_t count = 0; count < n && is_valid; count++) {- results.push_back(seq_id);- next();+ auto result_length = result.count = std::min(n, filter_result.count - result_index);+ result.docs = new uint32_t[result_length];+ for (const auto &item: filter_result.reference_filter_results) {+ result.reference_filter_results[item.first] = new reference_filter_result_t[result_length];+ }++ for (uint32_t i = 0; i < result_length; i++, result_index++) {+ result.docs[i] = filter_result.docs[result_index];+ for (const auto &item: filter_result.reference_filter_results) {+ result.reference_filter_results[item.first][i] = item.second[result_index];+ }}++ is_valid = result_index < filter_result.count;}void filter_result_iterator_t::get_n_ids(const uint32_t& n,- uint32_t & excluded_result_index,+ uint32_t& excluded_result_index,uint32_t const* const excluded_result_ids, const size_t& excluded_result_ids_size,- std::vector<uint32_t>& results) {+ filter_result_t& result) {if (excluded_result_ids == nullptr || excluded_result_ids_size == 0 ||excluded_result_index >= excluded_result_ids_size) {- return get_n_ids(n, results);+ return get_n_ids(n, result);}- if (is_filter_result_initialized) {- for (uint32_t count = 0; count < n && result_index < filter_result.count;) {- auto id = filter_result.docs[result_index++];+ // This method is only called in Index::search_wildcard after filter_result_iterator_t::compute_result.+ if (!is_filter_result_initialized) {+ return;+ }- if (!ArrayUtils::skip_index_to_id(excluded_result_index, excluded_result_ids, excluded_result_ids_size, id)) {- results.push_back(id);- count++;- }+ std::vector<uint32_t> match_indexes;+ for (uint32_t count = 0; count < n && result_index < filter_result.count; result_index++) {+ auto id = filter_result.docs[result_index];++ if (!ArrayUtils::skip_index_to_id(excluded_result_index, excluded_result_ids, excluded_result_ids_size, id)) {+ match_indexes.push_back(result_index);+ count++;}+ }- is_valid = result_index < filter_result.count;- return;+ result.count = match_indexes.size();+ result.docs = new uint32_t[match_indexes.size()];+ for (auto const& item: filter_result.reference_filter_results) {+ result.reference_filter_results[item.first] = new reference_filter_result_t[match_indexes.size()];}- for (uint32_t count = 0; count < n && is_valid;) {- if (!ArrayUtils::skip_index_to_id(excluded_result_index, excluded_result_ids, excluded_result_ids_size, seq_id)) {- results.push_back(seq_id);- count++;+ for (uint32_t i = 0; i < match_indexes.size(); i++) {+ auto const& match_index = match_indexes[i];+ result.docs[i] = filter_result.docs[match_index];+ for (auto const& item: filter_result.reference_filter_results) {+ result.reference_filter_results[item.first][i] = item.second[match_index];}- next();}++ is_valid = result_index < filter_result.count;}filter_result_iterator_t::filter_result_iterator_t(uint32_t approx_filter_ids_length) :diff --git a/src/index.cpp b/src/index.cppindex 71e734b5..58061789 100644--- a/src/index.cpp+++ b/src/index.cpp@@ -1808,7 +1808,7 @@ void Index::collate_included_ids(const std::vector<token_t>& q_included_tokens,scores[1] = int64_t(1);scores[2] = int64_t(1);- KV kv(searched_queries.size(), seq_id, distinct_id, 0, scores, nullptr);+ KV kv(searched_queries.size(), seq_id, distinct_id, 0, scores);curated_topster->add(&kv);}}@@ -2326,8 +2326,7 @@ Option<bool> Index::search(std::vector<query_tokens_t>& field_query_tokens, consint64_t match_score_index = -1;result_ids.push_back(seq_id);-- KV kv(searched_queries.size(), seq_id, distinct_id, match_score_index, scores, nullptr);+ KV kv(searched_queries.size(), seq_id, distinct_id, match_score_index, scores);int ret = topster->add(&kv);if(group_limit != 0 && ret < 2) {@@ -2356,12 +2355,13 @@ Option<bool> Index::search(std::vector<query_tokens_t>& field_query_tokens, consauto& field_vector_index = vector_index.at(vector_query.field_name);- std::vector<std::pair<float, size_t>> dist_labels;+ std::vector<std::pair<float, single_filter_result_t>> dist_results;uint32_t filter_id_count = 0;while (!no_filters_provided &&filter_id_count < vector_query.flat_search_cutoff && filter_result_iterator->is_valid) {- auto seq_id = filter_result_iterator->seq_id;+ auto& seq_id = filter_result_iterator->seq_id;+ auto filter_result = single_filter_result_t(seq_id, std::move(filter_result_iterator->reference));filter_result_iterator->next();std::vector<float> values;@@ -2383,23 +2383,30 @@ Option<bool> Index::search(std::vector<query_tokens_t>& field_query_tokens, cons&field_vector_index->num_dim);}- dist_labels.emplace_back(dist, seq_id);+ dist_results.emplace_back(dist, filter_result);filter_id_count++;}filter_result_iterator->reset();if(no_filters_provided ||(filter_id_count >= vector_query.flat_search_cutoff && filter_result_iterator->is_valid)) {- dist_labels.clear();+ dist_results.clear();+ // TODO: Need to pass reference infoVectorFilterFunctor filterFunctor(filter_result_iterator);+ std::vector<std::pair<float, size_t>> pairs;if(field_vector_index->distance_type == cosine) {std::vector<float> normalized_q(vector_query.values.size());hnsw_index_t::normalize_vector(vector_query.values, normalized_q);- dist_labels = field_vector_index->vecdex->searchKnnCloserFirst(normalized_q.data(), k, &filterFunctor);+ pairs = field_vector_index->vecdex->searchKnnCloserFirst(normalized_q.data(), k, &filterFunctor);} else {- dist_labels = field_vector_index->vecdex->searchKnnCloserFirst(vector_query.values.data(), k, &filterFunctor);+ pairs = field_vector_index->vecdex->searchKnnCloserFirst(vector_query.values.data(), k, &filterFunctor);+ }++ for (const auto &pair: pairs) {+ auto filter_result = single_filter_result_t(static_cast<uint32_t>(pair.second), {});+ dist_results.emplace_back(pair.first, filter_result);}}@@ -2407,8 +2414,8 @@ Option<bool> Index::search(std::vector<query_tokens_t>& field_query_tokens, consstd::vector<uint32_t> nearest_ids;- for (const auto& dist_label : dist_labels) {- uint32 seq_id = dist_label.second;+ for (const auto& dist_result : dist_results) {+ auto& seq_id = dist_result.second.seq_id;if(vector_query.query_doc_given && vector_query.seq_id == seq_id) {continue;@@ -2422,8 +2429,8 @@ Option<bool> Index::search(std::vector<query_tokens_t>& field_query_tokens, cons}}- auto vec_dist_score = (field_vector_index->distance_type == cosine) ? std::abs(dist_label.first) :- dist_label.first;+ auto vec_dist_score = (field_vector_index->distance_type == cosine) ? std::abs(dist_result.first) :+ dist_result.first;if(vec_dist_score > vector_query.distance_threshold) {continue;@@ -2432,9 +2439,11 @@ Option<bool> Index::search(std::vector<query_tokens_t>& field_query_tokens, consint64_t scores[3] = {0};int64_t match_score_index = -1;- compute_sort_scores(sort_fields_std, sort_order, field_values, geopoint_indices, seq_id, 0, 0, scores, match_score_index, vec_dist_score);+ compute_sort_scores(sort_fields_std, sort_order, field_values, geopoint_indices,+ seq_id, 0, 0, scores, match_score_index, vec_dist_score);- KV kv(searched_queries.size(), seq_id, distinct_id, match_score_index, scores, nullptr);+ KV kv(searched_queries.size(), seq_id, distinct_id, match_score_index, scores,+ dist_result.second.reference_filter_results);kv.vector_distance = vec_dist_score;int ret = topster->add(&kv);@@ -2661,6 +2670,8 @@ Option<bool> Index::search(std::vector<query_tokens_t>& field_query_tokens, consVectorFilterFunctor filterFunctor(filter_result_iterator);auto& field_vector_index = vector_index.at(vector_query.field_name);++ // TODO: Need to pass reference infostd::vector<std::pair<float, size_t>> dist_labels;// use k as 100 by default for ensuring results stability in paginationsize_t default_k = 100;@@ -3410,7 +3421,9 @@ void Index::find_across_fields(const token_t& previous_token,or_iterator_t token_fields(its);token_its.push_back(std::move(token_fields));- or_iterator_t::intersect(token_its, istate, [&](uint32_t seq_id, const std::vector<or_iterator_t>& its) {+ or_iterator_t::intersect(token_its, istate,+ [&](const single_filter_result_t& filter_result, const std::vector<or_iterator_t>& its) {+ auto& seq_id = filter_result.seq_id;prev_token_doc_ids.push_back(seq_id);});@@ -3562,7 +3575,9 @@ void Index::search_across_fields(const std::vector<token_t>& query_tokens,std::vector<uint32_t> result_ids;size_t filter_index = 0;- or_iterator_t::intersect(token_its, istate, [&](uint32_t seq_id, const std::vector<or_iterator_t>& its) {+ or_iterator_t::intersect(token_its, istate,+ [&](single_filter_result_t& filter_result, const std::vector<or_iterator_t>& its) {+ auto& seq_id = filter_result.seq_id;//LOG(INFO) << "seq_id: " << seq_id;// Convert [token -> fields] orientation to [field -> tokens] orientationstd::vector<std::vector<posting_list_t::iterator_t>> field_to_tokens(num_search_fields);@@ -3683,6 +3698,8 @@ void Index::search_across_fields(const std::vector<token_t>& query_tokens,<< ", aggregated_score: " << aggregated_score;*/KV kv(searched_queries.size(), seq_id, distinct_id, match_score_index, scores);+ kv.reference_filter_results = std::move(filter_result.reference_filter_results);+if(match_score_index != -1) {kv.scores[match_score_index] = aggregated_score;}@@ -4071,20 +4088,21 @@ void Index::do_phrase_search(const size_t num_search_fields, const std::vector<sfilter_result_iterator = new filter_result_iterator_t(phrase_result_ids, phrase_result_count);}- size_t filter_index = 0;-- if(is_wildcard_query) {- all_result_ids_len = filter_result_iterator->to_filter_id_array(all_result_ids);- filter_result_iterator->reset();- } else {+ if (!is_wildcard_query) {// this means that the there are non-phrase tokens in the query// so we cannot directly copy to the all_result_ids array- return ;+ return;}+ all_result_ids_len = filter_result_iterator->to_filter_id_array(all_result_ids);+ filter_result_iterator->reset();++ size_t filter_index = 0;// populate topsterfor(size_t i = 0; i < std::min<size_t>(10000, all_result_ids_len); i++) {- auto seq_id = all_result_ids[i];+ auto seq_id = filter_result_iterator->seq_id;+ auto reference_filter_results = std::move(filter_result_iterator->reference);+ filter_result_iterator->next();int64_t match_score = phrase_match_id_scores[seq_id];int64_t scores[3] = {0};@@ -4102,6 +4120,8 @@ void Index::do_phrase_search(const size_t num_search_fields, const std::vector<s}KV kv(searched_queries.size(), seq_id, distinct_id, match_score_index, scores);+ kv.reference_filter_results = std::move(reference_filter_results);+int ret = actual_topster->add(&kv);if(group_limit != 0 && ret < 2) {groups_processed[distinct_id]++;@@ -4111,6 +4131,7 @@ void Index::do_phrase_search(const size_t num_search_fields, const std::vector<sBREAK_CIRCUIT_BREAKER}}+ filter_result_iterator->reset();searched_queries.push_back({});}@@ -4187,6 +4208,7 @@ void Index::do_infix_search(const size_t num_search_fields, const std::vector<segfx::timsort(infix_ids.begin(), infix_ids.end());infix_ids.erase(std::unique( infix_ids.begin(), infix_ids.end() ), infix_ids.end());+ // TODO: Need to pass reference infouint32_t *raw_infix_ids = nullptr;size_t raw_infix_ids_length = 0;@@ -4234,9 +4256,10 @@ void Index::do_infix_search(const size_t num_search_fields, const std::vector<secontinue;}}- KV kv(searched_queries.size(), seq_id, distinct_id, match_score_index, scores);- int ret = actual_topster->add(&kv);-++ KV kv(searched_queries.size(), seq_id, distinct_id, match_score_index, scores);+ int ret = actual_topster->add(&kv);+if(group_limit != 0 && ret < 2) {groups_processed[distinct_id]++;}@@ -4533,11 +4556,9 @@ void Index::search_wildcard(filter_node_t const* const& filter_tree_root,uint32_t excluded_result_index = 0;for(size_t thread_id = 0; thread_id < num_threads && filter_result_iterator->is_valid; thread_id++) {- std::vector<uint32_t> batch_result_ids;- batch_result_ids.reserve(window_size);-- filter_result_iterator->get_n_ids(window_size, excluded_result_index, exclude_token_ids, exclude_token_ids_size,- batch_result_ids);+ filter_result_t batch_result;+ filter_result_iterator->get_n_ids(window_size, excluded_result_index, exclude_token_ids,+ exclude_token_ids_size, batch_result);num_queued++;@@ -4550,7 +4571,7 @@ void Index::search_wildcard(filter_node_t const* const& filter_tree_root,&group_limit, &group_by_fields, &topsters, &tgroups_processed, &excluded_group_ids,&sort_order, field_values, &geopoint_indices, &plists,check_for_circuit_break,- batch_result_ids,+ batch_result,&num_processed, &m_process, &cv_process]() {search_begin_us = parent_search_begin;@@ -4559,8 +4580,8 @@ void Index::search_wildcard(filter_node_t const* const& filter_tree_root,size_t filter_index = 0;- for(size_t i = 0; i < batch_result_ids.size(); i++) {- const uint32_t seq_id = batch_result_ids[i];+ for(size_t i = 0; i < batch_result.count; i++) {+ const uint32_t seq_id = batch_result.docs[i];int64_t match_score = 0;score_results2(sort_fields, (uint16_t) searched_queries.size(), 0, false, 0,@@ -4579,9 +4600,13 @@ void Index::search_wildcard(filter_node_t const* const& filter_tree_root,continue;}}- KV kv(searched_queries.size(), seq_id, distinct_id, match_score_index, scores);- int ret = topsters[thread_id]->add(&kv);-++ KV kv(searched_queries.size(), seq_id, distinct_id, match_score_index, scores);+ for (const auto& item: batch_result.reference_filter_results) {+ kv.reference_filter_results[item.first] = item.second[i];+ }++ int ret = topsters[thread_id]->add(&kv);if(group_limit != 0 && ret < 2) {tgroups_processed[thread_id][distinct_id]++;}diff --git a/src/or_iterator.cpp b/src/or_iterator.cppindex f2e72612..8ed0e5c2 100644--- a/src/or_iterator.cpp+++ b/src/or_iterator.cpp@@ -220,6 +220,61 @@ bool or_iterator_t::take_id(result_iter_state_t& istate, uint32_t id, bool& is_ereturn true;}+bool or_iterator_t::take_id(result_iter_state_t& istate, uint32_t id, bool& is_excluded,+ single_filter_result_t& filter_result) {+ is_excluded = false;++ // decide if this result id should be excluded+ if(istate.excluded_result_ids_size != 0) {+ if (std::binary_search(istate.excluded_result_ids,+ istate.excluded_result_ids + istate.excluded_result_ids_size, id)) {+ is_excluded = true;+ return false;+ }+ }++ // decide if this result be matched with filter results+ if(istate.filter_ids_length != 0) {+ if(istate.filter_ids_index >= istate.filter_ids_length) {+ return false;+ }++ // Returns iterator to the first element that is >= to value or last if no such element is found.+ size_t found_index = std::lower_bound(istate.filter_ids + istate.filter_ids_index,+ istate.filter_ids + istate.filter_ids_length, id) - istate.filter_ids;++ if(found_index == istate.filter_ids_length) {+ // all elements are lesser than lowest value (id), so we can stop looking+ istate.filter_ids_index = found_index + 1;+ return false;+ } else {+ if(istate.filter_ids[found_index] == id) {+ filter_result.seq_id = id;+ istate.filter_ids_index = found_index + 1;+ return true;+ }++ istate.filter_ids_index = found_index;+ }++ return false;+ }++ if (istate.fit != nullptr && istate.fit->approx_filter_ids_length > 0) {+ if (istate.fit->valid(id) == 1) {+ filter_result.seq_id = id;+ filter_result.reference_filter_results = std::move(istate.fit->reference);+ istate.fit->next();+ return true;+ }++ return false;+ }++ filter_result.seq_id = id;+ return true;+}+or_iterator_t::or_iterator_t(std::vector<posting_list_t::iterator_t>& its): its(std::move(its)) {curr_index = 0;diff --git a/src/string_utils.cpp b/src/string_utils.cppindex 6b320fc2..d5401ee8 100644--- a/src/string_utils.cpp+++ b/src/string_utils.cpp@@ -582,6 +582,54 @@ size_t StringUtils::split_facet(const std::string &s, std::vector<std::string> &return std::min(end_index, s.size());}+void StringUtils::get_reference_collection_names(const std::string& filter_query,+ std::set<std::string>& reference_collection_names) {+ auto size = filter_query.size();+ for (uint32_t i = 0; i < size;) {+ auto c = filter_query[i];+ if (c == ' ' || c == '(' || c == ')') {+ i++;+ } else if (c == '&' || c == '|') {+ i += 2;+ } else {+ // Reference filter would start with $ symbol.+ if (c == '$') {+ auto open_paren_pos = filter_query.find('(', ++i);+ if (open_paren_pos == std::string::npos) {+ return;+ }++ auto reference_collection_name = filter_query.substr(i, open_paren_pos - i);+ StringUtils::trim(reference_collection_name);+ if (!reference_collection_name.empty()) {+ reference_collection_names.insert(reference_collection_name);+ }++ i = open_paren_pos;+ int parenthesis_count = 1;+ while (++i < size && parenthesis_count > 0) {+ if (filter_query[i] == '(') {+ parenthesis_count++;+ } else if (filter_query[i] == ')') {+ parenthesis_count--;+ }+ }+ } else {+ while (filter_query[++i] != ':');+ bool in_backtick = false;+ do {+ c = filter_query[++i];+ if (c == '`') {+ in_backtick = !in_backtick;+ }+ } while (i < size && (in_backtick || (c != '(' && c != ')' &&+ !(c == '&' && filter_query[i + 1] == '&') &&+ !(c == '|' && filter_query[i + 1] == '|'))));+ }+ }+ }+}+/*size_t StringUtils::unicode_length(const std::string& bytes) {std::wstring_convert<std::codecvt_utf8<char32_t>, char32_t> utf8conv;return utf8conv.from_bytes(bytes).size();diff --git a/test/collection_all_fields_test.cpp b/test/collection_all_fields_test.cppindex 3a7b69fb..5dca533f 100644--- a/test/collection_all_fields_test.cpp+++ b/test/collection_all_fields_test.cpp@@ -39,1339 +39,1339 @@ protected:}};-TEST_F(CollectionAllFieldsTest, IndexDocsWithoutSchema) {- Collection *coll1;-- std::ifstream infile(std::string(ROOT_DIR)+"test/multi_field_documents.jsonl");- std::vector<field> fields = {};-- std::vector<sort_by> sort_fields = { sort_by("points", "DESC") };-- // try to create collection with random fallback field type- auto bad_coll_op = collectionManager.create_collection("coll_bad", 1, fields, "", 0, "blah");- ASSERT_FALSE(bad_coll_op.ok());- ASSERT_EQ("Field `.*` has an invalid type.", bad_coll_op.error());-- coll1 = collectionManager.get_collection("coll1").get();- if(coll1 == nullptr) {- auto coll_op = collectionManager.create_collection("coll1", 1, fields, "", 0, field_types::AUTO);- coll1 = coll_op.get();- }-- std::string json_line;-- while (std::getline(infile, json_line)) {- nlohmann::json document = nlohmann::json::parse(json_line);- Option<nlohmann::json> add_op = coll1->add(document.dump());- ASSERT_TRUE(add_op.ok());- }-- infile.close();-- query_fields = {"starring"};- std::vector<std::string> facets;-- // check default no specific dirty values option is sent for a collection that has schema detection enabled- std::string dirty_values;- ASSERT_EQ(DIRTY_VALUES::COERCE_OR_REJECT, coll1->parse_dirty_values_option(dirty_values));-- dirty_values = "coerce_or_reject";- ASSERT_EQ(DIRTY_VALUES::COERCE_OR_REJECT, coll1->parse_dirty_values_option(dirty_values));-- dirty_values = "COERCE_OR_DROP";- ASSERT_EQ(DIRTY_VALUES::COERCE_OR_DROP, coll1->parse_dirty_values_option(dirty_values));-- dirty_values = "reject";- ASSERT_EQ(DIRTY_VALUES::REJECT, coll1->parse_dirty_values_option(dirty_values));-- dirty_values = "DROP";- ASSERT_EQ(DIRTY_VALUES::DROP, coll1->parse_dirty_values_option(dirty_values));-- // same should succeed when verbatim filter is made- auto results = coll1->search("will", query_fields, "", facets, sort_fields, {0}, 10, 1, FREQUENCY, {false}).get();-- ASSERT_EQ(2, results["hits"].size());- ASSERT_EQ(2, results["found"].get<size_t>());-- ASSERT_STREQ("1", results["hits"][0]["document"]["id"].get<std::string>().c_str());- ASSERT_STREQ("0", results["hits"][1]["document"]["id"].get<std::string>().c_str());-- results = coll1->search("chris", {"cast"}, "", facets, sort_fields, {0}, 10, 1, FREQUENCY, {false}).get();-- ASSERT_EQ(3, results["hits"].size());- ASSERT_EQ(3, results["found"].get<size_t>());-- ASSERT_STREQ("6", results["hits"][0]["document"]["id"].get<std::string>().c_str());- ASSERT_STREQ("1", results["hits"][1]["document"]["id"].get<std::string>().c_str());- ASSERT_STREQ("7", results["hits"][2]["document"]["id"].get<std::string>().c_str());-- // reject field with a different type than already inferred type- // default for `index_all_fields` is `DIRTY_FIELD_COERCE_IGNORE`-- // unable to coerce- auto doc_json = R"({"cast":"William Barnes","points":63,"starring":"Will Ferrell",- "starring_facet":"Will Ferrell","title":"Anchorman 2: The Legend Continues"})";-- Option<nlohmann::json> add_op = coll1->add(doc_json);- ASSERT_FALSE(add_op.ok());- ASSERT_STREQ("Field `cast` must be an array.", add_op.error().c_str());-- // coerce integer to string- doc_json = R"({"cast": ["William Barnes"],"points": 63, "starring":"Will Ferrell",- "starring_facet":"Will Ferrell","title": 300})";-- add_op = coll1->add(doc_json);- ASSERT_TRUE(add_op.ok());-- results = coll1->search("300", {"title"}, "", {}, sort_fields, {0}, 10, 1, FREQUENCY, {false}).get();- ASSERT_EQ(1, results["hits"].size());- ASSERT_STREQ("300", results["hits"][0]["document"]["title"].get<std::string>().c_str());-- // with dirty values set to `COERCE_OR_DROP`- // `cast` field should not be indexed into store- doc_json = R"({"cast":"William Barnes","points":63,"starring":"Will Ferrell",- "starring_facet":"Will Ferrell","title":"With bad cast field."})";-- add_op = coll1->add(doc_json, CREATE, "", DIRTY_VALUES::COERCE_OR_DROP);- ASSERT_TRUE(add_op.ok());-- results = coll1->search("With bad cast field", {"title"}, "", {}, sort_fields, {0}, 10, 1, FREQUENCY, {false}).get();- ASSERT_EQ(1, results["hits"].size());- ASSERT_STREQ("With bad cast field.", results["hits"][0]["document"]["title"].get<std::string>().c_str());- ASSERT_EQ(0, results["hits"][0]["document"].count("cast"));-- // with dirty values set to `DROP`- // no coercion should happen, `title` field will just be dropped, but record indexed- doc_json = R"({"cast": ["Jeremy Livingston"],"points":63,"starring":"Will Ferrell",- "starring_facet":"Will Ferrell","title": 1200 })";-- add_op = coll1->add(doc_json, CREATE, "", DIRTY_VALUES::DROP);- ASSERT_TRUE(add_op.ok());-- results = coll1->search("1200", {"title"}, "", {}, sort_fields, {0}, 10, 1, FREQUENCY, {false}).get();- ASSERT_EQ(0, results["hits"].size());-- results = coll1->search("Jeremy Livingston", {"cast"}, "", {}, sort_fields, {0}, 10, 1, FREQUENCY, {false}).get();- ASSERT_EQ(1, results["hits"].size());- ASSERT_EQ(0, results["hits"][0]["document"].count("title"));-- // with dirty values set to `REJECT`- doc_json = R"({"cast": ["Jeremy Livingston"],"points":63,"starring":"Will Ferrell",- "starring_facet":"Will Ferrell","title": 1200 })";-- add_op = coll1->add(doc_json, CREATE, "", DIRTY_VALUES::REJECT);- ASSERT_FALSE(add_op.ok());- ASSERT_STREQ("Field `title` must be a string.", add_op.error().c_str());-- // try querying using an non-existing sort field- sort_fields = { sort_by("not-found", "DESC") };- auto res_op = coll1->search("*", {}, "", {}, sort_fields, {0}, 10, 1, FREQUENCY, {false});- ASSERT_FALSE(res_op.ok());- ASSERT_EQ("Could not find a field named `not-found` in the schema for sorting.", res_op.error());-- collectionManager.drop_collection("coll1");-}--TEST_F(CollectionAllFieldsTest, CoerceDynamicStringField) {- Collection *coll1;-- std::vector<field> fields = {field("title", field_types::STRING, true),- field(".*_name", "string", true, true),};-- coll1 = collectionManager.get_collection("coll1").get();- if (coll1 == nullptr) {- coll1 = collectionManager.create_collection("coll1", 1, fields, "", 0, "").get();- }-- std::string dirty_values;- ASSERT_EQ(DIRTY_VALUES::COERCE_OR_REJECT, coll1->parse_dirty_values_option(dirty_values));-- collectionManager.drop_collection("coll1");-}--TEST_F(CollectionAllFieldsTest, HandleArrayTypes) {- Collection *coll1;-- coll1 = collectionManager.get_collection("coll1").get();- if(coll1 == nullptr) {- coll1 = collectionManager.create_collection("coll1", 1, {}, "", 0, field_types::AUTO).get();- }-- nlohmann::json doc;- doc["title"] = "FIRST";- doc["int_values"] = {1, 2};-- Option<nlohmann::json> add_op = coll1->add(doc.dump(), CREATE, "0");- ASSERT_TRUE(add_op.ok());-- // coercion of string -> int-- doc["int_values"] = {"3"};-- add_op = coll1->add(doc.dump(), UPDATE, "0");- ASSERT_TRUE(add_op.ok());-- // bad array type value should be dropped when stored-- doc["title"] = "SECOND";- doc["int_values"] = {{3}};- add_op = coll1->add(doc.dump(), CREATE, "", DIRTY_VALUES:: DROP);- ASSERT_TRUE(add_op.ok());-- auto results = coll1->search("second", {"title"}, "", {}, sort_fields, {0}, 10, 1, FREQUENCY, {false}).get();- ASSERT_EQ(1, results["hits"].size());-- // check that the "bad" value does not exists in the stored document- ASSERT_EQ(1, results["hits"][0]["document"].count("int_values"));- ASSERT_EQ(0, results["hits"][0]["document"]["int_values"].size());-- // bad array type should follow coercion rules- add_op = coll1->add(doc.dump(), CREATE, "", DIRTY_VALUES::REJECT);- ASSERT_FALSE(add_op.ok());- ASSERT_EQ("Field `int_values` must be an array of int64.", add_op.error());-- // non array field should be handled as per coercion rule- doc["title"] = "THIRD";- doc["int_values"] = 3;- add_op = coll1->add(doc.dump(), CREATE, "", DIRTY_VALUES::REJECT);- ASSERT_FALSE(add_op.ok());- ASSERT_EQ("Field `int_values` must be an array.", add_op.error());-- add_op = coll1->add(doc.dump(), CREATE, "", DIRTY_VALUES::COERCE_OR_DROP);- ASSERT_TRUE(add_op.ok());- results = coll1->search("third", {"title"}, "", {}, sort_fields, {0}, 10, 1, FREQUENCY, {false}).get();- ASSERT_EQ(1, results["hits"].size());- ASSERT_EQ(0, results["hits"][0]["document"].count("int_values"));-- collectionManager.drop_collection("coll1");-}--TEST_F(CollectionAllFieldsTest, NonOptionalFieldShouldNotBeDropped) {- Collection *coll1;-- std::vector<field> fields = {- field("points", field_types::INT32, false, false)- };-- coll1 = collectionManager.get_collection("coll1").get();- if (coll1 == nullptr) {- coll1 = collectionManager.create_collection("coll1", 1, fields, "", 0).get();- }-- nlohmann::json doc;- doc["title"] = "FIRST";- doc["points"] = {100};-- Option<nlohmann::json> add_op = coll1->add(doc.dump(), CREATE, "0", DIRTY_VALUES::DROP);- ASSERT_FALSE(add_op.ok());- ASSERT_EQ("Field `points` must be an int32.", add_op.error());-- add_op = coll1->add(doc.dump(), CREATE, "0", DIRTY_VALUES::COERCE_OR_DROP);- ASSERT_FALSE(add_op.ok());- ASSERT_EQ("Field `points` must be an int32.", add_op.error());-- collectionManager.drop_collection("coll1");-}--TEST_F(CollectionAllFieldsTest, ShouldBeAbleToUpdateSchemaDetectedDocs) {- Collection *coll1;-- std::vector<field> fields = {-- };-- coll1 = collectionManager.get_collection("coll1").get();- if (coll1 == nullptr) {- coll1 = collectionManager.create_collection("coll1", 4, fields, "", 0, field_types::AUTO).get();- }-- nlohmann::json doc;- doc["title"] = "FIRST";- doc["scores"] = {100, 200, 300};-- Option<nlohmann::json> add_op = coll1->add(doc.dump(), CREATE, "0", DIRTY_VALUES::REJECT);- ASSERT_TRUE(add_op.ok());-- // now update both values and reinsert- doc["title"] = "SECOND";- doc["scores"] = {100, 250, "300", 400};-- add_op = coll1->add(doc.dump(), UPDATE, "0", DIRTY_VALUES::COERCE_OR_DROP);- ASSERT_TRUE(add_op.ok());-- auto results = coll1->search("second", {"title"}, "", {}, {}, {0}, 10, 1, FREQUENCY, {false}).get();-- ASSERT_EQ(1, results["hits"].size());- ASSERT_EQ("SECOND", results["hits"][0]["document"]["title"].get<std::string>());- ASSERT_EQ(4, results["hits"][0]["document"]["scores"].size());-- ASSERT_EQ(100, results["hits"][0]["document"]["scores"][0].get<size_t>());- ASSERT_EQ(250, results["hits"][0]["document"]["scores"][1].get<size_t>());- ASSERT_EQ(300, results["hits"][0]["document"]["scores"][2].get<size_t>());- ASSERT_EQ(400, results["hits"][0]["document"]["scores"][3].get<size_t>());-- // insert multiple docs at the same time- const size_t NUM_DOCS = 20;- std::vector<std::string> json_lines;-- for(size_t i = 0; i < NUM_DOCS; i++) {- const std::string &i_str = std::to_string(i);- doc["title"] = std::string("upserted ") + std::to_string(StringUtils::hash_wy(i_str.c_str(), i_str.size()));- doc["scores"] = {i};- doc["max"] = i;- doc["id"] = std::to_string(i+10);-- json_lines.push_back(doc.dump());- }-- nlohmann::json insert_doc;- auto res = coll1->add_many(json_lines, insert_doc, UPSERT);- ASSERT_TRUE(res["success"].get<bool>());-- // now we will replace all `max` values with the same value and assert that- json_lines.clear();- insert_doc.clear();-- for(size_t i = 0; i < NUM_DOCS; i++) {- const std::string &i_str = std::to_string(i);- doc.clear();- doc["title"] = std::string("updated ") + std::to_string(StringUtils::hash_wy(i_str.c_str(), i_str.size()));- doc["scores"] = {1000, 2000};- doc["max"] = 2000;- doc["id"] = std::to_string(i+10);-- json_lines.push_back(doc.dump());- }-- res = coll1->add_many(json_lines, insert_doc, UPDATE);- ASSERT_TRUE(res["success"].get<bool>());-- results = coll1->search("updated", {"title"}, "", {}, {}, {0}, 50, 1, FREQUENCY, {false}).get();- ASSERT_EQ(20, results["hits"].size());-- for(auto& hit: results["hits"]) {- ASSERT_EQ(2000, hit["document"]["max"].get<int>());- ASSERT_EQ(2, hit["document"]["scores"].size());- ASSERT_EQ(1000, hit["document"]["scores"][0].get<int>());- ASSERT_EQ(2000, hit["document"]["scores"][1].get<int>());- }-- collectionManager.drop_collection("coll1");-}--TEST_F(CollectionAllFieldsTest, StringifyAllValues) {- Collection *coll1;-- coll1 = collectionManager.get_collection("coll1").get();- if (coll1 == nullptr) {- coll1 = collectionManager.create_collection("coll1", 1, {}, "", 0, "string*").get();- }-- nlohmann::json doc;- doc["title"] = "FIRST";- doc["int_values"] = {1, 2};-- Option<nlohmann::json> add_op = coll1->add(doc.dump(), CREATE, "0");- ASSERT_TRUE(add_op.ok());- auto added_doc = add_op.get();-- auto schema = coll1->get_fields();-- ASSERT_EQ("int_values", schema[0].name);- ASSERT_EQ(field_types::STRING_ARRAY, schema[0].type);-- ASSERT_EQ("title", schema[1].name);- ASSERT_EQ(field_types::STRING, schema[1].type);-- ASSERT_EQ("1", added_doc["int_values"][0].get<std::string>());- ASSERT_EQ("2", added_doc["int_values"][1].get<std::string>());-- auto results = coll1->search("first", {"title"}, "", {}, sort_fields, {0}, 10, 1, FREQUENCY, {false}).get();- ASSERT_EQ(1, results["hits"].size());- ASSERT_EQ("FIRST", results["hits"][0]["document"]["title"].get<std::string>());-- ASSERT_EQ(1, results["hits"][0]["document"].count("int_values"));- ASSERT_EQ(2, results["hits"][0]["document"]["int_values"].size());- ASSERT_EQ("1", results["hits"][0]["document"]["int_values"][0].get<std::string>());- ASSERT_EQ("2", results["hits"][0]["document"]["int_values"][1].get<std::string>());-- // try with DROP- doc["title"] = "SECOND";- add_op = coll1->add(doc.dump(), CREATE, "", DIRTY_VALUES::DROP);- ASSERT_TRUE(add_op.ok());-- results = coll1->search("second", {"title"}, "", {}, sort_fields, {0}, 10, 1, FREQUENCY, {false}).get();- ASSERT_EQ(1, results["hits"].size());- ASSERT_EQ("SECOND", results["hits"][0]["document"]["title"].get<std::string>());- ASSERT_EQ(1, results["hits"][0]["document"].count("int_values"));- ASSERT_EQ(0, results["hits"][0]["document"]["int_values"].size()); // since both array values are dropped-- // try with REJECT- doc["title"] = "THIRD";- add_op = coll1->add(doc.dump(), CREATE, "", DIRTY_VALUES::REJECT);- ASSERT_FALSE(add_op.ok());- ASSERT_EQ("Field `int_values` must be an array of string.", add_op.error());-- // singular field coercion- doc["int_values"] = {"100"};- doc["single_int"] = 100;- doc["title"] = "FOURTH";-- add_op = coll1->add(doc.dump(), CREATE, "", DIRTY_VALUES::REJECT);- ASSERT_FALSE(add_op.ok());- ASSERT_EQ("Field `single_int` must be a string.", add_op.error());-- // try with empty array- doc["title"] = "FIFTH";- doc["int_values"] = {"100"};- doc["int_values_2"] = nlohmann::json::array();- doc["single_int"] = "200";-- add_op = coll1->add(doc.dump(), CREATE, "", DIRTY_VALUES::REJECT);- ASSERT_TRUE(add_op.ok());-- collectionManager.drop_collection("coll1");-}--TEST_F(CollectionAllFieldsTest, IntegerAllValues) {- Collection *coll1;-- coll1 = collectionManager.get_collection("coll1").get();- if (coll1 == nullptr) {- coll1 = collectionManager.create_collection("coll1", 1, {}, "", 0, "int32").get();- }-- nlohmann::json doc;- doc["age"] = 100;- doc["year"] = 2000;-- Option<nlohmann::json> add_op = coll1->add(doc.dump(), CREATE, "0");- ASSERT_TRUE(add_op.ok());- auto added_doc = add_op.get();-- auto results = coll1->search("*", {}, "", {}, sort_fields, {0}, 10, 1, FREQUENCY, {false}).get();- ASSERT_EQ(1, results["hits"].size());-- // try with DROP- doc["age"] = "SECOND";- add_op = coll1->add(doc.dump(), CREATE, "", DIRTY_VALUES::DROP);- ASSERT_TRUE(add_op.ok());-- results = coll1->search("*", {}, "", {}, sort_fields, {0}, 10, 1, FREQUENCY, {false}).get();- ASSERT_EQ(2, results["hits"].size());-- // try with REJECT- doc["age"] = "THIRD";- add_op = coll1->add(doc.dump(), CREATE, "", DIRTY_VALUES::REJECT);- ASSERT_FALSE(add_op.ok());- ASSERT_EQ("Field `age` must be an int32.", add_op.error());-- // try with coerce_or_reject- add_op = coll1->add(doc.dump(), CREATE, "", DIRTY_VALUES::COERCE_OR_REJECT);- ASSERT_FALSE(add_op.ok());- ASSERT_EQ("Field `age` must be an int32.", add_op.error());-- // try with coerce_or_drop- doc["age"] = "FOURTH";- add_op = coll1->add(doc.dump(), CREATE, "66", DIRTY_VALUES::COERCE_OR_DROP);- ASSERT_TRUE(add_op.ok());-- collectionManager.drop_collection("coll1");-}--TEST_F(CollectionAllFieldsTest, SearchStringifiedField) {- Collection *coll1;-- std::vector<field> fields = {field("title", field_types::STRING, true),- field("department", "string*", true, true),- field(".*_name", "string*", true, true),};-- coll1 = collectionManager.get_collection("coll1").get();- if (coll1 == nullptr) {- const Option<Collection*> &coll_op = collectionManager.create_collection("coll1", 1, fields, "", 0, "");- ASSERT_TRUE(coll_op.ok());- coll1 = coll_op.get();- }-- nlohmann::json doc;- doc["title"] = "FIRST";- doc["department"] = "ENGINEERING";- doc["company_name"] = "Stark Inc.";-- Option<nlohmann::json> add_op = coll1->add(doc.dump(), CREATE, "0");- ASSERT_TRUE(add_op.ok());-- // department field's type must be "solidified" to an actual type-- auto schema = coll1->get_fields();- ASSERT_EQ("department", schema[4].name);- ASSERT_EQ(field_types::STRING, schema[4].type);-- auto results_op = coll1->search("stark", {"company_name"}, "", {}, sort_fields, {0}, 10, 1, FREQUENCY, {false});- ASSERT_TRUE(results_op.ok());- ASSERT_EQ(1, results_op.get()["hits"].size());-- results_op = coll1->search("engineering", {"department"}, "", {}, sort_fields, {0}, 10, 1, FREQUENCY, {false});- ASSERT_TRUE(results_op.ok());- ASSERT_EQ(1, results_op.get()["hits"].size());-- collectionManager.drop_collection("coll1");-}--TEST_F(CollectionAllFieldsTest, StringSingularAllValues) {- Collection *coll1;-- coll1 = collectionManager.get_collection("coll1").get();- if (coll1 == nullptr) {- coll1 = collectionManager.create_collection("coll1", 1, {}, "", 0, "string").get();- }-- nlohmann::json doc;- doc["title"] = "FIRST";- doc["int_values"] = {1, 2};-- Option<nlohmann::json> add_op = coll1->add(doc.dump(), CREATE, "0");- ASSERT_FALSE(add_op.ok());- ASSERT_EQ("Field `int_values` must be a string.", add_op.error());-- doc["int_values"] = 123;-- add_op = coll1->add(doc.dump(), CREATE, "0");- ASSERT_TRUE(add_op.ok());-- auto added_doc = add_op.get();-- ASSERT_EQ("FIRST", added_doc["title"].get<std::string>());- ASSERT_EQ("123", added_doc["int_values"].get<std::string>());-- auto results = coll1->search("first", {"title"}, "", {}, sort_fields, {0}, 10, 1, FREQUENCY, {false}).get();- ASSERT_EQ(1, results["hits"].size());- ASSERT_EQ("FIRST", results["hits"][0]["document"]["title"].get<std::string>());- ASSERT_EQ("123", results["hits"][0]["document"]["int_values"].get<std::string>());-- collectionManager.drop_collection("coll1");-}--TEST_F(CollectionAllFieldsTest, UpdateOfDocumentsInAutoMode) {- Collection *coll1;-- coll1 = collectionManager.get_collection("coll1").get();- if (coll1 == nullptr) {- coll1 = collectionManager.create_collection("coll1", 1, {}, "", 0, field_types::AUTO).get();- }-- nlohmann::json doc;- doc["title"] = "FIRST";- doc["single_float"] = 50.50;-- auto add_op = coll1->add(doc.dump(), CREATE, "0", DIRTY_VALUES::COERCE_OR_REJECT);- ASSERT_TRUE(add_op.ok());-- // try updating a value- nlohmann::json update_doc;- update_doc["single_float"] = "123";-- add_op = coll1->add(update_doc.dump(), UPDATE, "0", DIRTY_VALUES::COERCE_OR_REJECT);- ASSERT_TRUE(add_op.ok());-- collectionManager.drop_collection("coll1");-}--TEST_F(CollectionAllFieldsTest, NormalFieldWithAutoType) {- Collection *coll1;-- std::vector<field> fields = {- field("city", field_types::AUTO, true, true),- field("publication_year", field_types::AUTO, true, true),- };-- coll1 = collectionManager.get_collection("coll1").get();- if (coll1 == nullptr) {- auto coll_op = collectionManager.create_collection("coll1", 1, fields, "", 0, field_types::AUTO);- ASSERT_TRUE(coll_op.ok());- coll1 = coll_op.get();- }-- nlohmann::json doc;- doc["title"] = "FIRST";- doc["city"] = "Austin";- doc["publication_year"] = 2010;-- auto add_op = coll1->add(doc.dump(), CREATE, "0", DIRTY_VALUES::COERCE_OR_REJECT);- ASSERT_TRUE(add_op.ok());-- auto res_op = coll1->search("austin", {"city"}, "publication_year: 2010", {}, sort_fields, {0}, 10, 1, FREQUENCY, {false});- ASSERT_TRUE(res_op.ok());- auto results = res_op.get();- ASSERT_EQ(1, results["hits"].size());-- auto schema = coll1->get_fields();- ASSERT_EQ("city", schema[2].name);- ASSERT_EQ(field_types::STRING, schema[2].type);-- ASSERT_EQ("publication_year", schema[3].name);- ASSERT_EQ(field_types::INT64, schema[3].type);-- collectionManager.drop_collection("coll1");-}--TEST_F(CollectionAllFieldsTest, JsonFieldsToFieldsConversion) {- nlohmann::json fields_json = nlohmann::json::array();- nlohmann::json all_field;- all_field[fields::name] = ".*";- all_field[fields::type] = "string*";- fields_json.emplace_back(all_field);-- std::string fallback_field_type;- std::vector<field> fields;-- auto parse_op = field::json_fields_to_fields(false, fields_json, fallback_field_type, fields);-- ASSERT_TRUE(parse_op.ok());- ASSERT_EQ(1, fields.size());- ASSERT_EQ("string*", fallback_field_type);- ASSERT_EQ(true, fields[0].optional);- ASSERT_EQ(false, fields[0].facet);- ASSERT_EQ(".*", fields[0].name);- ASSERT_EQ("string*", fields[0].type);-- // non-wildcard string* field should be treated as optional by default- fields_json = nlohmann::json::array();- nlohmann::json string_star_field;- string_star_field[fields::name] = "title";- string_star_field[fields::type] = "string*";- fields_json.emplace_back(string_star_field);- fields.clear();-- parse_op = field::json_fields_to_fields(false, fields_json, fallback_field_type, fields);- ASSERT_TRUE(parse_op.ok());- ASSERT_EQ(true, fields[0].optional);-- fields_json = nlohmann::json::array();- fields_json.emplace_back(all_field);-- // reject when you try to set optional to false or facet to true- fields_json[0][fields::optional] = false;- parse_op = field::json_fields_to_fields(false, fields_json, fallback_field_type, fields);-- ASSERT_FALSE(parse_op.ok());- ASSERT_EQ("Field `.*` must be an optional field.", parse_op.error());-- fields_json[0][fields::optional] = true;- fields_json[0][fields::facet] = true;- parse_op = field::json_fields_to_fields(false, fields_json, fallback_field_type, fields);-- ASSERT_FALSE(parse_op.ok());- ASSERT_EQ("Field `.*` cannot be a facet field.", parse_op.error());-- fields_json[0][fields::facet] = false;-- // can have only one ".*" field- fields_json.emplace_back(all_field);-- parse_op = field::json_fields_to_fields(false, fields_json, fallback_field_type, fields);-- ASSERT_FALSE(parse_op.ok());- ASSERT_EQ("There can be only one field named `.*`.", parse_op.error());-- // try with the `auto` type- fields_json.clear();- fields.clear();- all_field[fields::type] = "auto";- fields_json.emplace_back(all_field);-- parse_op = field::json_fields_to_fields(false, fields_json, fallback_field_type, fields);- ASSERT_TRUE(parse_op.ok());- ASSERT_EQ("auto", fields[0].type);-- // try with locale on a regular field- fields_json.clear();- fields.clear();- all_field[fields::type] = "string";- all_field[fields::name] = "title";- all_field[fields::locale] = "ja";- fields_json.emplace_back(all_field);-- parse_op = field::json_fields_to_fields(false, fields_json, fallback_field_type, fields);- ASSERT_TRUE(parse_op.ok());- ASSERT_EQ("ja", fields[0].locale);-- // try with locale on fallback field- fields_json.clear();- fields.clear();- all_field[fields::type] = "string";- all_field[fields::name] = ".*";- all_field[fields::locale] = "ko";- fields_json.emplace_back(all_field);-- parse_op = field::json_fields_to_fields(false, fields_json, fallback_field_type, fields);- ASSERT_TRUE(parse_op.ok());- ASSERT_EQ("ko", fields[0].locale);-}--TEST_F(CollectionAllFieldsTest, WildcardFacetFieldsOnAutoSchema) {- Collection *coll1;-- std::vector<field> fields = {field("title", field_types::STRING, true),- field(".*_name", field_types::STRING, true, true),};-- coll1 = collectionManager.get_collection("coll1").get();- if (coll1 == nullptr) {- coll1 = collectionManager.create_collection("coll1", 1, fields, "", 0, field_types::AUTO).get();- }-- nlohmann::json doc;- doc["title"] = "Org";- doc["org_name"] = "Amazon";-- auto add_op = coll1->add(doc.dump(), CREATE);- ASSERT_TRUE(add_op.ok());-- doc["title"] = "Org";- doc["org_name"] = "Walmart";-- add_op = coll1->add(doc.dump(), CREATE);- ASSERT_TRUE(add_op.ok());-- auto results = coll1->search("org", {"title"}, "", {"org_name"}, sort_fields, {0}, 10, 1, FREQUENCY, {false}).get();-- ASSERT_EQ(2, results["hits"].size());- ASSERT_EQ("Walmart", results["hits"][0]["document"]["org_name"].get<std::string>());- ASSERT_EQ("Amazon", results["hits"][1]["document"]["org_name"].get<std::string>());-- ASSERT_EQ("Amazon", results["facet_counts"][0]["counts"][0]["value"].get<std::string>());- ASSERT_EQ(1, (int) results["facet_counts"][0]["counts"][0]["count"]);-- ASSERT_EQ("Walmart", results["facet_counts"][0]["counts"][1]["value"].get<std::string>());- ASSERT_EQ(1, (int) results["facet_counts"][0]["counts"][1]["count"]);-- // add another type of .*_name field-- doc.clear();- doc["title"] = "Company";- doc["company_name"] = "Stark";-- add_op = coll1->add(doc.dump(), CREATE);- ASSERT_TRUE(add_op.ok());-- results = coll1->search("*", {"title"}, "", {"company_name", "org_name"}, sort_fields, {0}, 10, 1, FREQUENCY, {false}).get();- ASSERT_EQ(3, results["hits"].size());-- ASSERT_EQ("company_name", results["facet_counts"][0]["field_name"].get<std::string>());- ASSERT_EQ(1, results["facet_counts"][0]["counts"].size());- ASSERT_EQ("Stark", results["facet_counts"][0]["counts"][0]["value"].get<std::string>());- ASSERT_EQ(1, (int) results["facet_counts"][0]["counts"][0]["count"]);-- ASSERT_EQ("org_name", results["facet_counts"][1]["field_name"].get<std::string>());- ASSERT_EQ(2, results["facet_counts"][1]["counts"].size());- ASSERT_EQ("Amazon", results["facet_counts"][1]["counts"][0]["value"].get<std::string>());- ASSERT_EQ(1, (int) results["facet_counts"][1]["counts"][0]["count"]);-- ASSERT_EQ("Walmart", results["facet_counts"][1]["counts"][1]["value"].get<std::string>());- ASSERT_EQ(1, (int) results["facet_counts"][1]["counts"][1]["count"]);-- collectionManager.drop_collection("coll1");-}--TEST_F(CollectionAllFieldsTest, WildcardFacetFieldsWithAuoFacetFieldType) {- Collection *coll1;-- std::vector<field> fields = {field("title", field_types::STRING, true),- field(".*_name", field_types::AUTO, true, true),};-- coll1 = collectionManager.get_collection("coll1").get();- if (coll1 == nullptr) {- coll1 = collectionManager.create_collection("coll1", 1, fields, "", 0, field_types::AUTO).get();- }-- nlohmann::json doc;- doc["title"] = "Org";- doc["org_name"] = "Amazon";- doc["year_name"] = 1990;-- auto add_op = coll1->add(doc.dump(), CREATE);- ASSERT_TRUE(add_op.ok());-- doc["title"] = "Org";- doc["org_name"] = "Walmart";-- add_op = coll1->add(doc.dump(), CREATE);- ASSERT_TRUE(add_op.ok());-- auto results = coll1->search("org", {"title"}, "", {"org_name"}, sort_fields, {0}, 10, 1, FREQUENCY, {false}).get();-- ASSERT_EQ(2, results["hits"].size());- ASSERT_EQ("Walmart", results["hits"][0]["document"]["org_name"].get<std::string>());- ASSERT_EQ("Amazon", results["hits"][1]["document"]["org_name"].get<std::string>());-- ASSERT_EQ("Amazon", results["facet_counts"][0]["counts"][0]["value"].get<std::string>());- ASSERT_EQ(1, (int) results["facet_counts"][0]["counts"][0]["count"]);-- ASSERT_EQ("Walmart", results["facet_counts"][0]["counts"][1]["value"].get<std::string>());- ASSERT_EQ(1, (int) results["facet_counts"][0]["counts"][1]["count"]);-- collectionManager.drop_collection("coll1");-}--TEST_F(CollectionAllFieldsTest, WildcardFacetFieldsWithoutAutoSchema) {- Collection *coll1;-- std::vector<field> fields = {field("title", field_types::STRING, true),- field(".*_name", field_types::STRING, true, true),};-- coll1 = collectionManager.get_collection("coll1").get();- if (coll1 == nullptr) {- coll1 = collectionManager.create_collection("coll1", 1, fields, "", 0).get();- }-- nlohmann::json doc;- doc["title"] = "Org";- doc["org_name"] = "Amazon";-- auto add_op = coll1->add(doc.dump(), CREATE);- ASSERT_TRUE(add_op.ok());-- doc["title"] = "Org";- doc["org_name"] = "Walmart";-- add_op = coll1->add(doc.dump(), CREATE);- ASSERT_TRUE(add_op.ok());-- auto results = coll1->search("org", {"title"}, "", {"org_name"}, sort_fields, {0}, 10, 1, FREQUENCY, {false}).get();-- ASSERT_EQ(2, results["hits"].size());- ASSERT_EQ("Walmart", results["hits"][0]["document"]["org_name"].get<std::string>());- ASSERT_EQ("Amazon", results["hits"][1]["document"]["org_name"].get<std::string>());-- ASSERT_EQ("Amazon", results["facet_counts"][0]["counts"][0]["value"].get<std::string>());- ASSERT_EQ(1, (int) results["facet_counts"][0]["counts"][0]["count"]);-- ASSERT_EQ("Walmart", results["facet_counts"][0]["counts"][1]["value"].get<std::string>());- ASSERT_EQ(1, (int) results["facet_counts"][0]["counts"][1]["count"]);-- // add another type of .*_name field-- doc.clear();- doc["title"] = "Company";- doc["company_name"] = "Stark";-- add_op = coll1->add(doc.dump(), CREATE);- ASSERT_TRUE(add_op.ok());-- results = coll1->search("*", {"title"}, "", {"company_name", "org_name"}, sort_fields, {0}, 10, 1, FREQUENCY, {false}).get();- ASSERT_EQ(3, results["hits"].size());-- ASSERT_EQ("company_name", results["facet_counts"][0]["field_name"].get<std::string>());- ASSERT_EQ(1, results["facet_counts"][0]["counts"].size());- ASSERT_EQ("Stark", results["facet_counts"][0]["counts"][0]["value"].get<std::string>());- ASSERT_EQ(1, (int) results["facet_counts"][0]["counts"][0]["count"]);-- ASSERT_EQ("org_name", results["facet_counts"][1]["field_name"].get<std::string>());- ASSERT_EQ(2, results["facet_counts"][1]["counts"].size());- ASSERT_EQ("Amazon", results["facet_counts"][1]["counts"][0]["value"].get<std::string>());- ASSERT_EQ(1, (int) results["facet_counts"][1]["counts"][0]["count"]);-- ASSERT_EQ("Walmart", results["facet_counts"][1]["counts"][1]["value"].get<std::string>());- ASSERT_EQ(1, (int) results["facet_counts"][1]["counts"][1]["count"]);-- // Don't allow auto detection of schema when AUTO mode is not chosen- doc["description"] = "Stark company.";- add_op = coll1->add(doc.dump(), CREATE);- ASSERT_TRUE(add_op.ok());-- auto res_op = coll1->search("*", {"description"}, "", {}, sort_fields, {0}, 10, 1, FREQUENCY, {false});- ASSERT_FALSE(res_op.ok());- ASSERT_EQ("Could not find a field named `description` in the schema.", res_op.error());-- collectionManager.drop_collection("coll1");-}--TEST_F(CollectionAllFieldsTest, RegexpExplicitFieldTypeCoercion) {- Collection *coll1;-- std::vector<field> fields = {field("title", field_types::STRING, true),- field("i.*", field_types::INT32, false, true),- field("s.*", field_types::STRING, false, true),- field("a.*", field_types::STRING_ARRAY, false, true),- field("nullsa.*", field_types::STRING_ARRAY, false, true),- field("num.*", "string*", false, true),};-- coll1 = collectionManager.get_collection("coll1").get();- if (coll1 == nullptr) {- coll1 = collectionManager.create_collection("coll1", 1, fields, "", 0).get();- }-- nlohmann::json doc;- doc["title"] = "Rand Building";- doc["i_age"] = "28";- doc["s_name"] = nullptr;- doc["a_name"] = {};- doc["nullsa"] = nullptr;- doc["num_employees"] = 28;-- // should coerce while retaining expected type-- auto add_op = coll1->add(doc.dump(), CREATE);- ASSERT_TRUE(add_op.ok());-- auto schema = coll1->get_fields();-- ASSERT_EQ("a_name", schema[6].name);- ASSERT_EQ(field_types::STRING_ARRAY, schema[6].type);-- ASSERT_EQ("i_age", schema[7].name);- ASSERT_EQ(field_types::INT32, schema[7].type);-- ASSERT_EQ("nullsa", schema[8].name);- ASSERT_EQ(field_types::STRING_ARRAY, schema[8].type);-- // num_employees field's type must be "solidified" to an actual type- ASSERT_EQ("num_employees", schema[9].name);- ASSERT_EQ(field_types::STRING, schema[9].type);-- ASSERT_EQ("s_name", schema[10].name);- ASSERT_EQ(field_types::STRING, schema[10].type);-- auto results = coll1->search("rand", {"title"}, "i_age: 28", {}, sort_fields, {0}, 10, 1, FREQUENCY, {false}).get();- ASSERT_EQ(1, results["hits"].size());-- collectionManager.drop_collection("coll1");-}--TEST_F(CollectionAllFieldsTest, DynamicFieldsMustOnlyBeOptional) {- Collection *coll1;-- std::vector<field> bad_fields = {field("title", field_types::STRING, true),- field(".*_name", field_types::STRING, true, false),};-- auto op = collectionManager.create_collection("coll1", 1, bad_fields, "", 0);- ASSERT_FALSE(op.ok());- ASSERT_EQ("Field `.*_name` must be an optional field.", op.error());-- // string* fields should only be optional- std::vector<field> bad_fields2 = {field("title", field_types::STRING, true),- field("name", "string*", true, false),};-- op = collectionManager.create_collection("coll1", 1, bad_fields2, "", 0);- ASSERT_FALSE(op.ok());- ASSERT_EQ("Field `name` must be an optional field.", op.error());-- std::vector<field> fields = {field("title", field_types::STRING, true),- field(".*_name", field_types::STRING, true, true),};-- coll1 = collectionManager.get_collection("coll1").get();- if (coll1 == nullptr) {- op = collectionManager.create_collection("coll1", 1, fields, "", 0);- ASSERT_TRUE(op.ok());- coll1 = op.get();- }-- ASSERT_TRUE(coll1->get_dynamic_fields()[".*_name"].optional);-- collectionManager.drop_collection("coll1");-}--TEST_F(CollectionAllFieldsTest, AutoAndStringStarFieldsShouldAcceptNullValues) {- Collection *coll1;-- std::vector<field> fields = {- field("foo", "string*", true, true),- field("buzz", "auto", true, true),- field("bar.*", "string*", true, true),- field("baz.*", "auto", true, true),- };-- coll1 = collectionManager.get_collection("coll1").get();- if (coll1 == nullptr) {- auto coll_op = collectionManager.create_collection("coll1", 1, fields, "", 0);- ASSERT_TRUE(coll_op.ok());- coll1 = coll_op.get();- }-- nlohmann::json doc;- doc["foo"] = nullptr;- doc["buzz"] = nullptr;- doc["bar_one"] = nullptr;- doc["baz_one"] = nullptr;-- // should allow indexing of null values since all are optional- auto add_op = coll1->add(doc.dump(), CREATE);- ASSERT_TRUE(add_op.ok());-- auto res = coll1->search("*", {}, "", {}, sort_fields, {0}, 10, 1, FREQUENCY, {false}).get();- ASSERT_EQ("0", res["hits"][0]["document"]["id"].get<std::string>());- ASSERT_EQ(1, res["hits"][0]["document"].size());-- auto schema = coll1->get_fields();- ASSERT_EQ(4, schema.size());-- doc["foo"] = {"hello", "world"};- doc["buzz"] = 123;- doc["bar_one"] = "hello";- doc["baz_one"] = true;-- add_op = coll1->add(doc.dump(), CREATE);- ASSERT_TRUE(add_op.ok());-- schema = coll1->get_fields();- ASSERT_EQ(8, schema.size());-- ASSERT_EQ("bar_one", schema[4].name);- ASSERT_EQ(field_types::STRING, schema[4].type);-- ASSERT_EQ("baz_one", schema[5].name);- ASSERT_EQ(field_types::BOOL, schema[5].type);-- ASSERT_EQ("buzz", schema[6].name);- ASSERT_EQ(field_types::INT64, schema[6].type);-- ASSERT_EQ("foo", schema[7].name);- ASSERT_EQ(field_types::STRING_ARRAY, schema[7].type);-- collectionManager.drop_collection("coll1");-}--TEST_F(CollectionAllFieldsTest, BothFallbackAndDynamicFields) {- Collection *coll1;-- std::vector<field> fields = {field("title", field_types::STRING, true),- field(".*_name", field_types::STRING, false, true),- field(".*_year", field_types::INT32, true, true),- field(".*", field_types::AUTO, false, true)};-- coll1 = collectionManager.get_collection("coll1").get();- if (coll1 == nullptr) {- auto op = collectionManager.create_collection("coll1", 1, fields, "", 0, field_types::AUTO);- ASSERT_TRUE(op.ok());- coll1 = op.get();- }-- ASSERT_EQ(4, coll1->get_fields().size());- ASSERT_EQ(2, coll1->get_dynamic_fields().size());-- ASSERT_TRUE(coll1->get_dynamic_fields().count(".*_name") != 0);- ASSERT_TRUE(coll1->get_dynamic_fields()[".*_name"].optional);- ASSERT_FALSE(coll1->get_dynamic_fields()[".*_name"].facet);-- ASSERT_TRUE(coll1->get_dynamic_fields().count(".*_year") != 0);- ASSERT_TRUE(coll1->get_dynamic_fields()[".*_year"].optional);- ASSERT_TRUE(coll1->get_dynamic_fields()[".*_year"].facet);-- nlohmann::json doc;- doc["title"] = "Amazon Inc.";- doc["org_name"] = "Amazon";- doc["org_year"] = 1994;- doc["rand_int"] = 42;- doc["rand_str"] = "fizzbuzz";-- auto add_op = coll1->add(doc.dump(), CREATE);- ASSERT_TRUE(add_op.ok());-- // org_year should be of type int32- auto schema = coll1->get_fields();- ASSERT_EQ("org_year", schema[5].name);- ASSERT_EQ(field_types::INT32, schema[5].type);-- auto res_op = coll1->search("Amazon", {"org_name"}, "", {"org_name"}, sort_fields, {0}, 10, 1, FREQUENCY, {false});- ASSERT_FALSE(res_op.ok());- ASSERT_EQ("Could not find a facet field named `org_name` in the schema.", res_op.error());-- auto results = coll1->search("Amazon", {"org_name"}, "", {"org_year"}, sort_fields, {0}, 10, 1, FREQUENCY, {false}).get();- ASSERT_EQ(1, results["hits"].size());-- res_op = coll1->search("fizzbuzz", {"rand_str"}, "", {"rand_str"}, sort_fields, {0}, 10, 1, FREQUENCY, {false});- ASSERT_EQ("Could not find a facet field named `rand_str` in the schema.", res_op.error());-- results = coll1->search("fizzbuzz", {"rand_str"}, "", {"org_year"}, sort_fields, {0}, 10, 1, FREQUENCY, {false}).get();- ASSERT_EQ(1, results["hits"].size());-- collectionManager.drop_collection("coll1");-}--TEST_F(CollectionAllFieldsTest, WildcardFieldAndDictionaryField) {- Collection *coll1;-- std::vector<field> fields = {field(".*", field_types::AUTO, false, true)};-- coll1 = collectionManager.get_collection("coll1").get();- if (coll1 == nullptr) {- auto op = collectionManager.create_collection("coll1", 1, fields, "", 0, field_types::AUTO, {}, {}, true);- ASSERT_TRUE(op.ok());- coll1 = op.get();- }-- nlohmann::json doc;- doc["year"] = 2000;- doc["kinds"] = nlohmann::json::object();- doc["kinds"]["CGXX"] = 13;- doc["kinds"]["ZBXX"] = 24;-- auto add_op = coll1->add(doc.dump(), CREATE);- ASSERT_TRUE(add_op.ok());-- auto results = coll1->search("*", {}, "year: 2000", {}, sort_fields, {0}, 10, 1, FREQUENCY, {false}).get();- ASSERT_EQ(1, results["hits"].size());-- auto schema = coll1->get_fields();- ASSERT_EQ(5, schema.size());- ASSERT_EQ(".*", schema[0].name);- ASSERT_EQ("kinds", schema[1].name);- ASSERT_EQ("year", schema[2].name);- ASSERT_EQ("kinds.ZBXX", schema[3].name);- ASSERT_EQ("kinds.CGXX", schema[4].name);-- // filter on object key- results = coll1->search("*", {}, "kinds.CGXX: 13", {}, sort_fields, {0}, 10, 1, FREQUENCY, {false}).get();- ASSERT_EQ(1, results["hits"].size());-- collectionManager.drop_collection("coll1");-}--TEST_F(CollectionAllFieldsTest, DynamicFieldAndDictionaryField) {- Collection *coll1;-- std::vector<field> fields = {field("k.*", field_types::STRING, false, true),- field(".*", field_types::AUTO, false, true)};-- coll1 = collectionManager.get_collection("coll1").get();- if (coll1 == nullptr) {- auto op = collectionManager.create_collection("coll1", 1, fields, "", 0, field_types::AUTO);- ASSERT_TRUE(op.ok());- coll1 = op.get();- }-- nlohmann::json doc;- doc["kinds"] = nlohmann::json::object();- doc["kinds"]["CGXX"] = 13;- doc["kinds"]["ZBXX"] = 24;-- auto add_op = coll1->add(doc.dump(), CREATE);- ASSERT_FALSE(add_op.ok());- ASSERT_EQ("Field `kinds` must be a string.", add_op.error());-- collectionManager.drop_collection("coll1");-}--TEST_F(CollectionAllFieldsTest, RegexpIntFieldWithFallbackStringType) {- Collection *coll1;-- std::vector<field> fields = {field("title", field_types::STRING, true),- field("n.*", field_types::INT32, false, true),- field("s.*", "string*", false, true),- field(".*", field_types::STRING, false, true)};-- coll1 = collectionManager.get_collection("coll1").get();- if (coll1 == nullptr) {- auto op = collectionManager.create_collection("coll1", 1, fields, "", 0, field_types::STRING);- ASSERT_TRUE(op.ok());- coll1 = op.get();- }-- nlohmann::json doc;- doc["title"] = "Amazon Inc.";- doc["n_age"] = 32;- doc["s_tags"] = {"shopping"};- doc["rand_str"] = "fizzbuzz";-- auto add_op = coll1->add(doc.dump(), CREATE);- ASSERT_TRUE(add_op.ok());-- // n_age should be of type int32- auto schema = coll1->get_fields();-- ASSERT_EQ("n_age", schema[4].name);- ASSERT_EQ(field_types::INT32, schema[4].type);-- ASSERT_EQ("rand_str", schema[5].name);- ASSERT_EQ(field_types::STRING, schema[5].type);-- ASSERT_EQ("s_tags", schema[6].name);- ASSERT_EQ(field_types::STRING_ARRAY, schema[6].type);-- collectionManager.drop_collection("coll1");-}--TEST_F(CollectionAllFieldsTest, ContainingWildcardOnlyField) {- Collection *coll1;-- std::vector<field> fields = {field("company_name", field_types::STRING, false),- field("num_employees", field_types::INT32, false),- field(".*", field_types::BOOL, true, true)};-- coll1 = collectionManager.get_collection("coll1").get();- if (coll1 == nullptr) {- auto op = collectionManager.create_collection("coll1", 1, fields, "", 0, field_types::BOOL);- ASSERT_TRUE(op.ok());- coll1 = op.get();- }-- nlohmann::json doc;- doc["company_name"] = "Amazon Inc.";- doc["num_employees"] = 2000;- doc["country"] = "USA";-- auto add_op = coll1->add(doc.dump(), CREATE);- ASSERT_FALSE(add_op.ok());- ASSERT_EQ("Field `country` must be a bool.", add_op.error());-- collectionManager.drop_collection("coll1");-}--TEST_F(CollectionAllFieldsTest, DoNotIndexFieldMarkedAsNonIndex) {- Collection *coll1;-- std::vector<field> fields = {field("company_name", field_types::STRING, false),- field("num_employees", field_types::INT32, false),- field("post", field_types::STRING, false, true, false),- field(".*_txt", field_types::STRING, false, true, false),- field(".*", field_types::AUTO, false, true)};-- coll1 = collectionManager.get_collection("coll1").get();- if (coll1 == nullptr) {- auto op = collectionManager.create_collection("coll1", 1, fields, "", 0, field_types::AUTO);- ASSERT_TRUE(op.ok());- coll1 = op.get();- }-- nlohmann::json doc;- doc["company_name"] = "Amazon Inc.";- doc["num_employees"] = 2000;- doc["post"] = "Some post.";- doc["description_txt"] = "Rome was not built in a day.";-- auto add_op = coll1->add(doc.dump(), CREATE);- ASSERT_TRUE(add_op.ok());-- ASSERT_EQ(0, coll1->_get_index()->_get_search_index().count("post"));-- auto res_op = coll1->search("Amazon", {"description_txt"}, "", {}, sort_fields, {0}, 10, 1, FREQUENCY, {false});- ASSERT_FALSE(res_op.ok());- ASSERT_EQ("Could not find a field named `description_txt` in the schema.", res_op.error());-- res_op = coll1->search("Amazon", {"post"}, "", {}, sort_fields, {0}, 10, 1, FREQUENCY, {false});- ASSERT_FALSE(res_op.ok());- ASSERT_EQ("Field `post` is marked as a non-indexed field in the schema.", res_op.error());-- // try updating a document with non-indexable field- doc["post"] = "Some post updated.";- auto update_op = coll1->add(doc.dump(), UPDATE, "0");- ASSERT_TRUE(add_op.ok());-- ASSERT_EQ(0, coll1->_get_index()->_get_search_index().count("post"));-- auto res = coll1->search("Amazon", {"company_name"}, "", {}, sort_fields, {0}, 10, 1, FREQUENCY, {false}).get();- ASSERT_EQ("Some post updated.", res["hits"][0]["document"]["post"].get<std::string>());-- // try to delete doc with non-indexable field- auto del_op = coll1->remove("0");- ASSERT_TRUE(del_op.ok());-- // facet search should also be disabled- auto fs_op = coll1->search("Amazon", {"company_name"}, "", {"description_txt"}, sort_fields, {0}, 10, 1, FREQUENCY, {false});- ASSERT_FALSE(fs_op.ok());- ASSERT_EQ("Could not find a facet field named `description_txt` in the schema.", fs_op.error());-- fields = {field("company_name", field_types::STRING, false),- field("num_employees", field_types::INT32, false),- field("post", field_types::STRING, false, false, false),- field(".*_txt", field_types::STRING, true, true, false),- field(".*", field_types::AUTO, false, true)};-- auto op = collectionManager.create_collection("coll2", 1, fields, "", 0, field_types::AUTO);- ASSERT_FALSE(op.ok());- ASSERT_EQ("Field `post` must be optional since it is marked as non-indexable.", op.error());-- fields = {field("company_name", field_types::STRING, false),- field("num_employees", field_types::INT32, false),- field("post", field_types::STRING, false, true, false),- field(".*_txt", field_types::STRING, true, false, false),- field(".*", field_types::AUTO, false, true)};-- op = collectionManager.create_collection("coll2", 1, fields, "", 0, field_types::AUTO);- ASSERT_FALSE(op.ok());- ASSERT_EQ("Field `.*_txt` must be an optional field.", op.error());-- // don't allow catch all field to be non-indexable-- fields = {field("company_name", field_types::STRING, false),- field("num_employees", field_types::INT32, false),- field(".*_txt", field_types::STRING, false, true, false),- field(".*", field_types::AUTO, false, true, false)};-- op = collectionManager.create_collection("coll2", 1, fields, "", 0, field_types::AUTO);- ASSERT_FALSE(op.ok());- ASSERT_EQ("Field `.*` cannot be marked as non-indexable.", op.error());-- // allow auto field to be non-indexable-- fields = {field("company_name", field_types::STRING, false),- field("num_employees", field_types::INT32, false),- field("noidx_.*", field_types::AUTO, false, true, false)};-- op = collectionManager.create_collection("coll3", 1, fields, "", 0, field_types::AUTO);- ASSERT_TRUE(op.ok());-- // don't allow facet to be true when index is false- fields = {field("company_name", field_types::STRING, false),- field("num_employees", field_types::INT32, false),- field("facet_noindex", field_types::STRING, true, true, false)};-- op = collectionManager.create_collection("coll4", 1, fields, "", 0, field_types::AUTO);- ASSERT_FALSE(op.ok());- ASSERT_EQ("Field `facet_noindex` cannot be a facet since it's marked as non-indexable.", op.error());-- collectionManager.drop_collection("coll1");- collectionManager.drop_collection("coll2");- collectionManager.drop_collection("coll3");- collectionManager.drop_collection("coll4");-}--TEST_F(CollectionAllFieldsTest, NullValueUpdate) {- Collection *coll1;-- std::vector<field> fields = {field("title", field_types::STRING, false, true),- field(".*_name", field_types::STRING, true, true),- field("unindexed", field_types::STRING, false, true, false),- field(".*", field_types::STRING, false, true)};-- coll1 = collectionManager.get_collection("coll1").get();- if (coll1 == nullptr) {- auto op = collectionManager.create_collection("coll1", 1, fields, "", 0, field_types::STRING);- ASSERT_TRUE(op.ok());- coll1 = op.get();- }-- nlohmann::json doc;- doc["id"] = "0";- doc["title"] = "Running Shoes";- doc["company_name"] = "Nike";- doc["country"] = "USA";- doc["unindexed"] = "Hello";-- auto add_op = coll1->add(doc.dump(), CREATE);- ASSERT_TRUE(add_op.ok());-- doc["title"] = nullptr;- doc["company_name"] = nullptr;- doc["country"] = nullptr;-- add_op = coll1->add(doc.dump(), UPDATE);- ASSERT_TRUE(add_op.ok());-- // try updating the doc with null value again- add_op = coll1->add(doc.dump(), UPDATE);- ASSERT_TRUE(add_op.ok());-- // ensure that the fields are removed from the document- auto results = coll1->search("*", {}, "", {}, {}, {0}, 10, 1, FREQUENCY, {false}).get();-- ASSERT_EQ(1, results["hits"].size());- ASSERT_EQ(2, results["hits"][0]["document"].size());- ASSERT_EQ("Hello", results["hits"][0]["document"]["unindexed"].get<std::string>());- ASSERT_EQ("0", results["hits"][0]["document"]["id"].get<std::string>());-- collectionManager.drop_collection("coll1");-}+//TEST_F(CollectionAllFieldsTest, IndexDocsWithoutSchema) {+// Collection *coll1;+//+// std::ifstream infile(std::string(ROOT_DIR)+"test/multi_field_documents.jsonl");+// std::vector<field> fields = {};+//+// std::vector<sort_by> sort_fields = { sort_by("points", "DESC") };+//+// // try to create collection with random fallback field type+// auto bad_coll_op = collectionManager.create_collection("coll_bad", 1, fields, "", 0, "blah");+// ASSERT_FALSE(bad_coll_op.ok());+// ASSERT_EQ("Field `.*` has an invalid type.", bad_coll_op.error());+//+// coll1 = collectionManager.get_collection("coll1").get();+// if(coll1 == nullptr) {+// auto coll_op = collectionManager.create_collection("coll1", 1, fields, "", 0, field_types::AUTO);+// coll1 = coll_op.get();+// }+//+// std::string json_line;+//+// while (std::getline(infile, json_line)) {+// nlohmann::json document = nlohmann::json::parse(json_line);+// Option<nlohmann::json> add_op = coll1->add(document.dump());+// ASSERT_TRUE(add_op.ok());+// }+//+// infile.close();+//+// query_fields = {"starring"};+// std::vector<std::string> facets;+//+// // check default no specific dirty values option is sent for a collection that has schema detection enabled+// std::string dirty_values;+// ASSERT_EQ(DIRTY_VALUES::COERCE_OR_REJECT, coll1->parse_dirty_values_option(dirty_values));+//+// dirty_values = "coerce_or_reject";+// ASSERT_EQ(DIRTY_VALUES::COERCE_OR_REJECT, coll1->parse_dirty_values_option(dirty_values));+//+// dirty_values = "COERCE_OR_DROP";+// ASSERT_EQ(DIRTY_VALUES::COERCE_OR_DROP, coll1->parse_dirty_values_option(dirty_values));+//+// dirty_values = "reject";+// ASSERT_EQ(DIRTY_VALUES::REJECT, coll1->parse_dirty_values_option(dirty_values));+//+// dirty_values = "DROP";+// ASSERT_EQ(DIRTY_VALUES::DROP, coll1->parse_dirty_values_option(dirty_values));+//+// // same should succeed when verbatim filter is made+// auto results = coll1->search("will", query_fields, "", facets, sort_fields, {0}, 10, 1, FREQUENCY, {false}).get();+//+// ASSERT_EQ(2, results["hits"].size());+// ASSERT_EQ(2, results["found"].get<size_t>());+//+// ASSERT_STREQ("1", results["hits"][0]["document"]["id"].get<std::string>().c_str());+// ASSERT_STREQ("0", results["hits"][1]["document"]["id"].get<std::string>().c_str());+//+// results = coll1->search("chris", {"cast"}, "", facets, sort_fields, {0}, 10, 1, FREQUENCY, {false}).get();+//+// ASSERT_EQ(3, results["hits"].size());+// ASSERT_EQ(3, results["found"].get<size_t>());+//+// ASSERT_STREQ("6", results["hits"][0]["document"]["id"].get<std::string>().c_str());+// ASSERT_STREQ("1", results["hits"][1]["document"]["id"].get<std::string>().c_str());+// ASSERT_STREQ("7", results["hits"][2]["document"]["id"].get<std::string>().c_str());+//+// // reject field with a different type than already inferred type+// // default for `index_all_fields` is `DIRTY_FIELD_COERCE_IGNORE`+//+// // unable to coerce+// auto doc_json = R"({"cast":"William Barnes","points":63,"starring":"Will Ferrell",+// "starring_facet":"Will Ferrell","title":"Anchorman 2: The Legend Continues"})";+//+// Option<nlohmann::json> add_op = coll1->add(doc_json);+// ASSERT_FALSE(add_op.ok());+// ASSERT_STREQ("Field `cast` must be an array.", add_op.error().c_str());+//+// // coerce integer to string+// doc_json = R"({"cast": ["William Barnes"],"points": 63, "starring":"Will Ferrell",+// "starring_facet":"Will Ferrell","title": 300})";+//+// add_op = coll1->add(doc_json);+// ASSERT_TRUE(add_op.ok());+//+// results = coll1->search("300", {"title"}, "", {}, sort_fields, {0}, 10, 1, FREQUENCY, {false}).get();+// ASSERT_EQ(1, results["hits"].size());+// ASSERT_STREQ("300", results["hits"][0]["document"]["title"].get<std::string>().c_str());+//+// // with dirty values set to `COERCE_OR_DROP`+// // `cast` field should not be indexed into store+// doc_json = R"({"cast":"William Barnes","points":63,"starring":"Will Ferrell",+// "starring_facet":"Will Ferrell","title":"With bad cast field."})";+//+// add_op = coll1->add(doc_json, CREATE, "", DIRTY_VALUES::COERCE_OR_DROP);+// ASSERT_TRUE(add_op.ok());+//+// results = coll1->search("With bad cast field", {"title"}, "", {}, sort_fields, {0}, 10, 1, FREQUENCY, {false}).get();+// ASSERT_EQ(1, results["hits"].size());+// ASSERT_STREQ("With bad cast field.", results["hits"][0]["document"]["title"].get<std::string>().c_str());+// ASSERT_EQ(0, results["hits"][0]["document"].count("cast"));+//+// // with dirty values set to `DROP`+// // no coercion should happen, `title` field will just be dropped, but record indexed+// doc_json = R"({"cast": ["Jeremy Livingston"],"points":63,"starring":"Will Ferrell",+// "starring_facet":"Will Ferrell","title": 1200 })";+//+// add_op = coll1->add(doc_json, CREATE, "", DIRTY_VALUES::DROP);+// ASSERT_TRUE(add_op.ok());+//+// results = coll1->search("1200", {"title"}, "", {}, sort_fields, {0}, 10, 1, FREQUENCY, {false}).get();+// ASSERT_EQ(0, results["hits"].size());+//+// results = coll1->search("Jeremy Livingston", {"cast"}, "", {}, sort_fields, {0}, 10, 1, FREQUENCY, {false}).get();+// ASSERT_EQ(1, results["hits"].size());+// ASSERT_EQ(0, results["hits"][0]["document"].count("title"));+//+// // with dirty values set to `REJECT`+// doc_json = R"({"cast": ["Jeremy Livingston"],"points":63,"starring":"Will Ferrell",+// "starring_facet":"Will Ferrell","title": 1200 })";+//+// add_op = coll1->add(doc_json, CREATE, "", DIRTY_VALUES::REJECT);+// ASSERT_FALSE(add_op.ok());+// ASSERT_STREQ("Field `title` must be a string.", add_op.error().c_str());+//+// // try querying using an non-existing sort field+// sort_fields = { sort_by("not-found", "DESC") };+// auto res_op = coll1->search("*", {}, "", {}, sort_fields, {0}, 10, 1, FREQUENCY, {false});+// ASSERT_FALSE(res_op.ok());+// ASSERT_EQ("Could not find a field named `not-found` in the schema for sorting.", res_op.error());+//+// collectionManager.drop_collection("coll1");+//}+//+//TEST_F(CollectionAllFieldsTest, CoerceDynamicStringField) {+// Collection *coll1;+//+// std::vector<field> fields = {field("title", field_types::STRING, true),+// field(".*_name", "string", true, true),};+//+// coll1 = collectionManager.get_collection("coll1").get();+// if (coll1 == nullptr) {+// coll1 = collectionManager.create_collection("coll1", 1, fields, "", 0, "").get();+// }+//+// std::string dirty_values;+// ASSERT_EQ(DIRTY_VALUES::COERCE_OR_REJECT, coll1->parse_dirty_values_option(dirty_values));+//+// collectionManager.drop_collection("coll1");+//}+//+//TEST_F(CollectionAllFieldsTest, HandleArrayTypes) {+// Collection *coll1;+//+// coll1 = collectionManager.get_collection("coll1").get();+// if(coll1 == nullptr) {+// coll1 = collectionManager.create_collection("coll1", 1, {}, "", 0, field_types::AUTO).get();+// }+//+// nlohmann::json doc;+// doc["title"] = "FIRST";+// doc["int_values"] = {1, 2};+//+// Option<nlohmann::json> add_op = coll1->add(doc.dump(), CREATE, "0");+// ASSERT_TRUE(add_op.ok());+//+// // coercion of string -> int+//+// doc["int_values"] = {"3"};+//+// add_op = coll1->add(doc.dump(), UPDATE, "0");+// ASSERT_TRUE(add_op.ok());+//+// // bad array type value should be dropped when stored+//+// doc["title"] = "SECOND";+// doc["int_values"] = {{3}};+// add_op = coll1->add(doc.dump(), CREATE, "", DIRTY_VALUES:: DROP);+// ASSERT_TRUE(add_op.ok());+//+// auto results = coll1->search("second", {"title"}, "", {}, sort_fields, {0}, 10, 1, FREQUENCY, {false}).get();+// ASSERT_EQ(1, results["hits"].size());+//+// // check that the "bad" value does not exists in the stored document+// ASSERT_EQ(1, results["hits"][0]["document"].count("int_values"));+// ASSERT_EQ(0, results["hits"][0]["document"]["int_values"].size());+//+// // bad array type should follow coercion rules+// add_op = coll1->add(doc.dump(), CREATE, "", DIRTY_VALUES::REJECT);+// ASSERT_FALSE(add_op.ok());+// ASSERT_EQ("Field `int_values` must be an array of int64.", add_op.error());+//+// // non array field should be handled as per coercion rule+// doc["title"] = "THIRD";+// doc["int_values"] = 3;+// add_op = coll1->add(doc.dump(), CREATE, "", DIRTY_VALUES::REJECT);+// ASSERT_FALSE(add_op.ok());+// ASSERT_EQ("Field `int_values` must be an array.", add_op.error());+//+// add_op = coll1->add(doc.dump(), CREATE, "", DIRTY_VALUES::COERCE_OR_DROP);+// ASSERT_TRUE(add_op.ok());+// results = coll1->search("third", {"title"}, "", {}, sort_fields, {0}, 10, 1, FREQUENCY, {false}).get();+// ASSERT_EQ(1, results["hits"].size());+// ASSERT_EQ(0, results["hits"][0]["document"].count("int_values"));+//+// collectionManager.drop_collection("coll1");+//}+//+//TEST_F(CollectionAllFieldsTest, NonOptionalFieldShouldNotBeDropped) {+// Collection *coll1;+//+// std::vector<field> fields = {+// field("points", field_types::INT32, false, false)+// };+//+// coll1 = collectionManager.get_collection("coll1").get();+// if (coll1 == nullptr) {+// coll1 = collectionManager.create_collection("coll1", 1, fields, "", 0).get();+// }+//+// nlohmann::json doc;+// doc["title"] = "FIRST";+// doc["points"] = {100};+//+// Option<nlohmann::json> add_op = coll1->add(doc.dump(), CREATE, "0", DIRTY_VALUES::DROP);+// ASSERT_FALSE(add_op.ok());+// ASSERT_EQ("Field `points` must be an int32.", add_op.error());+//+// add_op = coll1->add(doc.dump(), CREATE, "0", DIRTY_VALUES::COERCE_OR_DROP);+// ASSERT_FALSE(add_op.ok());+// ASSERT_EQ("Field `points` must be an int32.", add_op.error());+//+// collectionManager.drop_collection("coll1");+//}+//+//TEST_F(CollectionAllFieldsTest, ShouldBeAbleToUpdateSchemaDetectedDocs) {+// Collection *coll1;+//+// std::vector<field> fields = {+//+// };+//+// coll1 = collectionManager.get_collection("coll1").get();+// if (coll1 == nullptr) {+// coll1 = collectionManager.create_collection("coll1", 4, fields, "", 0, field_types::AUTO).get();+// }+//+// nlohmann::json doc;+// doc["title"] = "FIRST";+// doc["scores"] = {100, 200, 300};+//+// Option<nlohmann::json> add_op = coll1->add(doc.dump(), CREATE, "0", DIRTY_VALUES::REJECT);+// ASSERT_TRUE(add_op.ok());+//+// // now update both values and reinsert+// doc["title"] = "SECOND";+// doc["scores"] = {100, 250, "300", 400};+//+// add_op = coll1->add(doc.dump(), UPDATE, "0", DIRTY_VALUES::COERCE_OR_DROP);+// ASSERT_TRUE(add_op.ok());+//+// auto results = coll1->search("second", {"title"}, "", {}, {}, {0}, 10, 1, FREQUENCY, {false}).get();+//+// ASSERT_EQ(1, results["hits"].size());+// ASSERT_EQ("SECOND", results["hits"][0]["document"]["title"].get<std::string>());+// ASSERT_EQ(4, results["hits"][0]["document"]["scores"].size());+//+// ASSERT_EQ(100, results["hits"][0]["document"]["scores"][0].get<size_t>());+// ASSERT_EQ(250, results["hits"][0]["document"]["scores"][1].get<size_t>());+// ASSERT_EQ(300, results["hits"][0]["document"]["scores"][2].get<size_t>());+// ASSERT_EQ(400, results["hits"][0]["document"]["scores"][3].get<size_t>());+//+// // insert multiple docs at the same time+// const size_t NUM_DOCS = 20;+// std::vector<std::string> json_lines;+//+// for(size_t i = 0; i < NUM_DOCS; i++) {+// const std::string &i_str = std::to_string(i);+// doc["title"] = std::string("upserted ") + std::to_string(StringUtils::hash_wy(i_str.c_str(), i_str.size()));+// doc["scores"] = {i};+// doc["max"] = i;+// doc["id"] = std::to_string(i+10);+//+// json_lines.push_back(doc.dump());+// }+//+// nlohmann::json insert_doc;+// auto res = coll1->add_many(json_lines, insert_doc, UPSERT);+// ASSERT_TRUE(res["success"].get<bool>());+//+// // now we will replace all `max` values with the same value and assert that+// json_lines.clear();+// insert_doc.clear();+//+// for(size_t i = 0; i < NUM_DOCS; i++) {+// const std::string &i_str = std::to_string(i);+// doc.clear();+// doc["title"] = std::string("updated ") + std::to_string(StringUtils::hash_wy(i_str.c_str(), i_str.size()));+// doc["scores"] = {1000, 2000};+// doc["max"] = 2000;+// doc["id"] = std::to_string(i+10);+//+// json_lines.push_back(doc.dump());+// }+//+// res = coll1->add_many(json_lines, insert_doc, UPDATE);+// ASSERT_TRUE(res["success"].get<bool>());+//+// results = coll1->search("updated", {"title"}, "", {}, {}, {0}, 50, 1, FREQUENCY, {false}).get();+// ASSERT_EQ(20, results["hits"].size());+//+// for(auto& hit: results["hits"]) {+// ASSERT_EQ(2000, hit["document"]["max"].get<int>());+// ASSERT_EQ(2, hit["document"]["scores"].size());+// ASSERT_EQ(1000, hit["document"]["scores"][0].get<int>());+// ASSERT_EQ(2000, hit["document"]["scores"][1].get<int>());+// }+//+// collectionManager.drop_collection("coll1");+//}+//+//TEST_F(CollectionAllFieldsTest, StringifyAllValues) {+// Collection *coll1;+//+// coll1 = collectionManager.get_collection("coll1").get();+// if (coll1 == nullptr) {+// coll1 = collectionManager.create_collection("coll1", 1, {}, "", 0, "string*").get();+// }+//+// nlohmann::json doc;+// doc["title"] = "FIRST";+// doc["int_values"] = {1, 2};+//+// Option<nlohmann::json> add_op = coll1->add(doc.dump(), CREATE, "0");+// ASSERT_TRUE(add_op.ok());+// auto added_doc = add_op.get();+//+// auto schema = coll1->get_fields();+//+// ASSERT_EQ("int_values", schema[0].name);+// ASSERT_EQ(field_types::STRING_ARRAY, schema[0].type);+//+// ASSERT_EQ("title", schema[1].name);+// ASSERT_EQ(field_types::STRING, schema[1].type);+//+// ASSERT_EQ("1", added_doc["int_values"][0].get<std::string>());+// ASSERT_EQ("2", added_doc["int_values"][1].get<std::string>());+//+// auto results = coll1->search("first", {"title"}, "", {}, sort_fields, {0}, 10, 1, FREQUENCY, {false}).get();+// ASSERT_EQ(1, results["hits"].size());+// ASSERT_EQ("FIRST", results["hits"][0]["document"]["title"].get<std::string>());+//+// ASSERT_EQ(1, results["hits"][0]["document"].count("int_values"));+// ASSERT_EQ(2, results["hits"][0]["document"]["int_values"].size());+// ASSERT_EQ("1", results["hits"][0]["document"]["int_values"][0].get<std::string>());+// ASSERT_EQ("2", results["hits"][0]["document"]["int_values"][1].get<std::string>());+//+// // try with DROP+// doc["title"] = "SECOND";+// add_op = coll1->add(doc.dump(), CREATE, "", DIRTY_VALUES::DROP);+// ASSERT_TRUE(add_op.ok());+//+// results = coll1->search("second", {"title"}, "", {}, sort_fields, {0}, 10, 1, FREQUENCY, {false}).get();+// ASSERT_EQ(1, results["hits"].size());+// ASSERT_EQ("SECOND", results["hits"][0]["document"]["title"].get<std::string>());+// ASSERT_EQ(1, results["hits"][0]["document"].count("int_values"));+// ASSERT_EQ(0, results["hits"][0]["document"]["int_values"].size()); // since both array values are dropped+//+// // try with REJECT+// doc["title"] = "THIRD";+// add_op = coll1->add(doc.dump(), CREATE, "", DIRTY_VALUES::REJECT);+// ASSERT_FALSE(add_op.ok());+// ASSERT_EQ("Field `int_values` must be an array of string.", add_op.error());+//+// // singular field coercion+// doc["int_values"] = {"100"};+// doc["single_int"] = 100;+// doc["title"] = "FOURTH";+//+// add_op = coll1->add(doc.dump(), CREATE, "", DIRTY_VALUES::REJECT);+// ASSERT_FALSE(add_op.ok());+// ASSERT_EQ("Field `single_int` must be a string.", add_op.error());+//+// // try with empty array+// doc["title"] = "FIFTH";+// doc["int_values"] = {"100"};+// doc["int_values_2"] = nlohmann::json::array();+// doc["single_int"] = "200";+//+// add_op = coll1->add(doc.dump(), CREATE, "", DIRTY_VALUES::REJECT);+// ASSERT_TRUE(add_op.ok());+//+// collectionManager.drop_collection("coll1");+//}+//+//TEST_F(CollectionAllFieldsTest, IntegerAllValues) {+// Collection *coll1;+//+// coll1 = collectionManager.get_collection("coll1").get();+// if (coll1 == nullptr) {+// coll1 = collectionManager.create_collection("coll1", 1, {}, "", 0, "int32").get();+// }+//+// nlohmann::json doc;+// doc["age"] = 100;+// doc["year"] = 2000;+//+// Option<nlohmann::json> add_op = coll1->add(doc.dump(), CREATE, "0");+// ASSERT_TRUE(add_op.ok());+// auto added_doc = add_op.get();+//+// auto results = coll1->search("*", {}, "", {}, sort_fields, {0}, 10, 1, FREQUENCY, {false}).get();+// ASSERT_EQ(1, results["hits"].size());+//+// // try with DROP+// doc["age"] = "SECOND";+// add_op = coll1->add(doc.dump(), CREATE, "", DIRTY_VALUES::DROP);+// ASSERT_TRUE(add_op.ok());+//+// results = coll1->search("*", {}, "", {}, sort_fields, {0}, 10, 1, FREQUENCY, {false}).get();+// ASSERT_EQ(2, results["hits"].size());+//+// // try with REJECT+// doc["age"] = "THIRD";+// add_op = coll1->add(doc.dump(), CREATE, "", DIRTY_VALUES::REJECT);+// ASSERT_FALSE(add_op.ok());+// ASSERT_EQ("Field `age` must be an int32.", add_op.error());+//+// // try with coerce_or_reject+// add_op = coll1->add(doc.dump(), CREATE, "", DIRTY_VALUES::COERCE_OR_REJECT);+// ASSERT_FALSE(add_op.ok());+// ASSERT_EQ("Field `age` must be an int32.", add_op.error());+//+// // try with coerce_or_drop+// doc["age"] = "FOURTH";+// add_op = coll1->add(doc.dump(), CREATE, "66", DIRTY_VALUES::COERCE_OR_DROP);+// ASSERT_TRUE(add_op.ok());+//+// collectionManager.drop_collection("coll1");+//}+//+//TEST_F(CollectionAllFieldsTest, SearchStringifiedField) {+// Collection *coll1;+//+// std::vector<field> fields = {field("title", field_types::STRING, true),+// field("department", "string*", true, true),+// field(".*_name", "string*", true, true),};+//+// coll1 = collectionManager.get_collection("coll1").get();+// if (coll1 == nullptr) {+// const Option<Collection*> &coll_op = collectionManager.create_collection("coll1", 1, fields, "", 0, "");+// ASSERT_TRUE(coll_op.ok());+// coll1 = coll_op.get();+// }+//+// nlohmann::json doc;+// doc["title"] = "FIRST";+// doc["department"] = "ENGINEERING";+// doc["company_name"] = "Stark Inc.";+//+// Option<nlohmann::json> add_op = coll1->add(doc.dump(), CREATE, "0");+// ASSERT_TRUE(add_op.ok());+//+// // department field's type must be "solidified" to an actual type+//+// auto schema = coll1->get_fields();+// ASSERT_EQ("department", schema[4].name);+// ASSERT_EQ(field_types::STRING, schema[4].type);+//+// auto results_op = coll1->search("stark", {"company_name"}, "", {}, sort_fields, {0}, 10, 1, FREQUENCY, {false});+// ASSERT_TRUE(results_op.ok());+// ASSERT_EQ(1, results_op.get()["hits"].size());+//+// results_op = coll1->search("engineering", {"department"}, "", {}, sort_fields, {0}, 10, 1, FREQUENCY, {false});+// ASSERT_TRUE(results_op.ok());+// ASSERT_EQ(1, results_op.get()["hits"].size());+//+// collectionManager.drop_collection("coll1");+//}+//+//TEST_F(CollectionAllFieldsTest, StringSingularAllValues) {+// Collection *coll1;+//+// coll1 = collectionManager.get_collection("coll1").get();+// if (coll1 == nullptr) {+// coll1 = collectionManager.create_collection("coll1", 1, {}, "", 0, "string").get();+// }+//+// nlohmann::json doc;+// doc["title"] = "FIRST";+// doc["int_values"] = {1, 2};+//+// Option<nlohmann::json> add_op = coll1->add(doc.dump(), CREATE, "0");+// ASSERT_FALSE(add_op.ok());+// ASSERT_EQ("Field `int_values` must be a string.", add_op.error());+//+// doc["int_values"] = 123;+//+// add_op = coll1->add(doc.dump(), CREATE, "0");+// ASSERT_TRUE(add_op.ok());+//+// auto added_doc = add_op.get();+//+// ASSERT_EQ("FIRST", added_doc["title"].get<std::string>());+// ASSERT_EQ("123", added_doc["int_values"].get<std::string>());+//+// auto results = coll1->search("first", {"title"}, "", {}, sort_fields, {0}, 10, 1, FREQUENCY, {false}).get();+// ASSERT_EQ(1, results["hits"].size());+// ASSERT_EQ("FIRST", results["hits"][0]["document"]["title"].get<std::string>());+// ASSERT_EQ("123", results["hits"][0]["document"]["int_values"].get<std::string>());+//+// collectionManager.drop_collection("coll1");+//}+//+//TEST_F(CollectionAllFieldsTest, UpdateOfDocumentsInAutoMode) {+// Collection *coll1;+//+// coll1 = collectionManager.get_collection("coll1").get();+// if (coll1 == nullptr) {+// coll1 = collectionManager.create_collection("coll1", 1, {}, "", 0, field_types::AUTO).get();+// }+//+// nlohmann::json doc;+// doc["title"] = "FIRST";+// doc["single_float"] = 50.50;+//+// auto add_op = coll1->add(doc.dump(), CREATE, "0", DIRTY_VALUES::COERCE_OR_REJECT);+// ASSERT_TRUE(add_op.ok());+//+// // try updating a value+// nlohmann::json update_doc;+// update_doc["single_float"] = "123";+//+// add_op = coll1->add(update_doc.dump(), UPDATE, "0", DIRTY_VALUES::COERCE_OR_REJECT);+// ASSERT_TRUE(add_op.ok());+//+// collectionManager.drop_collection("coll1");+//}+//+//TEST_F(CollectionAllFieldsTest, NormalFieldWithAutoType) {+// Collection *coll1;+//+// std::vector<field> fields = {+// field("city", field_types::AUTO, true, true),+// field("publication_year", field_types::AUTO, true, true),+// };+//+// coll1 = collectionManager.get_collection("coll1").get();+// if (coll1 == nullptr) {+// auto coll_op = collectionManager.create_collection("coll1", 1, fields, "", 0, field_types::AUTO);+// ASSERT_TRUE(coll_op.ok());+// coll1 = coll_op.get();+// }+//+// nlohmann::json doc;+// doc["title"] = "FIRST";+// doc["city"] = "Austin";+// doc["publication_year"] = 2010;+//+// auto add_op = coll1->add(doc.dump(), CREATE, "0", DIRTY_VALUES::COERCE_OR_REJECT);+// ASSERT_TRUE(add_op.ok());+//+// auto res_op = coll1->search("austin", {"city"}, "publication_year: 2010", {}, sort_fields, {0}, 10, 1, FREQUENCY, {false});+// ASSERT_TRUE(res_op.ok());+// auto results = res_op.get();+// ASSERT_EQ(1, results["hits"].size());+//+// auto schema = coll1->get_fields();+// ASSERT_EQ("city", schema[2].name);+// ASSERT_EQ(field_types::STRING, schema[2].type);+//+// ASSERT_EQ("publication_year", schema[3].name);+// ASSERT_EQ(field_types::INT64, schema[3].type);+//+// collectionManager.drop_collection("coll1");+//}+//+//TEST_F(CollectionAllFieldsTest, JsonFieldsToFieldsConversion) {+// nlohmann::json fields_json = nlohmann::json::array();+// nlohmann::json all_field;+// all_field[fields::name] = ".*";+// all_field[fields::type] = "string*";+// fields_json.emplace_back(all_field);+//+// std::string fallback_field_type;+// std::vector<field> fields;+//+// auto parse_op = field::json_fields_to_fields(false, fields_json, fallback_field_type, fields);+//+// ASSERT_TRUE(parse_op.ok());+// ASSERT_EQ(1, fields.size());+// ASSERT_EQ("string*", fallback_field_type);+// ASSERT_EQ(true, fields[0].optional);+// ASSERT_EQ(false, fields[0].facet);+// ASSERT_EQ(".*", fields[0].name);+// ASSERT_EQ("string*", fields[0].type);+//+// // non-wildcard string* field should be treated as optional by default+// fields_json = nlohmann::json::array();+// nlohmann::json string_star_field;+// string_star_field[fields::name] = "title";+// string_star_field[fields::type] = "string*";+// fields_json.emplace_back(string_star_field);+// fields.clear();+//+// parse_op = field::json_fields_to_fields(false, fields_json, fallback_field_type, fields);+// ASSERT_TRUE(parse_op.ok());+// ASSERT_EQ(true, fields[0].optional);+//+// fields_json = nlohmann::json::array();+// fields_json.emplace_back(all_field);+//+// // reject when you try to set optional to false or facet to true+// fields_json[0][fields::optional] = false;+// parse_op = field::json_fields_to_fields(false, fields_json, fallback_field_type, fields);+//+// ASSERT_FALSE(parse_op.ok());+// ASSERT_EQ("Field `.*` must be an optional field.", parse_op.error());+//+// fields_json[0][fields::optional] = true;+// fields_json[0][fields::facet] = true;+// parse_op = field::json_fields_to_fields(false, fields_json, fallback_field_type, fields);+//+// ASSERT_FALSE(parse_op.ok());+// ASSERT_EQ("Field `.*` cannot be a facet field.", parse_op.error());+//+// fields_json[0][fields::facet] = false;+//+// // can have only one ".*" field+// fields_json.emplace_back(all_field);+//+// parse_op = field::json_fields_to_fields(false, fields_json, fallback_field_type, fields);+//+// ASSERT_FALSE(parse_op.ok());+// ASSERT_EQ("There can be only one field named `.*`.", parse_op.error());+//+// // try with the `auto` type+// fields_json.clear();+// fields.clear();+// all_field[fields::type] = "auto";+// fields_json.emplace_back(all_field);+//+// parse_op = field::json_fields_to_fields(false, fields_json, fallback_field_type, fields);+// ASSERT_TRUE(parse_op.ok());+// ASSERT_EQ("auto", fields[0].type);+//+// // try with locale on a regular field+// fields_json.clear();+// fields.clear();+// all_field[fields::type] = "string";+// all_field[fields::name] = "title";+// all_field[fields::locale] = "ja";+// fields_json.emplace_back(all_field);+//+// parse_op = field::json_fields_to_fields(false, fields_json, fallback_field_type, fields);+// ASSERT_TRUE(parse_op.ok());+// ASSERT_EQ("ja", fields[0].locale);+//+// // try with locale on fallback field+// fields_json.clear();+// fields.clear();+// all_field[fields::type] = "string";+// all_field[fields::name] = ".*";+// all_field[fields::locale] = "ko";+// fields_json.emplace_back(all_field);+//+// parse_op = field::json_fields_to_fields(false, fields_json, fallback_field_type, fields);+// ASSERT_TRUE(parse_op.ok());+// ASSERT_EQ("ko", fields[0].locale);+//}+//+//TEST_F(CollectionAllFieldsTest, WildcardFacetFieldsOnAutoSchema) {+// Collection *coll1;+//+// std::vector<field> fields = {field("title", field_types::STRING, true),+// field(".*_name", field_types::STRING, true, true),};+//+// coll1 = collectionManager.get_collection("coll1").get();+// if (coll1 == nullptr) {+// coll1 = collectionManager.create_collection("coll1", 1, fields, "", 0, field_types::AUTO).get();+// }+//+// nlohmann::json doc;+// doc["title"] = "Org";+// doc["org_name"] = "Amazon";+//+// auto add_op = coll1->add(doc.dump(), CREATE);+// ASSERT_TRUE(add_op.ok());+//+// doc["title"] = "Org";+// doc["org_name"] = "Walmart";+//+// add_op = coll1->add(doc.dump(), CREATE);+// ASSERT_TRUE(add_op.ok());+//+// auto results = coll1->search("org", {"title"}, "", {"org_name"}, sort_fields, {0}, 10, 1, FREQUENCY, {false}).get();+//+// ASSERT_EQ(2, results["hits"].size());+// ASSERT_EQ("Walmart", results["hits"][0]["document"]["org_name"].get<std::string>());+// ASSERT_EQ("Amazon", results["hits"][1]["document"]["org_name"].get<std::string>());+//+// ASSERT_EQ("Amazon", results["facet_counts"][0]["counts"][0]["value"].get<std::string>());+// ASSERT_EQ(1, (int) results["facet_counts"][0]["counts"][0]["count"]);+//+// ASSERT_EQ("Walmart", results["facet_counts"][0]["counts"][1]["value"].get<std::string>());+// ASSERT_EQ(1, (int) results["facet_counts"][0]["counts"][1]["count"]);+//+// // add another type of .*_name field+//+// doc.clear();+// doc["title"] = "Company";+// doc["company_name"] = "Stark";+//+// add_op = coll1->add(doc.dump(), CREATE);+// ASSERT_TRUE(add_op.ok());+//+// results = coll1->search("*", {"title"}, "", {"company_name", "org_name"}, sort_fields, {0}, 10, 1, FREQUENCY, {false}).get();+// ASSERT_EQ(3, results["hits"].size());+//+// ASSERT_EQ("company_name", results["facet_counts"][0]["field_name"].get<std::string>());+// ASSERT_EQ(1, results["facet_counts"][0]["counts"].size());+// ASSERT_EQ("Stark", results["facet_counts"][0]["counts"][0]["value"].get<std::string>());+// ASSERT_EQ(1, (int) results["facet_counts"][0]["counts"][0]["count"]);+//+// ASSERT_EQ("org_name", results["facet_counts"][1]["field_name"].get<std::string>());+// ASSERT_EQ(2, results["facet_counts"][1]["counts"].size());+// ASSERT_EQ("Amazon", results["facet_counts"][1]["counts"][0]["value"].get<std::string>());+// ASSERT_EQ(1, (int) results["facet_counts"][1]["counts"][0]["count"]);+//+// ASSERT_EQ("Walmart", results["facet_counts"][1]["counts"][1]["value"].get<std::string>());+// ASSERT_EQ(1, (int) results["facet_counts"][1]["counts"][1]["count"]);+//+// collectionManager.drop_collection("coll1");+//}+//+//TEST_F(CollectionAllFieldsTest, WildcardFacetFieldsWithAuoFacetFieldType) {+// Collection *coll1;+//+// std::vector<field> fields = {field("title", field_types::STRING, true),+// field(".*_name", field_types::AUTO, true, true),};+//+// coll1 = collectionManager.get_collection("coll1").get();+// if (coll1 == nullptr) {+// coll1 = collectionManager.create_collection("coll1", 1, fields, "", 0, field_types::AUTO).get();+// }+//+// nlohmann::json doc;+// doc["title"] = "Org";+// doc["org_name"] = "Amazon";+// doc["year_name"] = 1990;+//+// auto add_op = coll1->add(doc.dump(), CREATE);+// ASSERT_TRUE(add_op.ok());+//+// doc["title"] = "Org";+// doc["org_name"] = "Walmart";+//+// add_op = coll1->add(doc.dump(), CREATE);+// ASSERT_TRUE(add_op.ok());+//+// auto results = coll1->search("org", {"title"}, "", {"org_name"}, sort_fields, {0}, 10, 1, FREQUENCY, {false}).get();+//+// ASSERT_EQ(2, results["hits"].size());+// ASSERT_EQ("Walmart", results["hits"][0]["document"]["org_name"].get<std::string>());+// ASSERT_EQ("Amazon", results["hits"][1]["document"]["org_name"].get<std::string>());+//+// ASSERT_EQ("Amazon", results["facet_counts"][0]["counts"][0]["value"].get<std::string>());+// ASSERT_EQ(1, (int) results["facet_counts"][0]["counts"][0]["count"]);+//+// ASSERT_EQ("Walmart", results["facet_counts"][0]["counts"][1]["value"].get<std::string>());+// ASSERT_EQ(1, (int) results["facet_counts"][0]["counts"][1]["count"]);+//+// collectionManager.drop_collection("coll1");+//}+//+//TEST_F(CollectionAllFieldsTest, WildcardFacetFieldsWithoutAutoSchema) {+// Collection *coll1;+//+// std::vector<field> fields = {field("title", field_types::STRING, true),+// field(".*_name", field_types::STRING, true, true),};+//+// coll1 = collectionManager.get_collection("coll1").get();+// if (coll1 == nullptr) {+// coll1 = collectionManager.create_collection("coll1", 1, fields, "", 0).get();+// }+//+// nlohmann::json doc;+// doc["title"] = "Org";+// doc["org_name"] = "Amazon";+//+// auto add_op = coll1->add(doc.dump(), CREATE);+// ASSERT_TRUE(add_op.ok());+//+// doc["title"] = "Org";+// doc["org_name"] = "Walmart";+//+// add_op = coll1->add(doc.dump(), CREATE);+// ASSERT_TRUE(add_op.ok());+//+// auto results = coll1->search("org", {"title"}, "", {"org_name"}, sort_fields, {0}, 10, 1, FREQUENCY, {false}).get();+//+// ASSERT_EQ(2, results["hits"].size());+// ASSERT_EQ("Walmart", results["hits"][0]["document"]["org_name"].get<std::string>());+// ASSERT_EQ("Amazon", results["hits"][1]["document"]["org_name"].get<std::string>());+//+// ASSERT_EQ("Amazon", results["facet_counts"][0]["counts"][0]["value"].get<std::string>());+// ASSERT_EQ(1, (int) results["facet_counts"][0]["counts"][0]["count"]);+//+// ASSERT_EQ("Walmart", results["facet_counts"][0]["counts"][1]["value"].get<std::string>());+// ASSERT_EQ(1, (int) results["facet_counts"][0]["counts"][1]["count"]);+//+// // add another type of .*_name field+//+// doc.clear();+// doc["title"] = "Company";+// doc["company_name"] = "Stark";+//+// add_op = coll1->add(doc.dump(), CREATE);+// ASSERT_TRUE(add_op.ok());+//+// results = coll1->search("*", {"title"}, "", {"company_name", "org_name"}, sort_fields, {0}, 10, 1, FREQUENCY, {false}).get();+// ASSERT_EQ(3, results["hits"].size());+//+// ASSERT_EQ("company_name", results["facet_counts"][0]["field_name"].get<std::string>());+// ASSERT_EQ(1, results["facet_counts"][0]["counts"].size());+// ASSERT_EQ("Stark", results["facet_counts"][0]["counts"][0]["value"].get<std::string>());+// ASSERT_EQ(1, (int) results["facet_counts"][0]["counts"][0]["count"]);+//+// ASSERT_EQ("org_name", results["facet_counts"][1]["field_name"].get<std::string>());+// ASSERT_EQ(2, results["facet_counts"][1]["counts"].size());+// ASSERT_EQ("Amazon", results["facet_counts"][1]["counts"][0]["value"].get<std::string>());+// ASSERT_EQ(1, (int) results["facet_counts"][1]["counts"][0]["count"]);+//+// ASSERT_EQ("Walmart", results["facet_counts"][1]["counts"][1]["value"].get<std::string>());+// ASSERT_EQ(1, (int) results["facet_counts"][1]["counts"][1]["count"]);+//+// // Don't allow auto detection of schema when AUTO mode is not chosen+// doc["description"] = "Stark company.";+// add_op = coll1->add(doc.dump(), CREATE);+// ASSERT_TRUE(add_op.ok());+//+// auto res_op = coll1->search("*", {"description"}, "", {}, sort_fields, {0}, 10, 1, FREQUENCY, {false});+// ASSERT_FALSE(res_op.ok());+// ASSERT_EQ("Could not find a field named `description` in the schema.", res_op.error());+//+// collectionManager.drop_collection("coll1");+//}+//+//TEST_F(CollectionAllFieldsTest, RegexpExplicitFieldTypeCoercion) {+// Collection *coll1;+//+// std::vector<field> fields = {field("title", field_types::STRING, true),+// field("i.*", field_types::INT32, false, true),+// field("s.*", field_types::STRING, false, true),+// field("a.*", field_types::STRING_ARRAY, false, true),+// field("nullsa.*", field_types::STRING_ARRAY, false, true),+// field("num.*", "string*", false, true),};+//+// coll1 = collectionManager.get_collection("coll1").get();+// if (coll1 == nullptr) {+// coll1 = collectionManager.create_collection("coll1", 1, fields, "", 0).get();+// }+//+// nlohmann::json doc;+// doc["title"] = "Rand Building";+// doc["i_age"] = "28";+// doc["s_name"] = nullptr;+// doc["a_name"] = {};+// doc["nullsa"] = nullptr;+// doc["num_employees"] = 28;+//+// // should coerce while retaining expected type+//+// auto add_op = coll1->add(doc.dump(), CREATE);+// ASSERT_TRUE(add_op.ok());+//+// auto schema = coll1->get_fields();+//+// ASSERT_EQ("a_name", schema[6].name);+// ASSERT_EQ(field_types::STRING_ARRAY, schema[6].type);+//+// ASSERT_EQ("i_age", schema[7].name);+// ASSERT_EQ(field_types::INT32, schema[7].type);+//+// ASSERT_EQ("nullsa", schema[8].name);+// ASSERT_EQ(field_types::STRING_ARRAY, schema[8].type);+//+// // num_employees field's type must be "solidified" to an actual type+// ASSERT_EQ("num_employees", schema[9].name);+// ASSERT_EQ(field_types::STRING, schema[9].type);+//+// ASSERT_EQ("s_name", schema[10].name);+// ASSERT_EQ(field_types::STRING, schema[10].type);+//+// auto results = coll1->search("rand", {"title"}, "i_age: 28", {}, sort_fields, {0}, 10, 1, FREQUENCY, {false}).get();+// ASSERT_EQ(1, results["hits"].size());+//+// collectionManager.drop_collection("coll1");+//}+//+//TEST_F(CollectionAllFieldsTest, DynamicFieldsMustOnlyBeOptional) {+// Collection *coll1;+//+// std::vector<field> bad_fields = {field("title", field_types::STRING, true),+// field(".*_name", field_types::STRING, true, false),};+//+// auto op = collectionManager.create_collection("coll1", 1, bad_fields, "", 0);+// ASSERT_FALSE(op.ok());+// ASSERT_EQ("Field `.*_name` must be an optional field.", op.error());+//+// // string* fields should only be optional+// std::vector<field> bad_fields2 = {field("title", field_types::STRING, true),+// field("name", "string*", true, false),};+//+// op = collectionManager.create_collection("coll1", 1, bad_fields2, "", 0);+// ASSERT_FALSE(op.ok());+// ASSERT_EQ("Field `name` must be an optional field.", op.error());+//+// std::vector<field> fields = {field("title", field_types::STRING, true),+// field(".*_name", field_types::STRING, true, true),};+//+// coll1 = collectionManager.get_collection("coll1").get();+// if (coll1 == nullptr) {+// op = collectionManager.create_collection("coll1", 1, fields, "", 0);+// ASSERT_TRUE(op.ok());+// coll1 = op.get();+// }+//+// ASSERT_TRUE(coll1->get_dynamic_fields()[".*_name"].optional);+//+// collectionManager.drop_collection("coll1");+//}+//+//TEST_F(CollectionAllFieldsTest, AutoAndStringStarFieldsShouldAcceptNullValues) {+// Collection *coll1;+//+// std::vector<field> fields = {+// field("foo", "string*", true, true),+// field("buzz", "auto", true, true),+// field("bar.*", "string*", true, true),+// field("baz.*", "auto", true, true),+// };+//+// coll1 = collectionManager.get_collection("coll1").get();+// if (coll1 == nullptr) {+// auto coll_op = collectionManager.create_collection("coll1", 1, fields, "", 0);+// ASSERT_TRUE(coll_op.ok());+// coll1 = coll_op.get();+// }+//+// nlohmann::json doc;+// doc["foo"] = nullptr;+// doc["buzz"] = nullptr;+// doc["bar_one"] = nullptr;+// doc["baz_one"] = nullptr;+//+// // should allow indexing of null values since all are optional+// auto add_op = coll1->add(doc.dump(), CREATE);+// ASSERT_TRUE(add_op.ok());+//+// auto res = coll1->search("*", {}, "", {}, sort_fields, {0}, 10, 1, FREQUENCY, {false}).get();+// ASSERT_EQ("0", res["hits"][0]["document"]["id"].get<std::string>());+// ASSERT_EQ(1, res["hits"][0]["document"].size());+//+// auto schema = coll1->get_fields();+// ASSERT_EQ(4, schema.size());+//+// doc["foo"] = {"hello", "world"};+// doc["buzz"] = 123;+// doc["bar_one"] = "hello";+// doc["baz_one"] = true;+//+// add_op = coll1->add(doc.dump(), CREATE);+// ASSERT_TRUE(add_op.ok());+//+// schema = coll1->get_fields();+// ASSERT_EQ(8, schema.size());+//+// ASSERT_EQ("bar_one", schema[4].name);+// ASSERT_EQ(field_types::STRING, schema[4].type);+//+// ASSERT_EQ("baz_one", schema[5].name);+// ASSERT_EQ(field_types::BOOL, schema[5].type);+//+// ASSERT_EQ("buzz", schema[6].name);+// ASSERT_EQ(field_types::INT64, schema[6].type);+//+// ASSERT_EQ("foo", schema[7].name);+// ASSERT_EQ(field_types::STRING_ARRAY, schema[7].type);+//+// collectionManager.drop_collection("coll1");+//}+//+//TEST_F(CollectionAllFieldsTest, BothFallbackAndDynamicFields) {+// Collection *coll1;+//+// std::vector<field> fields = {field("title", field_types::STRING, true),+// field(".*_name", field_types::STRING, false, true),+// field(".*_year", field_types::INT32, true, true),+// field(".*", field_types::AUTO, false, true)};+//+// coll1 = collectionManager.get_collection("coll1").get();+// if (coll1 == nullptr) {+// auto op = collectionManager.create_collection("coll1", 1, fields, "", 0, field_types::AUTO);+// ASSERT_TRUE(op.ok());+// coll1 = op.get();+// }+//+// ASSERT_EQ(4, coll1->get_fields().size());+// ASSERT_EQ(2, coll1->get_dynamic_fields().size());+//+// ASSERT_TRUE(coll1->get_dynamic_fields().count(".*_name") != 0);+// ASSERT_TRUE(coll1->get_dynamic_fields()[".*_name"].optional);+// ASSERT_FALSE(coll1->get_dynamic_fields()[".*_name"].facet);+//+// ASSERT_TRUE(coll1->get_dynamic_fields().count(".*_year") != 0);+// ASSERT_TRUE(coll1->get_dynamic_fields()[".*_year"].optional);+// ASSERT_TRUE(coll1->get_dynamic_fields()[".*_year"].facet);+//+// nlohmann::json doc;+// doc["title"] = "Amazon Inc.";+// doc["org_name"] = "Amazon";+// doc["org_year"] = 1994;+// doc["rand_int"] = 42;+// doc["rand_str"] = "fizzbuzz";+//+// auto add_op = coll1->add(doc.dump(), CREATE);+// ASSERT_TRUE(add_op.ok());+//+// // org_year should be of type int32+// auto schema = coll1->get_fields();+// ASSERT_EQ("org_year", schema[5].name);+// ASSERT_EQ(field_types::INT32, schema[5].type);+//+// auto res_op = coll1->search("Amazon", {"org_name"}, "", {"org_name"}, sort_fields, {0}, 10, 1, FREQUENCY, {false});+// ASSERT_FALSE(res_op.ok());+// ASSERT_EQ("Could not find a facet field named `org_name` in the schema.", res_op.error());+//+// auto results = coll1->search("Amazon", {"org_name"}, "", {"org_year"}, sort_fields, {0}, 10, 1, FREQUENCY, {false}).get();+// ASSERT_EQ(1, results["hits"].size());+//+// res_op = coll1->search("fizzbuzz", {"rand_str"}, "", {"rand_str"}, sort_fields, {0}, 10, 1, FREQUENCY, {false});+// ASSERT_EQ("Could not find a facet field named `rand_str` in the schema.", res_op.error());+//+// results = coll1->search("fizzbuzz", {"rand_str"}, "", {"org_year"}, sort_fields, {0}, 10, 1, FREQUENCY, {false}).get();+// ASSERT_EQ(1, results["hits"].size());+//+// collectionManager.drop_collection("coll1");+//}+//+//TEST_F(CollectionAllFieldsTest, WildcardFieldAndDictionaryField) {+// Collection *coll1;+//+// std::vector<field> fields = {field(".*", field_types::AUTO, false, true)};+//+// coll1 = collectionManager.get_collection("coll1").get();+// if (coll1 == nullptr) {+// auto op = collectionManager.create_collection("coll1", 1, fields, "", 0, field_types::AUTO, {}, {}, true);+// ASSERT_TRUE(op.ok());+// coll1 = op.get();+// }+//+// nlohmann::json doc;+// doc["year"] = 2000;+// doc["kinds"] = nlohmann::json::object();+// doc["kinds"]["CGXX"] = 13;+// doc["kinds"]["ZBXX"] = 24;+//+// auto add_op = coll1->add(doc.dump(), CREATE);+// ASSERT_TRUE(add_op.ok());+//+// auto results = coll1->search("*", {}, "year: 2000", {}, sort_fields, {0}, 10, 1, FREQUENCY, {false}).get();+// ASSERT_EQ(1, results["hits"].size());+//+// auto schema = coll1->get_fields();+// ASSERT_EQ(5, schema.size());+// ASSERT_EQ(".*", schema[0].name);+// ASSERT_EQ("kinds", schema[1].name);+// ASSERT_EQ("year", schema[2].name);+// ASSERT_EQ("kinds.ZBXX", schema[3].name);+// ASSERT_EQ("kinds.CGXX", schema[4].name);+//+// // filter on object key+// results = coll1->search("*", {}, "kinds.CGXX: 13", {}, sort_fields, {0}, 10, 1, FREQUENCY, {false}).get();+// ASSERT_EQ(1, results["hits"].size());+//+// collectionManager.drop_collection("coll1");+//}+//+//TEST_F(CollectionAllFieldsTest, DynamicFieldAndDictionaryField) {+// Collection *coll1;+//+// std::vector<field> fields = {field("k.*", field_types::STRING, false, true),+// field(".*", field_types::AUTO, false, true)};+//+// coll1 = collectionManager.get_collection("coll1").get();+// if (coll1 == nullptr) {+// auto op = collectionManager.create_collection("coll1", 1, fields, "", 0, field_types::AUTO);+// ASSERT_TRUE(op.ok());+// coll1 = op.get();+// }+//+// nlohmann::json doc;+// doc["kinds"] = nlohmann::json::object();+// doc["kinds"]["CGXX"] = 13;+// doc["kinds"]["ZBXX"] = 24;+//+// auto add_op = coll1->add(doc.dump(), CREATE);+// ASSERT_FALSE(add_op.ok());+// ASSERT_EQ("Field `kinds` must be a string.", add_op.error());+//+// collectionManager.drop_collection("coll1");+//}+//+//TEST_F(CollectionAllFieldsTest, RegexpIntFieldWithFallbackStringType) {+// Collection *coll1;+//+// std::vector<field> fields = {field("title", field_types::STRING, true),+// field("n.*", field_types::INT32, false, true),+// field("s.*", "string*", false, true),+// field(".*", field_types::STRING, false, true)};+//+// coll1 = collectionManager.get_collection("coll1").get();+// if (coll1 == nullptr) {+// auto op = collectionManager.create_collection("coll1", 1, fields, "", 0, field_types::STRING);+// ASSERT_TRUE(op.ok());+// coll1 = op.get();+// }+//+// nlohmann::json doc;+// doc["title"] = "Amazon Inc.";+// doc["n_age"] = 32;+// doc["s_tags"] = {"shopping"};+// doc["rand_str"] = "fizzbuzz";+//+// auto add_op = coll1->add(doc.dump(), CREATE);+// ASSERT_TRUE(add_op.ok());+//+// // n_age should be of type int32+// auto schema = coll1->get_fields();+//+// ASSERT_EQ("n_age", schema[4].name);+// ASSERT_EQ(field_types::INT32, schema[4].type);+//+// ASSERT_EQ("rand_str", schema[5].name);+// ASSERT_EQ(field_types::STRING, schema[5].type);+//+// ASSERT_EQ("s_tags", schema[6].name);+// ASSERT_EQ(field_types::STRING_ARRAY, schema[6].type);+//+// collectionManager.drop_collection("coll1");+//}+//+//TEST_F(CollectionAllFieldsTest, ContainingWildcardOnlyField) {+// Collection *coll1;+//+// std::vector<field> fields = {field("company_name", field_types::STRING, false),+// field("num_employees", field_types::INT32, false),+// field(".*", field_types::BOOL, true, true)};+//+// coll1 = collectionManager.get_collection("coll1").get();+// if (coll1 == nullptr) {+// auto op = collectionManager.create_collection("coll1", 1, fields, "", 0, field_types::BOOL);+// ASSERT_TRUE(op.ok());+// coll1 = op.get();+// }+//+// nlohmann::json doc;+// doc["company_name"] = "Amazon Inc.";+// doc["num_employees"] = 2000;+// doc["country"] = "USA";+//+// auto add_op = coll1->add(doc.dump(), CREATE);+// ASSERT_FALSE(add_op.ok());+// ASSERT_EQ("Field `country` must be a bool.", add_op.error());+//+// collectionManager.drop_collection("coll1");+//}+//+//TEST_F(CollectionAllFieldsTest, DoNotIndexFieldMarkedAsNonIndex) {+// Collection *coll1;+//+// std::vector<field> fields = {field("company_name", field_types::STRING, false),+// field("num_employees", field_types::INT32, false),+// field("post", field_types::STRING, false, true, false),+// field(".*_txt", field_types::STRING, false, true, false),+// field(".*", field_types::AUTO, false, true)};+//+// coll1 = collectionManager.get_collection("coll1").get();+// if (coll1 == nullptr) {+// auto op = collectionManager.create_collection("coll1", 1, fields, "", 0, field_types::AUTO);+// ASSERT_TRUE(op.ok());+// coll1 = op.get();+// }+//+// nlohmann::json doc;+// doc["company_name"] = "Amazon Inc.";+// doc["num_employees"] = 2000;+// doc["post"] = "Some post.";+// doc["description_txt"] = "Rome was not built in a day.";+//+// auto add_op = coll1->add(doc.dump(), CREATE);+// ASSERT_TRUE(add_op.ok());+//+// ASSERT_EQ(0, coll1->_get_index()->_get_search_index().count("post"));+//+// auto res_op = coll1->search("Amazon", {"description_txt"}, "", {}, sort_fields, {0}, 10, 1, FREQUENCY, {false});+// ASSERT_FALSE(res_op.ok());+// ASSERT_EQ("Could not find a field named `description_txt` in the schema.", res_op.error());+//+// res_op = coll1->search("Amazon", {"post"}, "", {}, sort_fields, {0}, 10, 1, FREQUENCY, {false});+// ASSERT_FALSE(res_op.ok());+// ASSERT_EQ("Field `post` is marked as a non-indexed field in the schema.", res_op.error());+//+// // try updating a document with non-indexable field+// doc["post"] = "Some post updated.";+// auto update_op = coll1->add(doc.dump(), UPDATE, "0");+// ASSERT_TRUE(add_op.ok());+//+// ASSERT_EQ(0, coll1->_get_index()->_get_search_index().count("post"));+//+// auto res = coll1->search("Amazon", {"company_name"}, "", {}, sort_fields, {0}, 10, 1, FREQUENCY, {false}).get();+// ASSERT_EQ("Some post updated.", res["hits"][0]["document"]["post"].get<std::string>());+//+// // try to delete doc with non-indexable field+// auto del_op = coll1->remove("0");+// ASSERT_TRUE(del_op.ok());+//+// // facet search should also be disabled+// auto fs_op = coll1->search("Amazon", {"company_name"}, "", {"description_txt"}, sort_fields, {0}, 10, 1, FREQUENCY, {false});+// ASSERT_FALSE(fs_op.ok());+// ASSERT_EQ("Could not find a facet field named `description_txt` in the schema.", fs_op.error());+//+// fields = {field("company_name", field_types::STRING, false),+// field("num_employees", field_types::INT32, false),+// field("post", field_types::STRING, false, false, false),+// field(".*_txt", field_types::STRING, true, true, false),+// field(".*", field_types::AUTO, false, true)};+//+// auto op = collectionManager.create_collection("coll2", 1, fields, "", 0, field_types::AUTO);+// ASSERT_FALSE(op.ok());+// ASSERT_EQ("Field `post` must be optional since it is marked as non-indexable.", op.error());+//+// fields = {field("company_name", field_types::STRING, false),+// field("num_employees", field_types::INT32, false),+// field("post", field_types::STRING, false, true, false),+// field(".*_txt", field_types::STRING, true, false, false),+// field(".*", field_types::AUTO, false, true)};+//+// op = collectionManager.create_collection("coll2", 1, fields, "", 0, field_types::AUTO);+// ASSERT_FALSE(op.ok());+// ASSERT_EQ("Field `.*_txt` must be an optional field.", op.error());+//+// // don't allow catch all field to be non-indexable+//+// fields = {field("company_name", field_types::STRING, false),+// field("num_employees", field_types::INT32, false),+// field(".*_txt", field_types::STRING, false, true, false),+// field(".*", field_types::AUTO, false, true, false)};+//+// op = collectionManager.create_collection("coll2", 1, fields, "", 0, field_types::AUTO);+// ASSERT_FALSE(op.ok());+// ASSERT_EQ("Field `.*` cannot be marked as non-indexable.", op.error());+//+// // allow auto field to be non-indexable+//+// fields = {field("company_name", field_types::STRING, false),+// field("num_employees", field_types::INT32, false),+// field("noidx_.*", field_types::AUTO, false, true, false)};+//+// op = collectionManager.create_collection("coll3", 1, fields, "", 0, field_types::AUTO);+// ASSERT_TRUE(op.ok());+//+// // don't allow facet to be true when index is false+// fields = {field("company_name", field_types::STRING, false),+// field("num_employees", field_types::INT32, false),+// field("facet_noindex", field_types::STRING, true, true, false)};+//+// op = collectionManager.create_collection("coll4", 1, fields, "", 0, field_types::AUTO);+// ASSERT_FALSE(op.ok());+// ASSERT_EQ("Field `facet_noindex` cannot be a facet since it's marked as non-indexable.", op.error());+//+// collectionManager.drop_collection("coll1");+// collectionManager.drop_collection("coll2");+// collectionManager.drop_collection("coll3");+// collectionManager.drop_collection("coll4");+//}+//+//TEST_F(CollectionAllFieldsTest, NullValueUpdate) {+// Collection *coll1;+//+// std::vector<field> fields = {field("title", field_types::STRING, false, true),+// field(".*_name", field_types::STRING, true, true),+// field("unindexed", field_types::STRING, false, true, false),+// field(".*", field_types::STRING, false, true)};+//+// coll1 = collectionManager.get_collection("coll1").get();+// if (coll1 == nullptr) {+// auto op = collectionManager.create_collection("coll1", 1, fields, "", 0, field_types::STRING);+// ASSERT_TRUE(op.ok());+// coll1 = op.get();+// }+//+// nlohmann::json doc;+// doc["id"] = "0";+// doc["title"] = "Running Shoes";+// doc["company_name"] = "Nike";+// doc["country"] = "USA";+// doc["unindexed"] = "Hello";+//+// auto add_op = coll1->add(doc.dump(), CREATE);+// ASSERT_TRUE(add_op.ok());+//+// doc["title"] = nullptr;+// doc["company_name"] = nullptr;+// doc["country"] = nullptr;+//+// add_op = coll1->add(doc.dump(), UPDATE);+// ASSERT_TRUE(add_op.ok());+//+// // try updating the doc with null value again+// add_op = coll1->add(doc.dump(), UPDATE);+// ASSERT_TRUE(add_op.ok());+//+// // ensure that the fields are removed from the document+// auto results = coll1->search("*", {}, "", {}, {}, {0}, 10, 1, FREQUENCY, {false}).get();+//+// ASSERT_EQ(1, results["hits"].size());+// ASSERT_EQ(2, results["hits"][0]["document"].size());+// ASSERT_EQ("Hello", results["hits"][0]["document"]["unindexed"].get<std::string>());+// ASSERT_EQ("0", results["hits"][0]["document"]["id"].get<std::string>());+//+// collectionManager.drop_collection("coll1");+//}TEST_F(CollectionAllFieldsTest, NullValueArrayUpdate) {Collection *coll1;@@ -1447,254 +1447,254 @@ TEST_F(CollectionAllFieldsTest, NullValueArrayUpdate) {collectionManager.drop_collection("coll1");}--TEST_F(CollectionAllFieldsTest, EmptyArrayShouldBeAcceptedAsFirstValueOfAutoField) {- Collection *coll1;-- std::vector<field> fields = {field(".*", field_types::AUTO, false, true)};-- coll1 = collectionManager.get_collection("coll1").get();- if (coll1 == nullptr) {- auto op = collectionManager.create_collection("coll1", 1, fields, "",- 0, field_types::AUTO);- ASSERT_TRUE(op.ok());- coll1 = op.get();- }-- nlohmann::json doc;- doc["company_name"] = "Amazon Inc.";- doc["tags"] = nlohmann::json::array();- doc["country"] = "USA";-- auto add_op = coll1->add(doc.dump(), CREATE);- ASSERT_TRUE(add_op.ok());-- collectionManager.drop_collection("coll1");-}--TEST_F(CollectionAllFieldsTest, DISABLED_SchemaUpdateShouldBeAtomicForAllFields) {- // when a given field in a document is "bad", other fields should not be partially added to schema- Collection *coll1;-- std::vector<field> fields = {field(".*", field_types::AUTO, false, true),};-- coll1 = collectionManager.get_collection("coll1").get();- if (coll1 == nullptr) {- auto op = collectionManager.create_collection("coll1", 1, fields, "",- 0, field_types::AUTO);- ASSERT_TRUE(op.ok());- coll1 = op.get();- }-- // insert a document with bad data for that key, but surrounded by "good" keys- // this should NOT end up creating schema changes- nlohmann::json doc;- doc["int_2"] = 200;-- auto add_op = coll1->add(doc.dump(), CREATE);- ASSERT_TRUE(add_op.ok());-- doc["int_1"] = 100;- doc["int_2"] = nlohmann::json::array();- doc["int_2"].push_back(nlohmann::json::object());- doc["int_3"] = 300;-- add_op = coll1->add(doc.dump(), CREATE);- ASSERT_FALSE(add_op.ok());-- auto f = coll1->get_fields();-- ASSERT_EQ(1, coll1->get_fields().size());- ASSERT_EQ(0, coll1->get_sort_fields().size());- ASSERT_EQ(0, coll1->_get_index()->_get_search_index().size());- ASSERT_EQ(0, coll1->_get_index()->_get_numerical_index().size());-- // now insert document with just "int_1" key- nlohmann::json doc2;- doc2["int_1"] = 200;- add_op = coll1->add(doc2.dump(), CREATE);- ASSERT_TRUE(add_op.ok());-- ASSERT_EQ(2, coll1->get_fields().size());- ASSERT_EQ(1, coll1->get_sort_fields().size());- ASSERT_EQ(0, coll1->_get_index()->_get_search_index().size());- ASSERT_EQ(1, coll1->_get_index()->_get_numerical_index().size());-- collectionManager.drop_collection("coll1");-}--TEST_F(CollectionAllFieldsTest, FieldNameMatchingRegexpShouldNotBeIndexed) {- std::vector<field> fields = {field(".*", field_types::AUTO, false, true),- field("title", field_types::STRING, false),- field("name.*", field_types::STRING, true, true)};-- Collection* coll1 = collectionManager.create_collection("coll1", 1, fields, "", 0, field_types::AUTO).get();-- nlohmann::json doc1;- doc1["id"] = "0";- doc1["title"] = "One Two Three";- doc1["name.*"] = "Rowling";- doc1["name.*barbaz"] = "JK";- doc1[".*"] = "foo";-- std::vector<std::string> json_lines;- json_lines.push_back(doc1.dump());-- coll1->add_many(json_lines, doc1, UPSERT);- json_lines[0] = doc1.dump();- coll1->add_many(json_lines, doc1, UPSERT);-- ASSERT_EQ(1, coll1->_get_index()->_get_search_index().size());- ASSERT_EQ(3, coll1->get_fields().size());-- auto results = coll1->search("one", {"title"},- "", {}, {}, {2}, 10,- 1, FREQUENCY, {true},- 1, spp::sparse_hash_set<std::string>(),- spp::sparse_hash_set<std::string>(), 10, "", 30, 4, "title", 5, {}, {}, {}, 0,- "<mark>", "</mark>", {}, 1000, true).get();-- ASSERT_EQ(1, results["hits"].size());-}--TEST_F(CollectionAllFieldsTest, FieldNameMatchingRegexpShouldNotBeIndexedInNonAutoSchema) {- std::vector<field> fields = {field("title", field_types::STRING, false),- field("name.*", field_types::STRING, true, true)};-- Collection* coll1 = collectionManager.create_collection("coll1", 1, fields, "", 0, field_types::AUTO).get();-- nlohmann::json doc1;- doc1["id"] = "0";- doc1["title"] = "One Two Three";- doc1["name.*"] = "Rowling";- doc1["name.*barbaz"] = "JK";- doc1[".*"] = "foo";-- std::vector<std::string> json_lines;- json_lines.push_back(doc1.dump());-- coll1->add_many(json_lines, doc1, UPSERT);- json_lines[0] = doc1.dump();- coll1->add_many(json_lines, doc1, UPSERT);-- ASSERT_EQ(1, coll1->_get_index()->_get_search_index().size());- ASSERT_EQ(2, coll1->get_fields().size());-- auto results = coll1->search("one", {"title"},- "", {}, {}, {2}, 10,- 1, FREQUENCY, {true},- 1, spp::sparse_hash_set<std::string>(),- spp::sparse_hash_set<std::string>(), 10, "", 30, 4, "title", 5, {}, {}, {}, 0,- "<mark>", "</mark>", {}, 1000, true).get();-- ASSERT_EQ(1, results["hits"].size());-}--TEST_F(CollectionAllFieldsTest, EmbedFromFieldJSONInvalidField) {- TextEmbedderManager::set_model_dir("/tmp/typensense_test/models");- nlohmann::json field_json;- field_json["name"] = "embedding";- field_json["type"] = "float[]";- field_json["embed"] = nlohmann::json::object();- field_json["embed"]["from"] = {"name"};- field_json["embed"]["model_config"] = nlohmann::json::object();- field_json["embed"]["model_config"]["model_name"] = "ts/e5-small";-- std::vector<field> fields;- std::string fallback_field_type;- auto arr = nlohmann::json::array();- arr.push_back(field_json);-- auto field_op = field::json_fields_to_fields(false, arr, fallback_field_type, fields);-- ASSERT_FALSE(field_op.ok());- ASSERT_EQ("Property `embed.from` can only refer to string or string array fields.", field_op.error());-}--TEST_F(CollectionAllFieldsTest, EmbedFromNotArray) {- TextEmbedderManager::set_model_dir("/tmp/typesense_test/models");- nlohmann::json field_json;- field_json["name"] = "embedding";- field_json["type"] = "float[]";- field_json["embed"] = nlohmann::json::object();- field_json["embed"]["from"] = "name";- field_json["embed"]["model_config"] = nlohmann::json::object();- field_json["embed"]["model_config"]["model_name"] = "ts/e5-small";-- std::vector<field> fields;- std::string fallback_field_type;- auto arr = nlohmann::json::array();- arr.push_back(field_json);-- auto field_op = field::json_fields_to_fields(false, arr, fallback_field_type, fields);-- ASSERT_FALSE(field_op.ok());- ASSERT_EQ("Property `embed.from` must be an array.", field_op.error());-}--TEST_F(CollectionAllFieldsTest, ModelParametersWithoutEmbedFrom) {- TextEmbedderManager::set_model_dir("/tmp/typesense_test/models");- nlohmann::json field_json;- field_json["name"] = "embedding";- field_json["type"] = "float[]";- field_json["embed"]["model_config"] = nlohmann::json::object();- field_json["embed"]["model_config"]["model_name"] = "ts/e5-small";-- std::vector<field> fields;- std::string fallback_field_type;- auto arr = nlohmann::json::array();- arr.push_back(field_json);-- auto field_op = field::json_fields_to_fields(false, arr, fallback_field_type, fields);- ASSERT_FALSE(field_op.ok());- ASSERT_EQ("Property `embed` must contain a `from` property.", field_op.error());-}---TEST_F(CollectionAllFieldsTest, EmbedFromBasicValid) {-- TextEmbedderManager::set_model_dir("/tmp/typesense_test/models");-- field embedding = field("embedding", field_types::FLOAT_ARRAY, false);- embedding.embed["from"].push_back("name");- embedding.embed["model_config"]["model_name"] = "ts/e5-small";- std::vector<field> fields = {field("name", field_types::STRING, false),- embedding};- auto obj_coll_op = collectionManager.create_collection("obj_coll", 1, fields, "", 0, field_types::AUTO);-- ASSERT_TRUE(obj_coll_op.ok());- Collection* obj_coll = obj_coll_op.get();-- nlohmann::json doc1;- doc1["name"] = "One Two Three";-- auto add_res = obj_coll->add(doc1.dump());-- ASSERT_TRUE(add_res.ok());- ASSERT_TRUE(add_res.get()["name"].is_string());- ASSERT_TRUE(add_res.get()["embedding"].is_array());- ASSERT_EQ(384, add_res.get()["embedding"].size());--}--TEST_F(CollectionAllFieldsTest, WrongDataTypeForEmbedFrom) {- TextEmbedderManager::set_model_dir("/tmp/typesense_test/models");- nlohmann::json field_json;- field_json["name"] = "embedding";- field_json["type"] = "float[]";- field_json["embed"] = nlohmann::json::object();- field_json["embed"]["from"] = {"age"};- field_json["embed"]["model_config"] = nlohmann::json::object();- field_json["embed"]["model_config"]["model_name"] = "ts/e5-small";-- std::vector<field> fields;- std::string fallback_field_type;- auto arr = nlohmann::json::array();- arr.push_back(field_json);- field_json["name"] = "age";- field_json["type"] = "int32";- arr.push_back(field_json);- auto field_op = field::json_fields_to_fields(false, arr, fallback_field_type, fields);- ASSERT_FALSE(field_op.ok());- ASSERT_EQ("Property `embed.from` can only refer to string or string array fields.", field_op.error());-}+//+//TEST_F(CollectionAllFieldsTest, EmptyArrayShouldBeAcceptedAsFirstValueOfAutoField) {+// Collection *coll1;+//+// std::vector<field> fields = {field(".*", field_types::AUTO, false, true)};+//+// coll1 = collectionManager.get_collection("coll1").get();+// if (coll1 == nullptr) {+// auto op = collectionManager.create_collection("coll1", 1, fields, "",+// 0, field_types::AUTO);+// ASSERT_TRUE(op.ok());+// coll1 = op.get();+// }+//+// nlohmann::json doc;+// doc["company_name"] = "Amazon Inc.";+// doc["tags"] = nlohmann::json::array();+// doc["country"] = "USA";+//+// auto add_op = coll1->add(doc.dump(), CREATE);+// ASSERT_TRUE(add_op.ok());+//+// collectionManager.drop_collection("coll1");+//}+//+//TEST_F(CollectionAllFieldsTest, DISABLED_SchemaUpdateShouldBeAtomicForAllFields) {+// // when a given field in a document is "bad", other fields should not be partially added to schema+// Collection *coll1;+//+// std::vector<field> fields = {field(".*", field_types::AUTO, false, true),};+//+// coll1 = collectionManager.get_collection("coll1").get();+// if (coll1 == nullptr) {+// auto op = collectionManager.create_collection("coll1", 1, fields, "",+// 0, field_types::AUTO);+// ASSERT_TRUE(op.ok());+// coll1 = op.get();+// }+//+// // insert a document with bad data for that key, but surrounded by "good" keys+// // this should NOT end up creating schema changes+// nlohmann::json doc;+// doc["int_2"] = 200;+//+// auto add_op = coll1->add(doc.dump(), CREATE);+// ASSERT_TRUE(add_op.ok());+//+// doc["int_1"] = 100;+// doc["int_2"] = nlohmann::json::array();+// doc["int_2"].push_back(nlohmann::json::object());+// doc["int_3"] = 300;+//+// add_op = coll1->add(doc.dump(), CREATE);+// ASSERT_FALSE(add_op.ok());+//+// auto f = coll1->get_fields();+//+// ASSERT_EQ(1, coll1->get_fields().size());+// ASSERT_EQ(0, coll1->get_sort_fields().size());+// ASSERT_EQ(0, coll1->_get_index()->_get_search_index().size());+// ASSERT_EQ(0, coll1->_get_index()->_get_numerical_index().size());+//+// // now insert document with just "int_1" key+// nlohmann::json doc2;+// doc2["int_1"] = 200;+// add_op = coll1->add(doc2.dump(), CREATE);+// ASSERT_TRUE(add_op.ok());+//+// ASSERT_EQ(2, coll1->get_fields().size());+// ASSERT_EQ(1, coll1->get_sort_fields().size());+// ASSERT_EQ(0, coll1->_get_index()->_get_search_index().size());+// ASSERT_EQ(1, coll1->_get_index()->_get_numerical_index().size());+//+// collectionManager.drop_collection("coll1");+//}+//+//TEST_F(CollectionAllFieldsTest, FieldNameMatchingRegexpShouldNotBeIndexed) {+// std::vector<field> fields = {field(".*", field_types::AUTO, false, true),+// field("title", field_types::STRING, false),+// field("name.*", field_types::STRING, true, true)};+//+// Collection* coll1 = collectionManager.create_collection("coll1", 1, fields, "", 0, field_types::AUTO).get();+//+// nlohmann::json doc1;+// doc1["id"] = "0";+// doc1["title"] = "One Two Three";+// doc1["name.*"] = "Rowling";+// doc1["name.*barbaz"] = "JK";+// doc1[".*"] = "foo";+//+// std::vector<std::string> json_lines;+// json_lines.push_back(doc1.dump());+//+// coll1->add_many(json_lines, doc1, UPSERT);+// json_lines[0] = doc1.dump();+// coll1->add_many(json_lines, doc1, UPSERT);+//+// ASSERT_EQ(1, coll1->_get_index()->_get_search_index().size());+// ASSERT_EQ(3, coll1->get_fields().size());+//+// auto results = coll1->search("one", {"title"},+// "", {}, {}, {2}, 10,+// 1, FREQUENCY, {true},+// 1, spp::sparse_hash_set<std::string>(),+// spp::sparse_hash_set<std::string>(), 10, "", 30, 4, "title", 5, {}, {}, {}, 0,+// "<mark>", "</mark>", {}, 1000, true).get();+//+// ASSERT_EQ(1, results["hits"].size());+//}+//+//TEST_F(CollectionAllFieldsTest, FieldNameMatchingRegexpShouldNotBeIndexedInNonAutoSchema) {+// std::vector<field> fields = {field("title", field_types::STRING, false),+// field("name.*", field_types::STRING, true, true)};+//+// Collection* coll1 = collectionManager.create_collection("coll1", 1, fields, "", 0, field_types::AUTO).get();+//+// nlohmann::json doc1;+// doc1["id"] = "0";+// doc1["title"] = "One Two Three";+// doc1["name.*"] = "Rowling";+// doc1["name.*barbaz"] = "JK";+// doc1[".*"] = "foo";+//+// std::vector<std::string> json_lines;+// json_lines.push_back(doc1.dump());+//+// coll1->add_many(json_lines, doc1, UPSERT);+// json_lines[0] = doc1.dump();+// coll1->add_many(json_lines, doc1, UPSERT);+//+// ASSERT_EQ(1, coll1->_get_index()->_get_search_index().size());+// ASSERT_EQ(2, coll1->get_fields().size());+//+// auto results = coll1->search("one", {"title"},+// "", {}, {}, {2}, 10,+// 1, FREQUENCY, {true},+// 1, spp::sparse_hash_set<std::string>(),+// spp::sparse_hash_set<std::string>(), 10, "", 30, 4, "title", 5, {}, {}, {}, 0,+// "<mark>", "</mark>", {}, 1000, true).get();+//+// ASSERT_EQ(1, results["hits"].size());+//}+//+//TEST_F(CollectionAllFieldsTest, EmbedFromFieldJSONInvalidField) {+// TextEmbedderManager::set_model_dir("/tmp/typensense_test/models");+// nlohmann::json field_json;+// field_json["name"] = "embedding";+// field_json["type"] = "float[]";+// field_json["embed"] = nlohmann::json::object();+// field_json["embed"]["from"] = {"name"};+// field_json["embed"]["model_config"] = nlohmann::json::object();+// field_json["embed"]["model_config"]["model_name"] = "ts/e5-small";+//+// std::vector<field> fields;+// std::string fallback_field_type;+// auto arr = nlohmann::json::array();+// arr.push_back(field_json);+//+// auto field_op = field::json_fields_to_fields(false, arr, fallback_field_type, fields);+//+// ASSERT_FALSE(field_op.ok());+// ASSERT_EQ("Property `embed.from` can only refer to string or string array fields.", field_op.error());+//}++//TEST_F(CollectionAllFieldsTest, EmbedFromNotArray) {+// TextEmbedderManager::set_model_dir("/tmp/typesense_test/models");+// nlohmann::json field_json;+// field_json["name"] = "embedding";+// field_json["type"] = "float[]";+// field_json["embed"] = nlohmann::json::object();+// field_json["embed"]["from"] = "name";+// field_json["embed"]["model_config"] = nlohmann::json::object();+// field_json["embed"]["model_config"]["model_name"] = "ts/e5-small";+//+// std::vector<field> fields;+// std::string fallback_field_type;+// auto arr = nlohmann::json::array();+// arr.push_back(field_json);+//+// auto field_op = field::json_fields_to_fields(false, arr, fallback_field_type, fields);+//+// ASSERT_FALSE(field_op.ok());+// ASSERT_EQ("Property `embed.from` must be an array.", field_op.error());+//}+//+//TEST_F(CollectionAllFieldsTest, ModelParametersWithoutEmbedFrom) {+// TextEmbedderManager::set_model_dir("/tmp/typesense_test/models");+// nlohmann::json field_json;+// field_json["name"] = "embedding";+// field_json["type"] = "float[]";+// field_json["embed"]["model_config"] = nlohmann::json::object();+// field_json["embed"]["model_config"]["model_name"] = "ts/e5-small";+//+// std::vector<field> fields;+// std::string fallback_field_type;+// auto arr = nlohmann::json::array();+// arr.push_back(field_json);+//+// auto field_op = field::json_fields_to_fields(false, arr, fallback_field_type, fields);+// ASSERT_FALSE(field_op.ok());+// ASSERT_EQ("Property `embed` must contain a `from` property.", field_op.error());+//}+//+//+//TEST_F(CollectionAllFieldsTest, EmbedFromBasicValid) {+//+// TextEmbedderManager::set_model_dir("/tmp/typesense_test/models");+//+// field embedding = field("embedding", field_types::FLOAT_ARRAY, false);+// embedding.embed["from"].push_back("name");+// embedding.embed["model_config"]["model_name"] = "ts/e5-small";+// std::vector<field> fields = {field("name", field_types::STRING, false),+// embedding};+// auto obj_coll_op = collectionManager.create_collection("obj_coll", 1, fields, "", 0, field_types::AUTO);+//+// ASSERT_TRUE(obj_coll_op.ok());+// Collection* obj_coll = obj_coll_op.get();+//+// nlohmann::json doc1;+// doc1["name"] = "One Two Three";+//+// auto add_res = obj_coll->add(doc1.dump());+//+// ASSERT_TRUE(add_res.ok());+// ASSERT_TRUE(add_res.get()["name"].is_string());+// ASSERT_TRUE(add_res.get()["embedding"].is_array());+// ASSERT_EQ(384, add_res.get()["embedding"].size());+//+//}+//+//TEST_F(CollectionAllFieldsTest, WrongDataTypeForEmbedFrom) {+// TextEmbedderManager::set_model_dir("/tmp/typesense_test/models");+// nlohmann::json field_json;+// field_json["name"] = "embedding";+// field_json["type"] = "float[]";+// field_json["embed"] = nlohmann::json::object();+// field_json["embed"]["from"] = {"age"};+// field_json["embed"]["model_config"] = nlohmann::json::object();+// field_json["embed"]["model_config"]["model_name"] = "ts/e5-small";+//+// std::vector<field> fields;+// std::string fallback_field_type;+// auto arr = nlohmann::json::array();+// arr.push_back(field_json);+// field_json["name"] = "age";+// field_json["type"] = "int32";+// arr.push_back(field_json);+// auto field_op = field::json_fields_to_fields(false, arr, fallback_field_type, fields);+// ASSERT_FALSE(field_op.ok());+// ASSERT_EQ("Property `embed.from` can only refer to string or string array fields.", field_op.error());+//}diff --git a/test/collection_join_test.cpp b/test/collection_join_test.cppindex 7f453b27..f334bb3f 100644--- a/test/collection_join_test.cpp+++ b/test/collection_join_test.cpp@@ -395,10 +395,11 @@ TEST_F(CollectionJoinTest, FilterByReference_SingleMatch) {search_op = coll->search("s", {"product_name"}, "$Customers(foo:=customer_a)", {}, {}, {0},10, 1, FREQUENCY, {true}, Index::DROP_TOKENS_THRESHOLD);ASSERT_FALSE(search_op.ok());- ASSERT_EQ(search_op.error(), "Failed to apply reference filter on `Customers` collection: Could not find a filter field named `foo` in the schema.");+ ASSERT_EQ(search_op.error(), "Failed to apply reference filter on `Customers` collection: Could not find a filter "+ "field named `foo` in the schema.");- auto result = coll->search("s", {"product_name"}, "$Customers(customer_id:=customer_a && product_price:<100)", {}, {}, {0},- 10, 1, FREQUENCY, {true}, Index::DROP_TOKENS_THRESHOLD).get();+ auto result = coll->search("s", {"product_name"}, "$Customers(customer_id:=customer_a && product_price:<100)", {},+ {}, {0}, 10, 1, FREQUENCY, {true}, Index::DROP_TOKENS_THRESHOLD).get();ASSERT_EQ(1, result["found"].get<size_t>());ASSERT_EQ(1, result["hits"].size());@@ -1018,14 +1019,15 @@ TEST_F(CollectionJoinTest, FilterByNReferences) {collectionManager.drop_collection("Links");}-TEST_F(CollectionJoinTest, IncludeFieldsByReference_SingleMatch) {+TEST_F(CollectionJoinTest, IncludeExcludeFieldsByReference_SingleMatch) {auto schema_json =R"({"name": "Products","fields": [{"name": "product_id", "type": "string"},{"name": "product_name", "type": "string"},- {"name": "product_description", "type": "string"}+ {"name": "product_description", "type": "string"},+ {"name": "embedding", "type":"float[]", "embed":{"from": ["product_description"], "model_config": {"model_name": "ts/e5-small"}}}]})"_json;std::vector<nlohmann::json> documents = {@@ -1040,6 +1042,9 @@ TEST_F(CollectionJoinTest, IncludeFieldsByReference_SingleMatch) {"product_description": "Introducing our all-natural, organic soap bar made with essential oils and botanical ingredients."})"_json};++ TextEmbedderManager::set_model_dir("/tmp/typesense_test/models");+auto collection_create_op = collectionManager.create_collection(schema_json);ASSERT_TRUE(collection_create_op.ok());for (auto const &json: documents) {@@ -1098,17 +1103,16 @@ TEST_F(CollectionJoinTest, IncludeFieldsByReference_SingleMatch) {std::map<std::string, std::string> req_params = {{"collection", "Products"},- {"q", "s"},+ {"q", "*"},{"query_by", "product_name"},{"filter_by", "$Customers(customer_id:=customer_a && product_price:<100)"},+ {"include_fields", "$foo.bar"}};-nlohmann::json embedded_params;std::string json_res;auto now_ts = std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::system_clock::now().time_since_epoch()).count();- req_params["include_fields"] = "$foo.bar";auto search_op = collectionManager.do_search(req_params, embedded_params, json_res, now_ts);ASSERT_FALSE(search_op.ok());ASSERT_EQ("Invalid reference in include_fields, expected `$CollectionName(fieldA, ...)`.", search_op.error());@@ -1118,62 +1122,240 @@ TEST_F(CollectionJoinTest, IncludeFieldsByReference_SingleMatch) {ASSERT_FALSE(search_op.ok());ASSERT_EQ("Invalid reference in include_fields, expected `$CollectionName(fieldA, ...)`.", search_op.error());-// req_params["include_fields"] = "$foo(bar)";-// search_op = collectionManager.do_search(req_params, embedded_params, json_res, now_ts);-// ASSERT_FALSE(search_op.ok());-// ASSERT_EQ("Referenced collection `foo` not found.", search_op.error());-//-// req_params["include_fields"] = "$Customers(bar)";-// search_op = collectionManager.do_search(req_params, embedded_params, json_res, now_ts);-// ASSERT_TRUE(search_op.ok());-//-// nlohmann::json res_obj = nlohmann::json::parse(json_res);-// ASSERT_EQ(1, res_obj["found"].get<size_t>());-// ASSERT_EQ(1, res_obj["hits"].size());-// ASSERT_EQ(0, res_obj["hits"][0]["document"].size());-//-// req_params["include_fields"] = "$Customers(product_price)";-// search_op = collectionManager.do_search(req_params, embedded_params, json_res, now_ts);-// ASSERT_TRUE(search_op.ok());-//-// res_obj = nlohmann::json::parse(json_res);-// ASSERT_EQ(1, res_obj["found"].get<size_t>());-// ASSERT_EQ(1, res_obj["hits"].size());-// ASSERT_EQ(1, res_obj["hits"][0]["document"].size());-// ASSERT_EQ(1, res_obj["hits"][0]["document"].count("product_price"));-// ASSERT_EQ(73.5, res_obj["hits"][0]["document"].at("product_price"));-//-// req_params["include_fields"] = "$Customers(product_price, customer_id)";-// search_op = collectionManager.do_search(req_params, embedded_params, json_res, now_ts);-// ASSERT_TRUE(search_op.ok());-//-// res_obj = nlohmann::json::parse(json_res);-// ASSERT_EQ(1, res_obj["found"].get<size_t>());-// ASSERT_EQ(1, res_obj["hits"].size());-// ASSERT_EQ(2, res_obj["hits"][0]["document"].size());-// ASSERT_EQ(1, res_obj["hits"][0]["document"].count("product_price"));-// ASSERT_EQ(73.5, res_obj["hits"][0]["document"].at("product_price"));-// ASSERT_EQ(1, res_obj["hits"][0]["document"].count("customer_id"));-// ASSERT_EQ("customer_a", res_obj["hits"][0]["document"].at("customer_id"));-//-// req_params["include_fields"] = "*, $Customers(product_price, customer_id)";-// search_op = collectionManager.do_search(req_params, embedded_params, json_res, now_ts);-// ASSERT_TRUE(search_op.ok());-//-// res_obj = nlohmann::json::parse(json_res);-// ASSERT_EQ(1, res_obj["found"].get<size_t>());-// ASSERT_EQ(1, res_obj["hits"].size());-// // 3 fields in Products document and 2 fields from Customers document-// ASSERT_EQ(5, res_obj["hits"][0]["document"].size());-//-// req_params["include_fields"] = "*, $Customers(product*)";-// search_op = collectionManager.do_search(req_params, embedded_params, json_res, now_ts);-// ASSERT_TRUE(search_op.ok());-//-// res_obj = nlohmann::json::parse(json_res);-// ASSERT_EQ(1, res_obj["found"].get<size_t>());-// ASSERT_EQ(1, res_obj["hits"].size());-// // 3 fields in Products document and 2 fields from Customers document-// ASSERT_EQ(5, res_obj["hits"][0]["document"].size());-// ASSERT_EQ(1, res_obj["hits"][0]["document"].count("product_id_sequence_id"));+ req_params["include_fields"] = "$foo(bar)";+ search_op = collectionManager.do_search(req_params, embedded_params, json_res, now_ts);+ ASSERT_FALSE(search_op.ok());+ ASSERT_EQ("Referenced collection `foo` in include_fields not found.", search_op.error());++ req_params = {+ {"collection", "Products"},+ {"q", "*"},+ {"query_by", "product_name"},+ {"filter_by", "$Customers(customer_id:=customer_a && product_price:<100)"},+ };+ search_op = collectionManager.do_search(req_params, embedded_params, json_res, now_ts);++ nlohmann::json res_obj = nlohmann::json::parse(json_res);+ ASSERT_EQ(1, res_obj["found"].get<size_t>());+ ASSERT_EQ(1, res_obj["hits"].size());+ // No fields are mentioned in `include_fields`, should include all fields of Products and Customers by default.+ ASSERT_EQ(8, res_obj["hits"][0]["document"].size());+ ASSERT_EQ(1, res_obj["hits"][0]["document"].count("product_id"));+ ASSERT_EQ(1, res_obj["hits"][0]["document"].count("product_name"));+ ASSERT_EQ(1, res_obj["hits"][0]["document"].count("product_description"));+ ASSERT_EQ(1, res_obj["hits"][0]["document"].count("embedding"));+ ASSERT_EQ(1, res_obj["hits"][0]["document"].count("customer_id"));+ ASSERT_EQ(1, res_obj["hits"][0]["document"].count("customer_name"));+ ASSERT_EQ(1, res_obj["hits"][0]["document"].count("product_price"));+ ASSERT_EQ(1, res_obj["hits"][0]["document"].count("product_id_sequence_id"));++ req_params = {+ {"collection", "Products"},+ {"q", "*"},+ {"query_by", "product_name"},+ {"filter_by", "$Customers(customer_id:=customer_a && product_price:<100)"},+ {"include_fields", "$Customers(bar)"}+ };+ search_op = collectionManager.do_search(req_params, embedded_params, json_res, now_ts);+ ASSERT_TRUE(search_op.ok());++ res_obj = nlohmann::json::parse(json_res);+ ASSERT_EQ(1, res_obj["found"].get<size_t>());+ ASSERT_EQ(1, res_obj["hits"].size());+ // No fields of Products collection are mentioned in `include_fields`, should include all of its fields by default.+ ASSERT_EQ(4, res_obj["hits"][0]["document"].size());+ ASSERT_EQ(1, res_obj["hits"][0]["document"].count("product_id"));+ ASSERT_EQ(1, res_obj["hits"][0]["document"].count("product_name"));+ ASSERT_EQ(1, res_obj["hits"][0]["document"].count("product_description"));+ ASSERT_EQ(1, res_obj["hits"][0]["document"].count("embedding"));++ req_params = {+ {"collection", "Products"},+ {"q", "*"},+ {"query_by", "product_name"},+ {"filter_by", "$Customers(customer_id:=customer_a && product_price:<100)"},+ {"include_fields", "$Customers(product_price)"}+ };+ search_op = collectionManager.do_search(req_params, embedded_params, json_res, now_ts);+ ASSERT_TRUE(search_op.ok());++ res_obj = nlohmann::json::parse(json_res);+ ASSERT_EQ(1, res_obj["found"].get<size_t>());+ ASSERT_EQ(1, res_obj["hits"].size());+ ASSERT_EQ(5, res_obj["hits"][0]["document"].size());+ ASSERT_EQ(1, res_obj["hits"][0]["document"].count("product_price"));+ ASSERT_EQ(73.5, res_obj["hits"][0]["document"].at("product_price"));++ req_params = {+ {"collection", "Products"},+ {"q", "*"},+ {"query_by", "product_name"},+ {"filter_by", "$Customers(customer_id:=customer_a && product_price:<100)"},+ {"include_fields", "$Customers(product_price, customer_id)"}+ };+ search_op = collectionManager.do_search(req_params, embedded_params, json_res, now_ts);+ ASSERT_TRUE(search_op.ok());++ res_obj = nlohmann::json::parse(json_res);+ ASSERT_EQ(1, res_obj["found"].get<size_t>());+ ASSERT_EQ(1, res_obj["hits"].size());+ ASSERT_EQ(6, res_obj["hits"][0]["document"].size());+ ASSERT_EQ(1, res_obj["hits"][0]["document"].count("product_price"));+ ASSERT_EQ(73.5, res_obj["hits"][0]["document"].at("product_price"));+ ASSERT_EQ(1, res_obj["hits"][0]["document"].count("customer_id"));+ ASSERT_EQ("customer_a", res_obj["hits"][0]["document"].at("customer_id"));++ req_params = {+ {"collection", "Products"},+ {"q", "*"},+ {"query_by", "product_name"},+ {"filter_by", "$Customers(customer_id:=customer_a && product_price:<100)"},+ {"include_fields", "*, $Customers(product_price, customer_id)"}+ };+ search_op = collectionManager.do_search(req_params, embedded_params, json_res, now_ts);+ ASSERT_TRUE(search_op.ok());++ res_obj = nlohmann::json::parse(json_res);+ ASSERT_EQ(1, res_obj["found"].get<size_t>());+ ASSERT_EQ(1, res_obj["hits"].size());+ // 4 fields in Products document and 2 fields from Customers document+ ASSERT_EQ(6, res_obj["hits"][0]["document"].size());++ req_params = {+ {"collection", "Products"},+ {"q", "*"},+ {"query_by", "product_name"},+ {"filter_by", "$Customers(customer_id:=customer_a && product_price:<100)"},+ {"include_fields", "$Customers(product*)"}+ };+ search_op = collectionManager.do_search(req_params, embedded_params, json_res, now_ts);+ ASSERT_TRUE(search_op.ok());++ res_obj = nlohmann::json::parse(json_res);+ ASSERT_EQ(1, res_obj["found"].get<size_t>());+ ASSERT_EQ(1, res_obj["hits"].size());+ // 4 fields in Products document and 2 fields from Customers document+ ASSERT_EQ(6, res_obj["hits"][0]["document"].size());+ ASSERT_EQ(1, res_obj["hits"][0]["document"].count("product_id_sequence_id"));++ req_params = {+ {"collection", "Products"},+ {"q", "s"},+ {"query_by", "product_name"},+ {"filter_by", "$Customers(customer_id:=customer_a && product_price:<100)"},+ {"include_fields", "$Customers(product*)"},+ {"exclude_fields", "$Customers(product_id_sequence_id)"}+ };+ search_op = collectionManager.do_search(req_params, embedded_params, json_res, now_ts);+ ASSERT_TRUE(search_op.ok());++ res_obj = nlohmann::json::parse(json_res);+ ASSERT_EQ(1, res_obj["found"].get<size_t>());+ ASSERT_EQ(1, res_obj["hits"].size());+ // 4 fields in Products document and 1 fields from Customers document+ ASSERT_EQ(5, res_obj["hits"][0]["document"].size());+ ASSERT_EQ(1, res_obj["hits"][0]["document"].count("product_id"));+ ASSERT_EQ(1, res_obj["hits"][0]["document"].count("product_name"));+ ASSERT_EQ(1, res_obj["hits"][0]["document"].count("product_description"));+ ASSERT_EQ(1, res_obj["hits"][0]["document"].count("product_price"));+ ASSERT_EQ(73.5, res_obj["hits"][0]["document"].at("product_price"));++ req_params = {+ {"collection", "Products"},+ {"q", "-shampoo"},+ {"query_by", "product_name"},+ {"filter_by", "$Customers(product_price:<100)"}, // This filter will match both shampoo and soap.+ {"include_fields", "product_name"},+ {"exclude_fields", "$Customers(*)"}+ };+ search_op = collectionManager.do_search(req_params, embedded_params, json_res, now_ts);+ ASSERT_TRUE(search_op.ok());++ res_obj = nlohmann::json::parse(json_res);+ ASSERT_EQ(1, res_obj["found"].get<size_t>());+ ASSERT_EQ(1, res_obj["hits"].size());+ ASSERT_EQ(1, res_obj["hits"][0]["document"].size());+ ASSERT_EQ(1, res_obj["hits"][0]["document"].count("product_name"));+ ASSERT_EQ("soap", res_obj["hits"][0]["document"].at("product_name"));++ req_params = {+ {"collection", "Products"},+ {"q", R"("soap")"},+ {"query_by", "product_name"},+ {"filter_by", "$Customers(product_price:<100)"}, // This filter will match both shampoo and soap.+ {"include_fields", "product_name"},+ {"exclude_fields", "$Customers(*)"}+ };+ search_op = collectionManager.do_search(req_params, embedded_params, json_res, now_ts);+ ASSERT_TRUE(search_op.ok());++ res_obj = nlohmann::json::parse(json_res);+ ASSERT_EQ(1, res_obj["found"].get<size_t>());+ ASSERT_EQ(1, res_obj["hits"].size());+ ASSERT_EQ(1, res_obj["hits"][0]["document"].size());+ ASSERT_EQ(1, res_obj["hits"][0]["document"].count("product_name"));+ ASSERT_EQ("soap", res_obj["hits"][0]["document"].at("product_name"));++ req_params = {+ {"collection", "Products"},+ {"q", "*"},+ {"query_by", "product_name"},+ {"filter_by", "product_name:soap && $Customers(product_price:>100)"},+ {"include_fields", "product_name, $Customers(product_price)"},+ {"exclude_fields", ""}+ };+ search_op = collectionManager.do_search(req_params, embedded_params, json_res, now_ts);+ ASSERT_TRUE(search_op.ok());++ res_obj = nlohmann::json::parse(json_res);+ ASSERT_EQ(1, res_obj["found"].get<size_t>());+ ASSERT_EQ(1, res_obj["hits"].size());+ ASSERT_EQ(2, res_obj["hits"][0]["document"].size());+ ASSERT_EQ(1, res_obj["hits"][0]["document"].count("product_name"));+ ASSERT_EQ("soap", res_obj["hits"][0]["document"].at("product_name"));+ ASSERT_EQ(1, res_obj["hits"][0]["document"].count("product_price"));+ ASSERT_EQ(140, res_obj["hits"][0]["document"].at("product_price"));++ req_params = {+ {"collection", "Products"},+ {"q", "soap"},+ {"query_by", "product_name"},+ {"filter_by", "$Customers(product_price: >0)"},+ {"include_fields", "product_name, $Customers(customer_name, product_price)"},+ {"exclude_fields", ""}+ };+ search_op = collectionManager.do_search(req_params, embedded_params, json_res, now_ts);+ ASSERT_TRUE(search_op.ok());++ res_obj = nlohmann::json::parse(json_res);+ ASSERT_EQ(1, res_obj["found"].get<size_t>());+ ASSERT_EQ(1, res_obj["hits"].size());+ ASSERT_EQ(3, res_obj["hits"][0]["document"].size());+ ASSERT_EQ(1, res_obj["hits"][0]["document"].count("product_name"));+ ASSERT_EQ("soap", res_obj["hits"][0]["document"].at("product_name"));+ ASSERT_EQ(1, res_obj["hits"][0]["document"].count("customer_name"));+ ASSERT_EQ("Joe", res_obj["hits"][0]["document"].at("customer_name").at(0));+ ASSERT_EQ("Dan", res_obj["hits"][0]["document"].at("customer_name").at(1));+ ASSERT_EQ(1, res_obj["hits"][0]["document"].count("product_price"));+ ASSERT_EQ(73.5, res_obj["hits"][0]["document"].at("product_price").at(0));+ ASSERT_EQ(140, res_obj["hits"][0]["document"].at("product_price").at(1));++ req_params = {+ {"collection", "Products"},+ {"q", "natural products"},+ {"query_by", "embedding"},+ {"filter_by", "$Customers(customer_id:=customer_a && product_price:<100)"},+ {"include_fields", "product_name, $Customers(product_price)"},+ {"exclude_fields", ""}+ };+ search_op = collectionManager.do_search(req_params, embedded_params, json_res, now_ts);+ ASSERT_TRUE(search_op.ok());++ res_obj = nlohmann::json::parse(json_res);+ ASSERT_EQ(1, res_obj["found"].get<size_t>());+ ASSERT_EQ(1, res_obj["hits"].size());+ ASSERT_EQ(2, res_obj["hits"][0]["document"].size());+ ASSERT_EQ(1, res_obj["hits"][0]["document"].count("product_name"));+ ASSERT_EQ(1, res_obj["hits"][0]["document"].count("product_price"));+ ASSERT_EQ(73.5, res_obj["hits"][0]["document"].at("product_price").at(0));}diff --git a/test/or_iterator_test.cpp b/test/or_iterator_test.cppindex 7b4f396b..c03a7bba 100644--- a/test/or_iterator_test.cpp+++ b/test/or_iterator_test.cpp@@ -59,8 +59,9 @@ TEST(OrIteratorTest, IntersectTwoListsWith3SubLists) {std::vector<uint32_t> results;- or_iterator_t::intersect(or_its, istate, [&results](uint32_t id, std::vector<or_iterator_t>& its) {- results.push_back(id);+ or_iterator_t::intersect(or_its, istate,+ [&results](const single_filter_result_t& filter_result, std::vector<or_iterator_t>& its) {+ results.push_back(filter_result.seq_id);});@@ -136,8 +137,9 @@ TEST(OrIteratorTest, IntersectTwoListsWith4SubLists) {std::vector<uint32_t> results;- or_iterator_t::intersect(or_its, istate, [&results](uint32_t id, std::vector<or_iterator_t>& its) {- results.push_back(id);+ or_iterator_t::intersect(or_its, istate,+ [&results](const single_filter_result_t& filter_result, std::vector<or_iterator_t>& its) {+ results.push_back(filter_result.seq_id);});std::vector<uint32_t> expected_results = {3199, 6414, 13357};@@ -202,8 +204,9 @@ TEST(OrIteratorTest, IntersectAndFilterThreeIts) {result_iter_state_t istate(nullptr, 0, &filter_ids[0], filter_ids.size());std::vector<uint32_t> results;- or_iterator_t::intersect(or_its, istate, [&results](uint32_t id, std::vector<or_iterator_t>& its) {- results.push_back(id);+ or_iterator_t::intersect(or_its, istate,+ [&results](const single_filter_result_t& filter_result, std::vector<or_iterator_t>& its) {+ results.push_back(filter_result.seq_id);});ASSERT_EQ(1, results.size());@@ -249,8 +252,9 @@ TEST(OrIteratorTest, IntersectAndFilterTwoIts) {result_iter_state_t istate(nullptr, 0, &filter_ids[0], filter_ids.size());std::vector<uint32_t> results;- or_iterator_t::intersect(or_its, istate, [&results](uint32_t id, std::vector<or_iterator_t>& its) {- results.push_back(id);+ or_iterator_t::intersect(or_its, istate,+ [&results](const single_filter_result_t& filter_result, std::vector<or_iterator_t>& its) {+ results.push_back(filter_result.seq_id);});ASSERT_EQ(1, results.size());diff --git a/test/string_utils_test.cpp b/test/string_utils_test.cppindex 75700c8a..6b931972 100644--- a/test/string_utils_test.cpp+++ b/test/string_utils_test.cpp@@ -416,7 +416,37 @@ TEST(StringUtilsTest, SplitIncludeFields) {tokens = {"id", "title", "count"};splitIncludeTestHelper(include_fields, tokens);- include_fields = "id, $Collection(title, pref*), count";+ include_fields = "id, $Collection(title, pref*), count, ";tokens = {"id", "$Collection(title, pref*)", "count"};splitIncludeTestHelper(include_fields, tokens);-}\ No newline at end of file+}++TEST(StringUtilsTest, GetReferenceCollectionNames) {+ std::string filter_query = "";+ std::set<std::string> reference_collection_names;+ StringUtils::get_reference_collection_names(filter_query, reference_collection_names);+ ASSERT_TRUE(reference_collection_names.empty());++ filter_query = "foo:bar";+ StringUtils::get_reference_collection_names(filter_query, reference_collection_names);+ ASSERT_TRUE(reference_collection_names.empty());++ filter_query = "$foo(bar:baz)";+ std::vector<std::string> result = {"foo"};+ StringUtils::get_reference_collection_names(filter_query, reference_collection_names);+ ASSERT_EQ(1, reference_collection_names.size());+ for (const auto &item: result) {+ ASSERT_EQ(1, reference_collection_names.count(item));+ }+ reference_collection_names.clear();++ filter_query = "((age: <5 || age: >10) && category:= [shoes]) &&"+ " $Customers(customer_id:=customer_a && (product_price:>100 && product_price:<200))";+ result = {"Customers"};+ StringUtils::get_reference_collection_names(filter_query, reference_collection_names);+ ASSERT_EQ(1, reference_collection_names.size());+ for (const auto &item: result) {+ ASSERT_EQ(1, reference_collection_names.count(item));+ }+ reference_collection_names.clear();+}