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

Return metadata in collection summary response.

Kishore Nallan14338ce49147
diff --git a/include/collection.h b/include/collection.h
index 067cf84e..a04fe202 100644
--- a/include/collection.h
+++ b/include/collection.h
@@ -155,6 +155,10 @@ private:
// Keep index as the last field since it is initialized in the constructor via init_index(). Add a new field before it.
Index* index;
+ std::shared_ptr<VQModel> vq_model = nullptr;
+
+ nlohmann::json metadata;
+
// methods
std::string get_doc_id_key(const std::string & doc_id) const;
@@ -343,8 +347,6 @@ private:
const size_t remote_embedding_num_tries,
size_t& per_page) const;
- std::shared_ptr<VQModel> vq_model = nullptr;
-
public:
enum {MAX_ARRAY_MATCHES = 5};
@@ -383,7 +385,8 @@ public:
const float max_memory_ratio, const std::string& fallback_field_type,
const std::vector<std::string>& symbols_to_index, const std::vector<std::string>& token_separators,
const bool enable_nested_fields, std::shared_ptr<VQModel> vq_model = nullptr,
- spp::sparse_hash_map<std::string, std::string> referenced_in = spp::sparse_hash_map<std::string, std::string>());
+ spp::sparse_hash_map<std::string, std::string> referenced_in = spp::sparse_hash_map<std::string, std::string>(),
+ const nlohmann::json& metadata = {});
~Collection();
diff --git a/src/collection.cpp b/src/collection.cpp
index 3473f00b..5e5ccf44 100644
--- a/src/collection.cpp
+++ b/src/collection.cpp
@@ -53,7 +53,8 @@ Collection::Collection(const std::string& name, const uint32_t collection_id, co
const std::vector<std::string>& symbols_to_index,
const std::vector<std::string>& token_separators,
const bool enable_nested_fields, std::shared_ptr<VQModel> vq_model,
- spp::sparse_hash_map<std::string, std::string> referenced_in) :
+ spp::sparse_hash_map<std::string, std::string> referenced_in,
+ const nlohmann::json& metadata) :
name(name), collection_id(collection_id), created_at(created_at),
next_seq_id(next_seq_id), store(store),
fields(fields), default_sorting_field(default_sorting_field), enable_nested_fields(enable_nested_fields),
@@ -61,7 +62,8 @@ Collection::Collection(const std::string& name, const uint32_t collection_id, co
fallback_field_type(fallback_field_type), dynamic_fields({}),
symbols_to_index(to_char_array(symbols_to_index)), token_separators(to_char_array(token_separators)),
index(init_index()), vq_model(vq_model),
- referenced_in(std::move(referenced_in)) {
+ referenced_in(std::move(referenced_in)),
+ metadata(metadata) {
if (vq_model) {
vq_model->inc_collection_ref_count();
@@ -509,7 +511,10 @@ nlohmann::json Collection::get_summary_json() const {
json_response["fields"] = fields_arr;
json_response["default_sorting_field"] = default_sorting_field;
-
+ if(!metadata.empty()) {
+ json_response["metadata"] = metadata;
+ }
+
if(vq_model) {
json_response["voice_query_model"] = nlohmann::json::object();
json_response["voice_query_model"]["model_name"] = vq_model->get_model_name();
diff --git a/src/collection_manager.cpp b/src/collection_manager.cpp
index f9d70d14..655d37a8 100644
--- a/src/collection_manager.cpp
+++ b/src/collection_manager.cpp
@@ -183,6 +183,12 @@ Collection* CollectionManager::init_collection(const nlohmann::json & collection
}
}
+ nlohmann::json metadata;
+
+ if(collection_meta.count(Collection::COLLECTION_METADATA) != 0) {
+ metadata = collection_meta[Collection::COLLECTION_METADATA];
+ }
+
Collection* collection = new Collection(this_collection_name,
collection_meta[Collection::COLLECTION_ID_KEY].get<uint32_t>(),
created_at,
@@ -194,7 +200,8 @@ Collection* CollectionManager::init_collection(const nlohmann::json & collection
fallback_field_type,
symbols_to_index,
token_separators,
- enable_nested_fields, model, std::move(referenced_in));
+ enable_nested_fields, model, std::move(referenced_in),
+ metadata);
return collection;
}
@@ -540,7 +547,9 @@ Option<Collection*> CollectionManager::create_collection(const std::string& name
default_sorting_field,
this->max_memory_ratio, fallback_field_type,
symbols_to_index, token_separators,
- enable_nested_fields, model);
+ enable_nested_fields, model,
+ spp::sparse_hash_map<std::string, std::string>(),
+ metadata);
add_to_collections(new_collection);
diff --git a/test/collection_manager_test.cpp b/test/collection_manager_test.cpp
index 704e7123..d01e5dfd 100644
--- a/test/collection_manager_test.cpp
+++ b/test/collection_manager_test.cpp
@@ -1830,7 +1830,6 @@ TEST_F(CollectionManagerTest, CollectionCreationWithMetadata) {
ASSERT_TRUE(op.ok());
Collection* coll1 = op.get();
-
std::string collection_meta_json;
nlohmann::json collection_meta;
std::string next_seq_id;
@@ -1915,6 +1914,24 @@ TEST_F(CollectionManagerTest, CollectionCreationWithMetadata) {
expected_meta_json["created_at"] = actual_json["created_at"];
ASSERT_EQ(expected_meta_json.dump(), actual_json.dump());
+
+ // metadata should exist as part of collection summary
+ auto coll_summary = coll1->get_summary_json();
+ ASSERT_EQ(expected_meta_json["metadata"].dump(), coll_summary["metadata"].dump());
+
+ // if no metadata is given, the key should not be present in response
+ schema2 = R"({
+ "name": "coll2",
+ "enable_nested_fields": true,
+ "fields": [
+ {"name": "value.color", "type": "string"}
+ ]
+ })"_json;
+
+ op = collectionManager.create_collection(schema2);
+ ASSERT_TRUE(op.ok());
+ Collection* coll2 = op.get();
+ ASSERT_EQ(0, coll2->get_summary_json().count("metadata"));
}
TEST_F(CollectionManagerTest, PopulateReferencedIns) {