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

add: tests for duplicates in fields

Harisaran Gaaa5c00c8528
diff --git a/include/field.h b/include/field.h
index 420d7fc7..a3c03d0b 100644
--- a/include/field.h
+++ b/include/field.h
@@ -203,6 +203,7 @@ struct field {
std::set<std::string> field_names;
for(const field & field: fields) {
+ field_names.insert(field.name);
if(field.name == "id") {
continue;
}
@@ -255,7 +256,6 @@ struct field {
return Option<bool>(400, "Field `" + field.name + "` cannot be a facet since "
"it's marked as non-indexable.");
}
- field_names.insert(field.name);
}
if(!default_sorting_field.empty() && !found_default_sorting_field) {
@@ -263,10 +263,12 @@ struct field {
"` but is not found in the schema.");
}
- if (field_names.size() != fields.size()) {
- return Option<bool>(400, "Duplicate field names should not be present in `fields`");
+ LOG(WARNING) << "Fields size: " << fields.size() << " Field set size: " << field_names.size();
+ if(field_names.size() != fields.size()) {
+ return Option<bool>(400, "Duplicates are not allowed in `fields`");
}
+
return Option<bool>(true);
}
diff --git a/test/collection_specific_test.cpp b/test/collection_specific_test.cpp
index 654a156c..5918407b 100644
--- a/test/collection_specific_test.cpp
+++ b/test/collection_specific_test.cpp
@@ -1991,3 +1991,11 @@ TEST_F(CollectionSpecificTest, SingleHyphenInQueryNotToBeTreatedAsExclusion) {
ASSERT_EQ(1, results["hits"].size());
collectionManager.drop_collection("coll1");
}
+
+TEST_F(CollectionSpecificTest, DuplicateFieldsNotAllowed) {
+ std::vector<field> fields = {field("title", field_types::STRING, false),
+ field("title", field_types::INT32, true),};
+ Option<Collection*> response = collectionManager.create_collection("collection", 1, fields);
+
+ ASSERT_FALSE(response.ok());
+}
\ No newline at end of file