Skip to content

Open Source alternative to Algolia + Pinecone and an Easier-to-Use alternative to ElasticSearch ⚡ 🔍 ✨ Fast, typo tolerant, in-memory fuzzy Search Engine for building delightful search experiences

By default, don't enable nested fields.

Kishore Nallan134af1328122
diff --git a/include/collection.h b/include/collection.h
index 4f4ac1ae..c99ca220 100644
--- a/include/collection.h
+++ b/include/collection.h
@@ -112,7 +112,7 @@ private:
tsl::htrie_map<char, field> nested_fields;
- bool nested_fields_enabled;
+ bool enable_nested_fields;
std::vector<char> symbols_to_index;
@@ -164,7 +164,8 @@ private:
const std::unordered_map<std::string, field>& dyn_fields,
const tsl::htrie_map<char, field>& nested_fields,
const std::string& fallback_field_type,
- std::vector<field>& new_fields);
+ std::vector<field>& new_fields,
+ bool enable_nested_fields);
static bool facet_count_compare(const std::pair<uint64_t, facet_count_t>& a,
const std::pair<uint64_t, facet_count_t>& b) {
@@ -233,7 +234,8 @@ private:
static Option<bool> extract_field_name(const std::string& field_name,
const tsl::htrie_map<char, field>& search_schema,
std::vector<std::string>& processed_search_fields,
- bool extract_only_string_fields);
+ bool extract_only_string_fields,
+ bool enable_nested_fields);
static Option<bool> flatten_and_identify_new_fields(nlohmann::json& doc, const std::vector<field>& nested_fields_found,
const tsl::htrie_map<char, field>& schema,
@@ -277,7 +279,7 @@ public:
static constexpr const char* COLLECTION_CREATED = "created_at";
static constexpr const char* COLLECTION_NUM_MEMORY_SHARDS = "num_memory_shards";
static constexpr const char* COLLECTION_FALLBACK_FIELD_TYPE = "fallback_field_type";
- static constexpr const char* COLLECTION_NESTED_FIELDS_ENABLED = "nested_fields_enabled";
+ static constexpr const char* COLLECTION_ENABLE_NESTED_FIELDS = "enable_nested_fields";
static constexpr const char* COLLECTION_SYMBOLS_TO_INDEX = "symbols_to_index";
static constexpr const char* COLLECTION_SEPARATORS = "token_separators";
@@ -291,7 +293,7 @@ public:
const std::string& default_sorting_field,
const float max_memory_ratio, const std::string& fallback_field_type,
const std::vector<std::string>& symbols_to_index, const std::vector<std::string>& token_separators,
- const bool nested_fields_enabled);
+ const bool enable_nested_fields);
~Collection();
@@ -431,6 +433,8 @@ public:
std::string get_fallback_field_type();
+ bool get_enable_nested_fields();
+
// Override operations
Option<uint32_t> add_override(const override_t & override);
diff --git a/include/collection_manager.h b/include/collection_manager.h
index 618562aa..e65d9ce4 100644
--- a/include/collection_manager.h
+++ b/include/collection_manager.h
@@ -153,7 +153,7 @@ public:
const std::string& fallback_field_type = "",
const std::vector<std::string>& symbols_to_index = {},
const std::vector<std::string>& token_separators = {},
- const bool nested_fields_enabled = false);
+ const bool enable_nested_fields = false);
locked_resource_view_t<Collection> get_collection(const std::string & collection_name) const;
diff --git a/src/collection.cpp b/src/collection.cpp
index 92c1d46a..97e57d28 100644
--- a/src/collection.cpp
+++ b/src/collection.cpp
@@ -25,10 +25,10 @@ Collection::Collection(const std::string& name, const uint32_t collection_id, co
const float max_memory_ratio, const std::string& fallback_field_type,
const std::vector<std::string>& symbols_to_index,
const std::vector<std::string>& token_separators,
- const bool nested_fields_enabled):
+ const bool enable_nested_fields):
name(name), collection_id(collection_id), created_at(created_at),
next_seq_id(next_seq_id), store(store),
- fields(fields), default_sorting_field(default_sorting_field), nested_fields_enabled(nested_fields_enabled),
+ fields(fields), default_sorting_field(default_sorting_field), enable_nested_fields(enable_nested_fields),
max_memory_ratio(max_memory_ratio),
fallback_field_type(fallback_field_type), dynamic_fields({}),
symbols_to_index(to_char_array(symbols_to_index)), token_separators(to_char_array(token_separators)),
@@ -239,7 +239,7 @@ nlohmann::json Collection::add_many(std::vector<std::string>& json_lines, nlohma
search_schema, dynamic_fields,
nested_fields,
fallback_field_type,
- new_fields);
+ new_fields, enable_nested_fields);
if(!new_fields_op.ok()) {
record.index_failure(new_fields_op.code(), new_fields_op.error());
}
@@ -735,12 +735,17 @@ Option<bool> Collection::validate_and_standardize_sort_fields(const std::vector<
Option<bool> Collection::extract_field_name(const std::string& field_name,
const tsl::htrie_map<char, field>& search_schema,
std::vector<std::string>& processed_search_fields,
- const bool extract_only_string_fields) {
+ const bool extract_only_string_fields,
+ const bool enable_nested_fields) {
auto prefix_it = search_schema.equal_prefix_range(field_name);
bool field_found = false;
for(auto kv = prefix_it.first; kv != prefix_it.second; ++kv) {
if(extract_only_string_fields && !kv.value().is_string()) {
+ if(kv.value().nested && !enable_nested_fields) {
+ continue;
+ }
+
if(kv.key().size() == field_name.size() && !kv.value().is_object()) {
// exact key, must be rejected because of type mismatch
std::string error = "Field `" + field_name + "` should be a string or a string array.";;
@@ -856,7 +861,7 @@ Option<nlohmann::json> Collection::search(const std::string & raw_query,
std::vector<std::string> processed_search_fields;
for(const std::string& field_name: raw_search_fields) {
- auto field_op = extract_field_name(field_name, search_schema, processed_search_fields, true);
+ auto field_op = extract_field_name(field_name, search_schema, processed_search_fields, true, enable_nested_fields);
if(!field_op.ok()) {
return Option<nlohmann::json>(field_op.code(), field_op.error());
}
@@ -880,7 +885,7 @@ Option<nlohmann::json> Collection::search(const std::string & raw_query,
std::vector<std::string> group_by_fields;
for(const std::string& field_name: raw_group_by_fields) {
- auto field_op = extract_field_name(field_name, search_schema, group_by_fields, false);
+ auto field_op = extract_field_name(field_name, search_schema, group_by_fields, false, enable_nested_fields);
if(!field_op.ok()) {
return Option<nlohmann::json>(404, field_op.error());
}
@@ -907,14 +912,14 @@ Option<nlohmann::json> Collection::search(const std::string & raw_query,
tsl::htrie_set<char> exclude_fields_full;
for(auto& f_name: include_fields) {
- auto field_op = extract_field_name(f_name, search_schema, include_fields_vec, false);
+ auto field_op = extract_field_name(f_name, search_schema, include_fields_vec, false, enable_nested_fields);
if(!field_op.ok()) {
return Option<nlohmann::json>(404, field_op.error());
}
}
for(auto& f_name: exclude_fields) {
- auto field_op = extract_field_name(f_name, search_schema, exclude_fields_vec, false);
+ auto field_op = extract_field_name(f_name, search_schema, exclude_fields_vec, false, enable_nested_fields);
if(!field_op.ok()) {
return Option<nlohmann::json>(404, field_op.error());
}
@@ -1802,7 +1807,7 @@ void Collection::process_highlight_fields(const std::vector<std::string>& search
spp::sparse_hash_set<std::string> fields_highlighted_fully_set;
std::vector<std::string> fields_highlighted_fully_expanded;
for(const std::string& highlight_full_field: highlight_full_field_names) {
- extract_field_name(highlight_full_field, search_schema, fields_highlighted_fully_expanded, true);
+ extract_field_name(highlight_full_field, search_schema, fields_highlighted_fully_expanded, true, enable_nested_fields);
}
for(std::string & highlight_full_field: fields_highlighted_fully_expanded) {
@@ -1841,7 +1846,7 @@ void Collection::process_highlight_fields(const std::vector<std::string>& search
} else {
std::vector<std::string> highlight_field_names_expanded;
for(size_t i = 0; i < highlight_field_names.size(); i++) {
- extract_field_name(highlight_field_names[i], search_schema, highlight_field_names_expanded, true);
+ extract_field_name(highlight_field_names[i], search_schema, highlight_field_names_expanded, true, enable_nested_fields);
}
for(size_t i = 0; i < highlight_field_names_expanded.size(); i++) {
@@ -3524,7 +3529,7 @@ Option<bool> Collection::validate_alter_payload(nlohmann::json& schema_changes,
updated_search_schema, new_dynamic_fields,
nested_fields,
fallback_field_type,
- new_fields);
+ new_fields, enable_nested_fields);
if(!new_fields_op.ok()) {
return new_fields_op;
}
@@ -3683,11 +3688,15 @@ Option<bool> Collection::detect_new_fields(nlohmann::json& document,
const std::unordered_map<std::string, field>& dyn_fields,
const tsl::htrie_map<char, field>& nested_fields,
const std::string& fallback_field_type,
- std::vector<field>& new_fields) {
+ std::vector<field>& new_fields,
+ const bool enable_nested_fields) {
std::vector<field> nested_fields_found;
- for(auto& nested_field: nested_fields) {
- nested_fields_found.push_back(nested_field);
+
+ if(enable_nested_fields) {
+ for(auto& nested_field: nested_fields) {
+ nested_fields_found.push_back(nested_field);
+ }
}
auto kv = document.begin();
@@ -3750,7 +3759,11 @@ Option<bool> Collection::detect_new_fields(nlohmann::json& document,
kv++;
}
- return flatten_and_identify_new_fields(document, nested_fields_found, schema, new_fields);
+ if(enable_nested_fields) {
+ return flatten_and_identify_new_fields(document, nested_fields_found, schema, new_fields);
+ }
+
+ return Option<bool>(true);
}
Option<bool> Collection::flatten_and_identify_new_fields(nlohmann::json& doc,
@@ -3845,3 +3858,7 @@ std::vector<char> Collection::get_token_separators() {
std::string Collection::get_fallback_field_type() {
return fallback_field_type;
}
+
+bool Collection::get_enable_nested_fields() {
+ return enable_nested_fields;
+};
\ No newline at end of file
diff --git a/src/collection_manager.cpp b/src/collection_manager.cpp
index 10502dfe..76a4c728 100644
--- a/src/collection_manager.cpp
+++ b/src/collection_manager.cpp
@@ -69,8 +69,8 @@ Collection* CollectionManager::init_collection(const nlohmann::json & collection
collection_meta[Collection::COLLECTION_FALLBACK_FIELD_TYPE].get<std::string>() :
"";
- bool nested_fields_enabled = collection_meta.count(Collection::COLLECTION_NESTED_FIELDS_ENABLED) != 0 ?
- collection_meta[Collection::COLLECTION_NESTED_FIELDS_ENABLED].get<bool>() :
+ bool enable_nested_fields = collection_meta.count(Collection::COLLECTION_ENABLE_NESTED_FIELDS) != 0 ?
+ collection_meta[Collection::COLLECTION_ENABLE_NESTED_FIELDS].get<bool>() :
false;
std::vector<std::string> symbols_to_index;
@@ -97,7 +97,7 @@ Collection* CollectionManager::init_collection(const nlohmann::json & collection
fallback_field_type,
symbols_to_index,
token_separators,
- nested_fields_enabled);
+ enable_nested_fields);
return collection;
}
@@ -297,7 +297,7 @@ Option<Collection*> CollectionManager::create_collection(const std::string& name
const std::string& fallback_field_type,
const std::vector<std::string>& symbols_to_index,
const std::vector<std::string>& token_separators,
- const bool nested_fields_enabled) {
+ const bool enable_nested_fields) {
std::unique_lock lock(mutex);
if(store->contains(Collection::get_meta_key(name))) {
@@ -330,13 +330,13 @@ Option<Collection*> CollectionManager::create_collection(const std::string& name
collection_meta[Collection::COLLECTION_FALLBACK_FIELD_TYPE] = fallback_field_type;
collection_meta[Collection::COLLECTION_SYMBOLS_TO_INDEX] = symbols_to_index;
collection_meta[Collection::COLLECTION_SEPARATORS] = token_separators;
- collection_meta[Collection::COLLECTION_NESTED_FIELDS_ENABLED] = nested_fields_enabled;
+ collection_meta[Collection::COLLECTION_ENABLE_NESTED_FIELDS] = enable_nested_fields;
Collection* new_collection = new Collection(name, next_collection_id, created_at, 0, store, fields,
default_sorting_field,
this->max_memory_ratio, fallback_field_type,
symbols_to_index, token_separators,
- nested_fields_enabled);
+ enable_nested_fields);
next_collection_id++;
rocksdb::WriteBatch batch;
@@ -978,7 +978,7 @@ Option<Collection*> CollectionManager::create_collection(nlohmann::json& req_jso
const char* NUM_MEMORY_SHARDS = "num_memory_shards";
const char* SYMBOLS_TO_INDEX = "symbols_to_index";
const char* TOKEN_SEPARATORS = "token_separators";
- const char* NESTED_FIELDS_ENABLED = "nested_fields_enabled";
+ const char* ENABLE_NESTED_FIELDS = "enable_nested_fields";
const char* DEFAULT_SORTING_FIELD = "default_sorting_field";
// validate presence of mandatory fields
@@ -1003,8 +1003,8 @@ Option<Collection*> CollectionManager::create_collection(nlohmann::json& req_jso
req_json[TOKEN_SEPARATORS] = std::vector<std::string>();
}
- if(req_json.count(NESTED_FIELDS_ENABLED) == 0) {
- req_json[NESTED_FIELDS_ENABLED] = false;
+ if(req_json.count(ENABLE_NESTED_FIELDS) == 0) {
+ req_json[ENABLE_NESTED_FIELDS] = false;
}
if(req_json.count("fields") == 0) {
@@ -1032,8 +1032,8 @@ Option<Collection*> CollectionManager::create_collection(nlohmann::json& req_jso
return Option<Collection*>(400, std::string("`") + TOKEN_SEPARATORS + "` should be an array of character symbols.");
}
- if(!req_json[NESTED_FIELDS_ENABLED].is_boolean()) {
- return Option<Collection*>(400, std::string("`") + NESTED_FIELDS_ENABLED + "` should be a boolean.");
+ if(!req_json[ENABLE_NESTED_FIELDS].is_boolean()) {
+ return Option<Collection*>(400, std::string("`") + ENABLE_NESTED_FIELDS + "` should be a boolean.");
}
for (auto it = req_json[SYMBOLS_TO_INDEX].begin(); it != req_json[SYMBOLS_TO_INDEX].end(); ++it) {
@@ -1077,7 +1077,7 @@ Option<Collection*> CollectionManager::create_collection(nlohmann::json& req_jso
fallback_field_type,
req_json[SYMBOLS_TO_INDEX],
req_json[TOKEN_SEPARATORS],
- req_json[NESTED_FIELDS_ENABLED]);
+ req_json[ENABLE_NESTED_FIELDS]);
}
Option<bool> CollectionManager::load_collection(const nlohmann::json &collection_meta,
@@ -1314,7 +1314,8 @@ Option<Collection*> CollectionManager::clone_collection(const string& existing_n
auto coll_create_op = create_collection(new_name, DEFAULT_NUM_MEMORY_SHARDS, existing_coll->get_fields(),
existing_coll->get_default_sorting_field(), static_cast<uint64_t>(std::time(nullptr)),
- existing_coll->get_fallback_field_type(), symbols_to_index, token_separators);
+ existing_coll->get_fallback_field_type(), symbols_to_index, token_separators,
+ existing_coll->get_enable_nested_fields());
lock.lock();
diff --git a/src/core_api.cpp b/src/core_api.cpp
index c13f9ed7..3b19c152 100644
--- a/src/core_api.cpp
+++ b/src/core_api.cpp
@@ -167,6 +167,14 @@ bool post_create_collection(const std::shared_ptr<http_req>& req, const std::sha
const std::string SRC_COLL_NAME = "src_name";
+ /*if(res->is_alive && req_json.is_object() && req_json.count("enable_nested_fields") == 0) {
+ // This patch ensures that nested fields are only enabled for collections created on Typesense versions
+ // which support nested fields. This ensures that ".*" schema does not end up duplicating fields on
+ // manually flattened collection schemas that also contain nested versions for convenience.
+ // TO BE ENABLED WHEN READY!
+ // req_json["enable_nested_fields"] = true;
+ }*/
+
CollectionManager& collectionManager = CollectionManager::get_instance();
const Option<Collection*> &collection_op = req->params.count(SRC_COLL_NAME) != 0 ?
collectionManager.clone_collection(req->params[SRC_COLL_NAME], req_json) :
diff --git a/test/collection_all_fields_test.cpp b/test/collection_all_fields_test.cpp
index fdef4ebf..0deb0a86 100644
--- a/test/collection_all_fields_test.cpp
+++ b/test/collection_all_fields_test.cpp
@@ -1091,7 +1091,7 @@ TEST_F(CollectionAllFieldsTest, WildcardFieldAndDictionaryField) {
coll1 = collectionManager.get_collection("coll1").get();
if (coll1 == nullptr) {
- auto op = collectionManager.create_collection("coll1", 1, fields, "", 0, field_types::AUTO);
+ auto op = collectionManager.create_collection("coll1", 1, fields, "", 0, field_types::AUTO, {}, {}, true);
ASSERT_TRUE(op.ok());
coll1 = op.get();
}
diff --git a/test/collection_faceting_test.cpp b/test/collection_faceting_test.cpp
index fe8086fb..d5b07c1c 100644
--- a/test/collection_faceting_test.cpp
+++ b/test/collection_faceting_test.cpp
@@ -942,6 +942,7 @@ TEST_F(CollectionFacetingTest, FacetArrayValuesShouldBeNormalized) {
TEST_F(CollectionFacetingTest, FacetByNestedIntField) {
nlohmann::json schema = R"({
"name": "coll1",
+ "enable_nested_fields": true,
"fields": [
{"name": "details", "type": "object", "optional": false },
{"name": "company.num_employees", "type": "int32", "optional": false, "facet": true }
diff --git a/test/collection_manager_test.cpp b/test/collection_manager_test.cpp
index 4ae40b12..96acadc5 100644
--- a/test/collection_manager_test.cpp
+++ b/test/collection_manager_test.cpp
@@ -91,7 +91,7 @@ TEST_F(CollectionManagerTest, CollectionCreation) {
ASSERT_EQ(3, num_keys);
// we already call `collection1->get_next_seq_id` above, which is side-effecting
ASSERT_EQ(1, StringUtils::deserialize_uint32_t(next_seq_id));
- ASSERT_EQ("{\"created_at\":12345,\"default_sorting_field\":\"points\",\"fallback_field_type\":\"\","
+ ASSERT_EQ("{\"created_at\":12345,\"default_sorting_field\":\"points\",\"enable_nested_fields\":false,\"fallback_field_type\":\"\","
"\"fields\":[{\"facet\":false,\"index\":true,\"infix\":false,\"locale\":\"en\",\"name\":\"title\",\"optional\":false,\"sort\":false,\"type\":\"string\"},"
"{\"facet\":false,\"index\":true,\"infix\":true,\"locale\":\"\",\"name\":\"starring\",\"optional\":false,\"sort\":false,\"type\":\"string\"},"
"{\"facet\":true,\"index\":true,\"infix\":false,\"locale\":\"\",\"name\":\"cast\",\"optional\":true,\"sort\":false,\"type\":\"string[]\"},"
@@ -99,7 +99,7 @@ TEST_F(CollectionManagerTest, CollectionCreation) {
"{\"facet\":false,\"index\":true,\"infix\":false,\"locale\":\"\",\"name\":\"location\",\"optional\":true,\"sort\":true,\"type\":\"geopoint\"},"
"{\"facet\":false,\"index\":false,\"infix\":false,\"locale\":\"\",\"name\":\"not_stored\",\"optional\":true,\"sort\":false,\"type\":\"string\"},"
"{\"facet\":false,\"index\":true,\"infix\":false,\"locale\":\"\",\"name\":\"points\",\"optional\":false,\"sort\":true,\"type\":\"int32\"}],\"id\":0,"
- "\"name\":\"collection1\",\"nested_fields_enabled\":false,\"num_memory_shards\":4,\"symbols_to_index\":[\"+\"],\"token_separators\":[\"-\"]}",
+ "\"name\":\"collection1\",\"num_memory_shards\":4,\"symbols_to_index\":[\"+\"],\"token_separators\":[\"-\"]}",
collection_meta_json);
ASSERT_EQ("1", next_collection_id);
}
diff --git a/test/collection_nested_fields_test.cpp b/test/collection_nested_fields_test.cpp
index 60f887cb..560696cd 100644
--- a/test/collection_nested_fields_test.cpp
+++ b/test/collection_nested_fields_test.cpp
@@ -284,7 +284,7 @@ TEST_F(CollectionNestedFieldsTest, FlattenJSONObjectHandleErrors) {
TEST_F(CollectionNestedFieldsTest, SearchOnFieldsOnWildcardSchema) {
std::vector<field> fields = {field(".*", field_types::AUTO, false, true)};
- auto op = collectionManager.create_collection("coll1", 1, fields, "", 0, field_types::AUTO);
+ auto op = collectionManager.create_collection("coll1", 1, fields, "", 0, field_types::AUTO, {}, {}, true);
ASSERT_TRUE(op.ok());
Collection* coll1 = op.get();
@@ -436,7 +436,7 @@ TEST_F(CollectionNestedFieldsTest, IncludeExcludeFields) {
TEST_F(CollectionNestedFieldsTest, HighlightNestedFieldFully) {
std::vector<field> fields = {field(".*", field_types::AUTO, false, true)};
- auto op = collectionManager.create_collection("coll1", 1, fields, "", 0, field_types::AUTO);
+ auto op = collectionManager.create_collection("coll1", 1, fields, "", 0, field_types::AUTO, {}, {}, true);
ASSERT_TRUE(op.ok());
Collection* coll1 = op.get();
@@ -684,7 +684,7 @@ TEST_F(CollectionNestedFieldsTest, HighlightNestedFieldFully) {
TEST_F(CollectionNestedFieldsTest, HighlightShouldHaveMeta) {
std::vector<field> fields = {field(".*", field_types::AUTO, false, true)};
- auto op = collectionManager.create_collection("coll1", 1, fields, "", 0, field_types::AUTO);
+ auto op = collectionManager.create_collection("coll1", 1, fields, "", 0, field_types::AUTO, {}, {}, true);
ASSERT_TRUE(op.ok());
Collection* coll1 = op.get();
@@ -737,6 +737,7 @@ TEST_F(CollectionNestedFieldsTest, HighlightShouldHaveMeta) {
TEST_F(CollectionNestedFieldsTest, FieldsWithExplicitSchema) {
nlohmann::json schema = R"({
"name": "coll1",
+ "enable_nested_fields": true,
"fields": [
{"name": "details", "type": "object", "optional": false },
{"name": "company.name", "type": "string", "optional": false },
@@ -808,6 +809,7 @@ TEST_F(CollectionNestedFieldsTest, FieldsWithExplicitSchema) {
// explicit nested array field (locations.address.street)
schema = R"({
"name": "coll2",
+ "enable_nested_fields": true,
"fields": [
{"name": "details", "type": "object", "optional": false },
{"name": "company.name", "type": "string", "optional": false },
@@ -849,6 +851,7 @@ TEST_F(CollectionNestedFieldsTest, FieldsWithExplicitSchema) {
// explicit partial array object field in the schema
schema = R"({
"name": "coll3",
+ "enable_nested_fields": true,
"fields": [
{"name": "details", "type": "object", "optional": false },
{"name": "company.name", "type": "string", "optional": false },
@@ -920,6 +923,7 @@ TEST_F(CollectionNestedFieldsTest, FieldsWithExplicitSchema) {
TEST_F(CollectionNestedFieldsTest, ExplicitSchemaOptionalFieldValidation) {
nlohmann::json schema = R"({
"name": "coll1",
+ "enable_nested_fields": true,
"fields": [
{"name": "details", "type": "object", "optional": true },
{"name": "company.name", "type": "string", "optional": true },
@@ -963,6 +967,7 @@ TEST_F(CollectionNestedFieldsTest, ExplicitSchemaOptionalFieldValidation) {
TEST_F(CollectionNestedFieldsTest, SortByNestedField) {
nlohmann::json schema = R"({
"name": "coll1",
+ "enable_nested_fields": true,
"fields": [
{"name": "details", "type": "object", "optional": false },
{"name": "company.num_employees", "type": "int32", "optional": false }
@@ -1012,6 +1017,7 @@ TEST_F(CollectionNestedFieldsTest, SortByNestedField) {
// with auto schema
schema = R"({
"name": "coll2",
+ "enable_nested_fields": true,
"fields": [
{"name": ".*", "type": "auto"}
]
@@ -1051,6 +1057,7 @@ TEST_F(CollectionNestedFieldsTest, SortByNestedField) {
TEST_F(CollectionNestedFieldsTest, OnlyExplcitSchemaFieldMustBeIndexedInADoc) {
nlohmann::json schema = R"({
"name": "coll1",
+ "enable_nested_fields": true,
"fields": [
{"name": "company.num_employees", "type": "int32", "optional": false },
{"name": "company.founded", "type": "int32", "optional": false }
@@ -1070,12 +1077,53 @@ TEST_F(CollectionNestedFieldsTest, OnlyExplcitSchemaFieldMustBeIndexedInADoc) {
ASSERT_EQ(2, coll1->get_fields().size());
}
+TEST_F(CollectionNestedFieldsTest, VerifyDisableOfNestedFields) {
+ nlohmann::json schema = R"({
+ "name": "coll1",
+ "fields": [
+ {"name": ".*", "type": "auto"}
+ ]
+ })"_json;
+
+ auto op = collectionManager.create_collection(schema);
+ ASSERT_TRUE(op.ok());
+ Collection* coll1 = op.get();
+
+ auto doc1 = R"({
+ "company": {"num_employees": 2000, "founded": 1976, "year": 2000},
+ "company_num_employees": 2000,
+ "company_founded": 1976
+ })"_json;
+
+ ASSERT_TRUE(coll1->add(doc1.dump(), CREATE).ok());
+ auto fs = coll1->get_fields();
+ ASSERT_EQ(3, coll1->get_fields().size());
+
+ // explicit schema
+ schema = R"({
+ "name": "coll2",
+ "fields": [
+ {"name": "company_num_employees", "type": "int32"},
+ {"name": "company_founded", "type": "int32"}
+ ]
+ })"_json;
+
+ op = collectionManager.create_collection(schema);
+ ASSERT_TRUE(op.ok());
+ Collection* coll2 = op.get();
+
+ ASSERT_TRUE(coll2->add(doc1.dump(), CREATE).ok());
+ fs = coll2->get_fields();
+ ASSERT_EQ(2, coll2->get_fields().size());
+}
+
TEST_F(CollectionNestedFieldsTest, GroupByOnNestedFieldsWithWildcardSchema) {
std::vector<field> fields = {field(".*", field_types::AUTO, false, true),
field("education.name", field_types::STRING_ARRAY, true, true),
field("employee.num", field_types::INT32, true, true)};
- auto op = collectionManager.create_collection("coll1", 1, fields, "", 0, field_types::AUTO);
+ auto op = collectionManager.create_collection("coll1", 1, fields, "", 0, field_types::AUTO, {}, {},
+ true);
ASSERT_TRUE(op.ok());
Collection* coll1 = op.get();
@@ -1144,6 +1192,7 @@ TEST_F(CollectionNestedFieldsTest, GroupByOnNestedFieldsWithWildcardSchema) {
TEST_F(CollectionNestedFieldsTest, UpdateOfNestFields) {
nlohmann::json schema = R"({
"name": "coll1",
+ "enable_nested_fields": true,
"fields": [
{"name": ".*", "type": "auto"}
]
diff --git a/test/collection_specific_more_test.cpp b/test/collection_specific_more_test.cpp
index b3479d1a..b4b6aeed 100644
--- a/test/collection_specific_more_test.cpp
+++ b/test/collection_specific_more_test.cpp
@@ -841,7 +841,7 @@ TEST_F(CollectionSpecificMoreTest, CrossFieldWeightIsNotAugmentated) {
TEST_F(CollectionSpecificMoreTest, HighlightWithAccentedChars) {
std::vector<field> fields = {field(".*", field_types::AUTO, false)};
- Collection* coll1 = collectionManager.create_collection("coll1", 1, fields, "", 0, field_types::AUTO).get();
+ Collection* coll1 = collectionManager.create_collection("coll1", 1, fields, "", 0, field_types::AUTO, {}, {}, true).get();
auto nested_doc = R"({
"title": "Rāpeti Early Learning Centre",