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
Support dynamic field defining type of an object's children.
Kishore Nallana7adcf0d04d8
diff --git a/src/collection.cpp b/src/collection.cppindex f5fe1831..b0d24a1e 100644--- a/src/collection.cpp+++ b/src/collection.cpp@@ -4549,6 +4549,15 @@ Option<bool> Collection::detect_new_fields(nlohmann::json& document,new_field = dynamic_field;new_field.name = fname;found_dynamic_field = true;++ if(kv->is_object() && dynamic_field.name.find(".*") == kv.key().size()) {+ // e.g. { name => price.*, type: float } to match price.USD, price.UK etc.+ // top-level price field should be treated as type `object` and NOT `float`.+ new_field.nested = true;+ new_field.type = field_types::OBJECT;+ new_field.sort = false;+ }+break;}}diff --git a/src/field.cpp b/src/field.cppindex 25da190f..8c97bd2b 100644--- a/src/field.cpp+++ b/src/field.cpp@@ -907,7 +907,7 @@ Option<bool> field::flatten_field(nlohmann::json& doc, nlohmann::json& obj, cons}if(std::regex_match(the_field.name, std::regex(dynamic_field.name))) {- detected_type = dynamic_field.type;+ detected_type = obj.is_object() ? field_types::OBJECT : dynamic_field.type;found_dynamic_field = true;break;}diff --git a/test/collection_nested_fields_test.cpp b/test/collection_nested_fields_test.cppindex 80fc61db..20490d4a 100644--- a/test/collection_nested_fields_test.cpp+++ b/test/collection_nested_fields_test.cpp@@ -2816,6 +2816,57 @@ TEST_F(CollectionNestedFieldsTest, HighlightArrayOfObjects) {ASSERT_EQ(1, results["hits"][0]["highlight"]["details"][2].size());}+TEST_F(CollectionNestedFieldsTest, FloatInsideNestedObject) {+ nlohmann::json schema = R"({+ "name": "coll1",+ "enable_nested_fields": true,+ "fields": [+ {"name": "price.*", "type": "float"}+ ]+ })"_json;++ auto op = collectionManager.create_collection(schema);+ ASSERT_TRUE(op.ok());+ Collection *coll1 = op.get();++ auto doc1 = R"({+ "price": {+ "USD": 75.40+ }+ })"_json;++ auto add_op = coll1->add(doc1.dump(), CREATE);+ ASSERT_TRUE(add_op.ok());++ // should also accept whole numbers+ schema = R"({+ "name": "coll2",+ "enable_nested_fields": true,+ "fields": [+ {"name": "price.*", "type": "float"}+ ]+ })"_json;++ op = collectionManager.create_collection(schema);+ ASSERT_TRUE(op.ok());+ Collection *coll2 = op.get();++ auto doc2 = R"({+ "price": {+ "USD": 75+ }+ })"_json;++ add_op = coll2->add(doc2.dump(), CREATE);+ ASSERT_TRUE(add_op.ok());++ auto fs = coll2->get_fields();+ ASSERT_EQ(3, fs.size());++ add_op = coll2->add(doc1.dump(), CREATE);+ ASSERT_TRUE(add_op.ok());+}+TEST_F(CollectionNestedFieldsTest, HighlightOnFlatFieldWithSnippeting) {std::vector<field> fields = {field("title", field_types::STRING, false),field("body", field_types::STRING, false)};