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

Ignore id field present in schema.

Kishore Nallane45f18785f14
diff --git a/include/field.h b/include/field.h
index 8d9cbf83..e7200da8 100644
--- a/include/field.h
+++ b/include/field.h
@@ -5,6 +5,7 @@
#include "art.h"
#include "option.h"
#include "string_utils.h"
+#include "logger.h"
#include "json.hpp"
namespace field_types {
@@ -192,6 +193,10 @@ struct field {
bool found_default_sorting_field = false;
for(const field & field: fields) {
+ if(field.name == "id") {
+ continue;
+ }
+
nlohmann::json field_val;
field_val[fields::name] = field.name;
field_val[fields::type] = field.type;
@@ -263,6 +268,13 @@ struct field {
size_t num_auto_detect_fields = 0;
for(nlohmann::json & field_json: fields_json) {
+ if(field_json["name"] == "id") {
+ // No field should exist with the name "id" as it is reserved for internal use
+ // We cannot throw an error here anymore since that will break backward compatibility!
+ LOG(WARNING) << "Collection schema cannot contain a field with name `id`. Ignoring field.";
+ continue;
+ }
+
if(!field_json.is_object() ||
field_json.count(fields::name) == 0 || field_json.count(fields::type) == 0 ||
!field_json.at(fields::name).is_string() || !field_json.at(fields::type).is_string()) {
diff --git a/test/collection_specific_test.cpp b/test/collection_specific_test.cpp
index d316b230..f36be260 100644
--- a/test/collection_specific_test.cpp
+++ b/test/collection_specific_test.cpp
@@ -652,3 +652,27 @@ TEST_F(CollectionSpecificTest, HighlightSecondaryFieldWithPrefixMatch) {
collectionManager.drop_collection("coll1");
}
+
+TEST_F(CollectionSpecificTest, GuardAgainstIdFieldInSchema) {
+ // The "id" field, if defined in the schema should be ignored
+
+ std::vector<field> fields = {field("title", field_types::STRING, false),
+ field("id", field_types::STRING, false),
+ field("points", field_types::INT32, false),};
+
+ nlohmann::json schema;
+ schema["name"] = "books";
+ schema["fields"] = nlohmann::json::array();
+ schema["fields"][0]["name"] = "title";
+ schema["fields"][0]["type"] = "string";
+ schema["fields"][1]["name"] = "id";
+ schema["fields"][1]["type"] = "string";
+ schema["fields"][2]["name"] = "points";
+ schema["fields"][2]["type"] = "int32";
+
+ Collection* coll1 = collectionManager.create_collection(schema).get();
+
+ ASSERT_EQ(0, coll1->get_schema().count("id"));
+
+ collectionManager.drop_collection("coll1");
+}