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

Handle optional field for legacy records.

kishorenceef3a5a3def0
diff --git a/include/field.h b/include/field.h
index 30ef142d..04f0ad49 100644
--- a/include/field.h
+++ b/include/field.h
@@ -22,7 +22,7 @@ namespace fields {
static const std::string name = "name";
static const std::string type = "type";
static const std::string facet = "facet";
- static const std::string optional = "facet";
+ static const std::string optional = "optional";
}
struct field {
diff --git a/src/collection_manager.cpp b/src/collection_manager.cpp
index 8c070dc0..dbc80236 100644
--- a/src/collection_manager.cpp
+++ b/src/collection_manager.cpp
@@ -16,8 +16,15 @@ Collection* CollectionManager::init_collection(const nlohmann::json & collection
nlohmann::json fields_map = collection_meta[COLLECTION_SEARCH_FIELDS_KEY];
for (nlohmann::json::iterator it = fields_map.begin(); it != fields_map.end(); ++it) {
- fields.push_back({it.value()[fields::name], it.value()[fields::type],
- it.value()[fields::facet], it.value()[fields::optional]});
+ nlohmann::json & field_obj = it.value();
+
+ // handle older records indexed before optional field introduction
+ if(field_obj.count(fields::optional) == 0) {
+ field_obj[fields::optional] = false;
+ }
+
+ fields.push_back({field_obj[fields::name], field_obj[fields::type],
+ field_obj[fields::facet], field_obj[fields::optional]});
}
std::string default_sorting_field = collection_meta[COLLECTION_DEFAULT_SORTING_FIELD_KEY].get<std::string>();
diff --git a/test/collection_manager_test.cpp b/test/collection_manager_test.cpp
index 6408c3e7..cc663c59 100644
--- a/test/collection_manager_test.cpp
+++ b/test/collection_manager_test.cpp
@@ -25,7 +25,7 @@ protected:
search_fields = {
field("title", field_types::STRING, false),
field("starring", field_types::STRING, false),
- field("cast", field_types::STRING_ARRAY, true),
+ field("cast", field_types::STRING_ARRAY, true, true),
field("points", field_types::INT32, false)
};
@@ -81,10 +81,10 @@ TEST_F(CollectionManagerTest, CollectionCreation) {
// we already call `collection1->get_next_seq_id` above, which is side-effecting
ASSERT_EQ(1, StringUtils::deserialize_uint32_t(next_seq_id));
ASSERT_EQ("{\"created_at\":12345,\"default_sorting_field\":\"points\","
- "\"fields\":[{\"facet\":false,\"name\":\"title\",\"type\":\"string\"},"
- "{\"facet\":false,\"name\":\"starring\",\"type\":\"string\"},"
- "{\"facet\":true,\"name\":\"cast\",\"type\":\"string[]\"},"
- "{\"facet\":false,\"name\":\"points\",\"type\":\"int32\"}],\"id\":0,\"name\":\"collection1\"}",
+ "\"fields\":[{\"facet\":false,\"name\":\"title\",\"optional\":false,\"type\":\"string\"},"
+ "{\"facet\":false,\"name\":\"starring\",\"optional\":false,\"type\":\"string\"},"
+ "{\"facet\":true,\"name\":\"cast\",\"optional\":true,\"type\":\"string[]\"},"
+ "{\"facet\":false,\"name\":\"points\",\"optional\":false,\"type\":\"int32\"}],\"id\":0,\"name\":\"collection1\"}",
collection_meta_json);
ASSERT_EQ("1", next_collection_id);
}
@@ -225,6 +225,12 @@ TEST_F(CollectionManagerTest, RestoreRecordsOnRestart) {
ASSERT_EQ(schema.size(), collection1->get_schema().size());
ASSERT_EQ("points", collection1->get_default_sorting_field());
+ auto restored_schema = collection1->get_schema();
+ ASSERT_EQ(true, restored_schema.at("cast").optional);
+ ASSERT_EQ(true, restored_schema.at("cast").facet);
+ ASSERT_EQ(false, restored_schema.at("title").facet);
+ ASSERT_EQ(false, restored_schema.at("title").optional);
+
ASSERT_EQ(2, collection1->get_overrides().size());
ASSERT_STREQ("exclude-rule", collection1->get_overrides()["exclude-rule"].id.c_str());
ASSERT_STREQ("include-rule", collection1->get_overrides()["include-rule"].id.c_str());