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

Use ".*" for catch-all auto field definition.

kishorencbd43fc10bca6
diff --git a/include/field.h b/include/field.h
index f90d8df4..21173835 100644
--- a/include/field.h
+++ b/include/field.h
@@ -114,7 +114,7 @@ struct field {
}
bool is_dynamic() const {
- return name.find(".*") != std::string::npos;
+ return name != ".*" && name.find(".*") != std::string::npos;
}
bool has_numerical_index() const {
@@ -287,7 +287,7 @@ struct field {
}
}
- if(field_json["name"] == "*") {
+ if(field_json["name"] == ".*") {
if(field_json.count(fields::facet) == 0) {
field_json[fields::facet] = false;
}
@@ -357,7 +357,7 @@ struct field {
}
if(num_auto_detect_fields > 1) {
- return Option<bool>(400,"There can be only one field named `*`.");
+ return Option<bool>(400,"There can be only one field named `.*`.");
}
return Option<bool>(true);
diff --git a/src/collection.cpp b/src/collection.cpp
index 0e4e1ee4..f2003e44 100644
--- a/src/collection.cpp
+++ b/src/collection.cpp
@@ -2302,28 +2302,30 @@ Option<bool> Collection::check_and_update_schema(nlohmann::json& document, const
continue;
}
- 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(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) {
+ return Option<bool>(400, "Type of field `" + kv.key() + "` is invalid.");
+ } else {
+ // DROP or COERCE_OR_DROP
+ kv = document.erase(kv);
+ continue;
+ }
}
- }
- new_field.type = field_type;
+ new_field.type = field_type;
- if (field_types::is_string_or_array(fallback_field_type)) {
- // Supporting single/array field detection only for strings, as it does not seem to be too useful for
- // other field types.
- if (new_field.is_array()) {
- new_field.type = field_types::STRING_ARRAY;
- } else {
- new_field.type = field_types::STRING;
+ if (field_types::is_string_or_array(fallback_field_type)) {
+ // Supporting single/array field detection only for strings, as it does not seem to be too useful for
+ // other field types.
+ if (new_field.is_array()) {
+ new_field.type = field_types::STRING_ARRAY;
+ } else {
+ new_field.type = field_types::STRING;
+ }
}
- } else if(fallback_field_type != field_types::AUTO) {
+ } else {
new_field.type = fallback_field_type;
}
diff --git a/test/collection_all_fields_test.cpp b/test/collection_all_fields_test.cpp
index 8ad055f4..183c63f0 100644
--- a/test/collection_all_fields_test.cpp
+++ b/test/collection_all_fields_test.cpp
@@ -485,7 +485,7 @@ TEST_F(CollectionAllFieldsTest, UpdateOfDocumentsInAutoMode) {
TEST_F(CollectionAllFieldsTest, JsonFieldsToFieldsConversion) {
nlohmann::json fields_json = nlohmann::json::array();
nlohmann::json all_field;
- all_field[fields::name] = "*";
+ all_field[fields::name] = ".*";
all_field[fields::type] = "string*";
fields_json.emplace_back(all_field);
@@ -499,7 +499,7 @@ TEST_F(CollectionAllFieldsTest, JsonFieldsToFieldsConversion) {
ASSERT_EQ("string*", fallback_field_type);
ASSERT_EQ(true, fields[0].optional);
ASSERT_EQ(false, fields[0].facet);
- ASSERT_EQ("*", fields[0].name);
+ ASSERT_EQ(".*", fields[0].name);
ASSERT_EQ("string*", fields[0].type);
// reject when you try to set geo property on * field
@@ -526,13 +526,13 @@ TEST_F(CollectionAllFieldsTest, JsonFieldsToFieldsConversion) {
fields_json[0][fields::facet] = false;
- // can have only one "*" field
+ // can have only one ".*" field
fields_json.emplace_back(all_field);
parse_op = field::json_fields_to_fields(fields_json, fallback_field_type, fields);
ASSERT_FALSE(parse_op.ok());
- ASSERT_EQ("There can be only one field named `*`.", parse_op.error());
+ ASSERT_EQ("There can be only one field named `.*`.", parse_op.error());
// try with the `auto` type
fields_json.clear();
@@ -729,7 +729,7 @@ TEST_F(CollectionAllFieldsTest, BothFallbackAndDynamicFields) {
std::vector<field> fields = {field("title", field_types::STRING, true),
field(".*_name", field_types::STRING, false, true),
field(".*_year", field_types::INT32, true, true),
- field("*", field_types::AUTO, false, true)};
+ field(".*", field_types::AUTO, false, true)};
coll1 = collectionManager.get_collection("coll1").get();
if (coll1 == nullptr) {
@@ -780,12 +780,11 @@ TEST_F(CollectionAllFieldsTest, ContainingWildcardOnlyField) {
std::vector<field> fields = {field("company_name", field_types::STRING, false),
field("num_employees", field_types::INT32, false),
- field(".*", field_types::BOOL, true, true),
- field("*", field_types::AUTO, false, true)};
+ field(".*", field_types::BOOL, true, true)};
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::BOOL);
ASSERT_TRUE(op.ok());
coll1 = op.get();
}
@@ -809,7 +808,7 @@ TEST_F(CollectionAllFieldsTest, DoNotIndexFieldMarkedAsNonIndex) {
field("num_employees", field_types::INT32, false),
field("post", field_types::STRING, false, true, false),
field(".*_txt", field_types::STRING, true, true, false),
- field("*", field_types::AUTO, false, true)};
+ field(".*", field_types::AUTO, false, true)};
coll1 = collectionManager.get_collection("coll1").get();
if (coll1 == nullptr) {
@@ -851,7 +850,7 @@ TEST_F(CollectionAllFieldsTest, DoNotIndexFieldMarkedAsNonIndex) {
field("num_employees", field_types::INT32, false),
field("post", field_types::STRING, false, false, false),
field(".*_txt", field_types::STRING, true, true, false),
- field("*", field_types::AUTO, false, true)};
+ field(".*", field_types::AUTO, false, true)};
auto op = collectionManager.create_collection("coll2", 1, fields, "", 0, field_types::AUTO);
ASSERT_FALSE(op.ok());
@@ -861,7 +860,7 @@ TEST_F(CollectionAllFieldsTest, DoNotIndexFieldMarkedAsNonIndex) {
field("num_employees", field_types::INT32, false),
field("post", field_types::STRING, false, true, false),
field(".*_txt", field_types::STRING, true, false, false),
- field("*", field_types::AUTO, false, true)};
+ field(".*", field_types::AUTO, false, true)};
op = collectionManager.create_collection("coll2", 1, fields, "", 0, field_types::AUTO);
ASSERT_FALSE(op.ok());
@@ -872,11 +871,11 @@ TEST_F(CollectionAllFieldsTest, DoNotIndexFieldMarkedAsNonIndex) {
fields = {field("company_name", field_types::STRING, false),
field("num_employees", field_types::INT32, false),
field(".*_txt", field_types::STRING, true, true, false),
- field("*", field_types::AUTO, false, true, false)};
+ field(".*", field_types::AUTO, false, true, false)};
op = collectionManager.create_collection("coll2", 1, fields, "", 0, field_types::AUTO);
ASSERT_FALSE(op.ok());
- ASSERT_EQ("Field `*` cannot be marked as non-indexable.", op.error());
+ ASSERT_EQ("Field `.*` cannot be marked as non-indexable.", op.error());
collectionManager.drop_collection("coll1");
}