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

Accept null for field type auto and string*

Kishore Nallanf9446841ed35
diff --git a/include/field.h b/include/field.h
index e10cc8bb..d49d8e38 100644
--- a/include/field.h
+++ b/include/field.h
@@ -124,7 +124,7 @@ struct field {
}
static bool is_dynamic(const std::string& name, const std::string& type) {
- return type == "string*" || (name != ".*" && name.find(".*") != std::string::npos);
+ return type == "string*" || (name != ".*" && type == field_types::AUTO) || (name != ".*" && name.find(".*") != std::string::npos);
}
bool has_numerical_index() const {
@@ -236,19 +236,8 @@ struct field {
found_default_sorting_field = true;
}
- if(field.type == field_types::AUTO) {
- if(field.name.find(".*") == std::string::npos) {
- return Option<bool>(400, std::string("Cannot use type `auto` for `") +
- field.name + "`. It can be used only for a field name containing `.*`");
- }
- }
-
if(field.is_dynamic() && !field.optional) {
- if(field_types::is_string_or_array(field.type)) {
- return Option<bool>(400, "Field `" + field.name + "` must be an optional field.");
- }
-
- return Option<bool>(400, "Field `" + field.name + "` with wildcard name must be an optional field.");
+ return Option<bool>(400, "Field `" + field.name + "` must be an optional field.");
}
if(!field.index && !field.optional) {
@@ -305,15 +294,6 @@ struct field {
field_json[fields::name].get<std::string>() + std::string("` should be a boolean."));
}
- // field of type auto can be used only on a field name containing .*
- if(field_json.at(fields::type) == "auto") {
- if(field_json.at(fields::name).get<std::string>().find(".*") == std::string::npos) {
- return Option<bool>(400, std::string("Cannot use type `auto` for `") +
- field_json[fields::name].get<std::string>() +
- "`. It can be used only for a field name containing `.*`");
- }
- }
-
if(field_json.count(fields::locale) != 0){
if(!field_json.at(fields::locale).is_string()) {
return Option<bool>(400, std::string("The `locale` property of the field `") +
diff --git a/src/collection.cpp b/src/collection.cpp
index 838863e3..1fc3c38e 100644
--- a/src/collection.cpp
+++ b/src/collection.cpp
@@ -2360,7 +2360,7 @@ Option<bool> Collection::check_and_update_schema(nlohmann::json& document, const
Index* Collection::init_index() {
for(const field& field: fields) {
if(field.is_dynamic()) {
- // regexp fields are treated as dynamic fields
+ // regexp fields and fields with auto type are treated as dynamic fields
dynamic_fields.push_back(field);
continue;
}
diff --git a/test/collection_all_fields_test.cpp b/test/collection_all_fields_test.cpp
index a2d3e91c..e84e578b 100644
--- a/test/collection_all_fields_test.cpp
+++ b/test/collection_all_fields_test.cpp
@@ -551,16 +551,38 @@ TEST_F(CollectionAllFieldsTest, UpdateOfDocumentsInAutoMode) {
TEST_F(CollectionAllFieldsTest, NormalFieldWithAutoType) {
Collection *coll1;
- std::vector<field> fields = {field("publication_year", field_types::AUTO, true)};
+ std::vector<field> fields = {
+ field("city", field_types::AUTO, true, true),
+ field("publication_year", field_types::AUTO, true, true),
+ };
coll1 = collectionManager.get_collection("coll1").get();
if (coll1 == nullptr) {
auto coll_op = collectionManager.create_collection("coll1", 1, fields, "", 0, field_types::AUTO);
- ASSERT_FALSE(coll_op.ok());
- ASSERT_EQ("Cannot use type `auto` for `publication_year`. It can be used only for a field name containing `.*`",
- coll_op.error());
+ ASSERT_TRUE(coll_op.ok());
+ coll1 = coll_op.get();
}
+ nlohmann::json doc;
+ doc["title"] = "FIRST";
+ doc["city"] = "Austin";
+ doc["publication_year"] = 2010;
+
+ auto add_op = coll1->add(doc.dump(), CREATE, "0", DIRTY_VALUES::COERCE_OR_REJECT);
+ ASSERT_TRUE(add_op.ok());
+
+ auto res_op = coll1->search("austin", {"city"}, "publication_year: 2010", {}, sort_fields, {0}, 10, 1, FREQUENCY, {false});
+ ASSERT_TRUE(res_op.ok());
+ auto results = res_op.get();
+ ASSERT_EQ(1, results["hits"].size());
+
+ auto schema = coll1->get_fields();
+ ASSERT_EQ("city", schema[2].name);
+ ASSERT_EQ(field_types::STRING, schema[2].type);
+
+ ASSERT_EQ("publication_year", schema[3].name);
+ ASSERT_EQ(field_types::INT64, schema[3].type);
+
collectionManager.drop_collection("coll1");
}
@@ -890,7 +912,7 @@ TEST_F(CollectionAllFieldsTest, DynamicFieldsMustOnlyBeOptional) {
auto op = collectionManager.create_collection("coll1", 1, bad_fields, "", 0);
ASSERT_FALSE(op.ok());
- ASSERT_EQ("Field `.*_name` with wildcard name must be an optional field.", op.error());
+ ASSERT_EQ("Field `.*_name` must be an optional field.", op.error());
// string* fields should only be optional
std::vector<field> bad_fields2 = {field("title", field_types::STRING, true),
@@ -920,7 +942,7 @@ TEST_F(CollectionAllFieldsTest, AutoAndStringStarFieldsShouldAcceptNullValues) {
std::vector<field> fields = {
field("foo", "string*", true, true),
- //field("buzz", "auto", true, true),
+ field("buzz", "auto", true, true),
field("bar.*", "string*", true, true),
field("baz.*", "auto", true, true),
};
@@ -934,7 +956,7 @@ TEST_F(CollectionAllFieldsTest, AutoAndStringStarFieldsShouldAcceptNullValues) {
nlohmann::json doc;
doc["foo"] = nullptr;
- //doc["buzz"] = nullptr;
+ doc["buzz"] = nullptr;
doc["bar_one"] = nullptr;
doc["baz_one"] = nullptr;
@@ -942,6 +964,32 @@ TEST_F(CollectionAllFieldsTest, AutoAndStringStarFieldsShouldAcceptNullValues) {
auto add_op = coll1->add(doc.dump(), CREATE);
ASSERT_TRUE(add_op.ok());
+ auto schema = coll1->get_fields();
+ ASSERT_EQ(4, schema.size());
+
+ doc["foo"] = {"hello", "world"};
+ doc["buzz"] = 123;
+ doc["bar_one"] = "hello";
+ doc["baz_one"] = true;
+
+ add_op = coll1->add(doc.dump(), CREATE);
+ ASSERT_TRUE(add_op.ok());
+
+ schema = coll1->get_fields();
+ ASSERT_EQ(8, schema.size());
+
+ ASSERT_EQ("bar_one", schema[4].name);
+ ASSERT_EQ(field_types::STRING, schema[4].type);
+
+ ASSERT_EQ("baz_one", schema[5].name);
+ ASSERT_EQ(field_types::BOOL, schema[5].type);
+
+ ASSERT_EQ("buzz", schema[6].name);
+ ASSERT_EQ(field_types::INT64, schema[6].type);
+
+ ASSERT_EQ("foo", schema[7].name);
+ ASSERT_EQ(field_types::STRING_ARRAY, schema[7].type);
+
collectionManager.drop_collection("coll1");
}
@@ -1139,7 +1187,7 @@ TEST_F(CollectionAllFieldsTest, DoNotIndexFieldMarkedAsNonIndex) {
op = collectionManager.create_collection("coll2", 1, fields, "", 0, field_types::AUTO);
ASSERT_FALSE(op.ok());
- ASSERT_EQ("Field `.*_txt` with wildcard name must be an optional field.", op.error());
+ ASSERT_EQ("Field `.*_txt` must be an optional field.", op.error());
// don't allow catch all field to contain non-index field