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 Nallan8e9af6780620
diff --git a/src/field.cpp b/src/field.cpp
index 8c613e0c..67b676ea 100644
--- a/src/field.cpp
+++ b/src/field.cpp
@@ -866,6 +866,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;
@@ -877,6 +878,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;
}
}
@@ -898,7 +900,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 bcab0f73..40812ace 100644
--- a/test/collection_nested_fields_test.cpp
+++ b/test/collection_nested_fields_test.cpp
@@ -2454,6 +2454,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",