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

Disallow use of type `auto` for a non-wildcard containing field.

Kishore Nallane200f1500dc3
diff --git a/include/field.h b/include/field.h
index fc1d2b30..ae637dca 100644
--- a/include/field.h
+++ b/include/field.h
@@ -235,6 +235,13 @@ 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.");
@@ -290,6 +297,15 @@ 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::geo_resolution) != 0) {
if(!field_json.at(fields::geo_resolution).is_number_integer()) {
return Option<bool>(400, std::string("The `geo_resolution` property of the field `") +
diff --git a/test/collection_all_fields_test.cpp b/test/collection_all_fields_test.cpp
index 0c423cab..9f77ac27 100644
--- a/test/collection_all_fields_test.cpp
+++ b/test/collection_all_fields_test.cpp
@@ -532,6 +532,22 @@ TEST_F(CollectionAllFieldsTest, UpdateOfDocumentsInAutoMode) {
collectionManager.drop_collection("coll1");
}
+TEST_F(CollectionAllFieldsTest, NormalFieldWithAutoType) {
+ Collection *coll1;
+
+ std::vector<field> fields = {field("publication_year", field_types::AUTO, 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());
+ }
+
+ collectionManager.drop_collection("coll1");
+}
+
TEST_F(CollectionAllFieldsTest, JsonFieldsToFieldsConversion) {
nlohmann::json fields_json = nlohmann::json::array();
nlohmann::json all_field;