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

Fix faceting for field name with wildcard + type auto.

Kishore Nallan6cce81173731
diff --git a/src/collection.cpp b/src/collection.cpp
index 1d7491d9..4cf6656f 100644
--- a/src/collection.cpp
+++ b/src/collection.cpp
@@ -2406,42 +2406,28 @@ Option<bool> Collection::check_and_update_schema(nlohmann::json& document, const
std::string field_type;
bool parseable;
+ bool found_dynamic_field = false;
+
// check against dynamic field definitions
for(const auto& dynamic_field: dynamic_fields) {
if(std::regex_match (kv.key(), std::regex(dynamic_field.name))) {
new_field = dynamic_field;
new_field.name = fname;
-
- if (field_types::is_string_or_array(dynamic_field.type)) {
- parseable = field::get_type(kv.value(), field_type);
- if(!parseable) {
- if(dirty_values == DIRTY_VALUES::REJECT || dirty_values == DIRTY_VALUES::COERCE_OR_REJECT) {
- return Option<bool>(400, "Type of field `" + kv.key() + "` is invalid.");
- } else {
- // DROP or COERCE_OR_DROP
- kv = document.erase(kv);
- continue;
- }
- }
-
- if (field_type == field_types::STRING_ARRAY) {
- new_field.type = field_types::STRING_ARRAY;
- } else {
- new_field.type = field_types::STRING;
- }
- }
-
- goto UPDATE_SCHEMA;
+ found_dynamic_field = true;
+ break;
}
}
- if(fallback_field_type.empty()) {
- // we will not auto detect schema if auto detection is not enabled
+ if(!found_dynamic_field && fallback_field_type.empty()) {
+ // we will not auto detect schema for non-dynamic fields if auto detection is not enabled
kv++;
continue;
}
- if(fallback_field_type == field_types::AUTO || field_types::is_string_or_array(fallback_field_type)) {
+ // detect the actual type
+ if(fallback_field_type.empty() || fallback_field_type == field_types::AUTO ||
+ field_types::is_string_or_array(fallback_field_type)) {
+
parseable = field::get_type(kv.value(), field_type);
if(!parseable) {
if(dirty_values == DIRTY_VALUES::REJECT || dirty_values == DIRTY_VALUES::COERCE_OR_REJECT) {
@@ -2468,8 +2454,6 @@ Option<bool> Collection::check_and_update_schema(nlohmann::json& document, const
new_field.type = fallback_field_type;
}
- UPDATE_SCHEMA:
-
if(!new_field.index) {
kv++;
continue;
diff --git a/test/collection_all_fields_test.cpp b/test/collection_all_fields_test.cpp
index 3147e838..ccac0b09 100644
--- a/test/collection_all_fields_test.cpp
+++ b/test/collection_all_fields_test.cpp
@@ -714,6 +714,46 @@ TEST_F(CollectionAllFieldsTest, WildcardFacetFieldsOnAutoSchema) {
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;