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

Propagate dynamic field properties for nested field.

Kishore Nallanf87404de3706
diff --git a/src/field.cpp b/src/field.cpp
index 9d6a87a6..28b67aef 100644
--- a/src/field.cpp
+++ b/src/field.cpp
@@ -397,6 +397,7 @@ bool field::flatten_obj(nlohmann::json& doc, nlohmann::json& value, bool has_arr
std::string detected_type;
bool found_dynamic_field = false;
+ field dyn_field(the_field.name, field_types::STRING, false);
for(auto dyn_field_it = dyn_fields.begin(); dyn_field_it != dyn_fields.end(); dyn_field_it++) {
auto& dynamic_field = dyn_field_it->second;
@@ -408,6 +409,7 @@ bool field::flatten_obj(nlohmann::json& doc, nlohmann::json& value, bool has_arr
if(std::regex_match(flat_name, std::regex(dynamic_field.name))) {
detected_type = dynamic_field.type;
found_dynamic_field = true;
+ dyn_field = dynamic_field;
break;
}
}
@@ -429,7 +431,7 @@ bool field::flatten_obj(nlohmann::json& doc, nlohmann::json& value, bool has_arr
doc[flat_name] = value;
}
- field flattened_field = the_field;
+ field flattened_field = found_dynamic_field ? dyn_field : the_field;
flattened_field.name = flat_name;
flattened_field.type = detected_type;
flattened_field.optional = true;
diff --git a/test/collection_nested_fields_test.cpp b/test/collection_nested_fields_test.cpp
index 6572be2b..90232b7d 100644
--- a/test/collection_nested_fields_test.cpp
+++ b/test/collection_nested_fields_test.cpp
@@ -2503,6 +2503,34 @@ TEST_F(CollectionNestedFieldsTest, NestedSchemaAutoAndFacet) {
ASSERT_TRUE(coll1->get_schema()["schools.name"].optional);
}
+TEST_F(CollectionNestedFieldsTest, NestedObjectOfObjectEnableFacet) {
+ nlohmann::json schema = R"({
+ "name": "coll1",
+ "enable_nested_fields": true,
+ "fields": [
+ {"name": "variants", "type": "object"},
+ {"name": "variants\\..*\\.price", "type": "int64", "facet": true}
+ ]
+ })"_json;
+
+ auto op = collectionManager.create_collection(schema);
+ ASSERT_TRUE(op.ok());
+ Collection* coll1 = op.get();
+
+ auto doc1 = R"({
+ "variants": {
+ "store_1": {"price": 100},
+ "store_2": {"price": 200}
+ }
+ })"_json;
+
+ auto add_op = coll1->add(doc1.dump(), CREATE);
+ ASSERT_TRUE(add_op.ok());
+
+ ASSERT_TRUE(coll1->get_schema()["variants.store_1.price"].facet);
+ ASSERT_TRUE(coll1->get_schema()["variants.store_2.price"].facet);
+}
+
TEST_F(CollectionNestedFieldsTest, ArrayOfObjectsFaceting) {
nlohmann::json schema = R"({
"name": "coll1",