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 ability to exclude fields in collection listing.

Kishore Nallan665905460826
diff --git a/include/collection_manager.h b/include/collection_manager.h
index 4ecd5f5f..4d988dbf 100644
--- a/include/collection_manager.h
+++ b/include/collection_manager.h
@@ -181,6 +181,7 @@ public:
locked_resource_view_t<Collection> get_collection_with_id(uint32_t collection_id) const;
Option<nlohmann::json> get_collection_summaries(uint32_t limit = 0 , uint32_t offset = 0,
+ const std::vector<std::string>& exclude_fields = {},
const std::vector<std::string>& api_key_collections = {}) const;
Option<nlohmann::json> drop_collection(const std::string& collection_name,
diff --git a/src/collection_manager.cpp b/src/collection_manager.cpp
index 262f8e01..798a1f2d 100644
--- a/src/collection_manager.cpp
+++ b/src/collection_manager.cpp
@@ -2095,6 +2095,7 @@ ThreadPool* CollectionManager::get_thread_pool() const {
}
Option<nlohmann::json> CollectionManager::get_collection_summaries(uint32_t limit, uint32_t offset,
+ const std::vector<std::string>& exclude_fields,
const std::vector<std::string>& api_key_collections) const {
std::shared_lock lock(mutex);
@@ -2109,6 +2110,10 @@ Option<nlohmann::json> CollectionManager::get_collection_summaries(uint32_t limi
for(Collection* collection: colls) {
nlohmann::json collection_json = collection->get_summary_json();
+ for(const auto& exclude_field: exclude_fields) {
+ collection_json.erase(exclude_field);
+ }
+
json_summaries.push_back(collection_json);
}
diff --git a/src/core_api.cpp b/src/core_api.cpp
index 4331f4b9..c6d59d26 100644
--- a/src/core_api.cpp
+++ b/src/core_api.cpp
@@ -235,6 +235,8 @@ bool get_collections(const std::shared_ptr<http_req>& req, const std::shared_ptr
CollectionManager & collectionManager = CollectionManager::get_instance();
uint32_t offset = 0, limit = 0;
+ std::vector<std::string> exclude_fields;
+
if(req->params.count("offset") != 0) {
const auto &offset_str = req->params["offset"];
if(!StringUtils::is_uint32_t(offset_str)) {
@@ -253,10 +255,16 @@ bool get_collections(const std::shared_ptr<http_req>& req, const std::shared_ptr
limit = std::stoi(limit_str);
}
+ if(req->params.count("exclude_fields") != 0) {
+ const auto& exclude_fields_str = req->params["exclude_fields"];
+ StringUtils::split(exclude_fields_str, exclude_fields, ",");
+ }
+
AuthManager &auth_manager = collectionManager.getAuthManager();
auto api_key_collections = auth_manager.get_api_key_collections(req->api_auth_key);
- auto collections_summaries_op = collectionManager.get_collection_summaries(limit, offset, api_key_collections);
+ auto collections_summaries_op = collectionManager.get_collection_summaries(limit, offset,
+ api_key_collections);
if(!collections_summaries_op.ok()) {
res->set(collections_summaries_op.code(), collections_summaries_op.error());
return false;
diff --git a/test/collection_manager_test.cpp b/test/collection_manager_test.cpp
index 1f659175..5691f1c6 100644
--- a/test/collection_manager_test.cpp
+++ b/test/collection_manager_test.cpp
@@ -1877,6 +1877,36 @@ TEST_F(CollectionManagerTest, ReferencedInBacklog) {
ASSERT_EQ("Could not find any field in `Products` referencing the collection `foo`.", get_reference_field_op.error());
}
+TEST_F(CollectionManagerTest, ExcludeFieldsInCollectionListing) {
+ auto schema_json =
+ R"({
+ "name": "products",
+ "fields": [
+ {"name": "product_id", "type": "string"},
+ {"name": "name", "type": "string"},
+ {"name": "points", "type": "int32"}
+ ],
+ "default_sorting_field": "points"
+ })"_json;
+
+ auto create_op = collectionManager.create_collection(schema_json);
+ ASSERT_TRUE(create_op.ok());
+
+ nlohmann::json coll_json_summaries = collectionManager.get_collection_summaries(10, 0, {"fields"}).get();
+ ASSERT_EQ(2, coll_json_summaries.size());
+
+ for(auto coll_json: coll_json_summaries) {
+ ASSERT_FALSE(coll_json.contains("fields"));
+ }
+
+ coll_json_summaries = collectionManager.get_collection_summaries(10, 0, {}).get();
+ ASSERT_EQ(2, coll_json_summaries.size());
+
+ for(auto coll_json: coll_json_summaries) {
+ ASSERT_TRUE(coll_json.contains("fields"));
+ }
+}
+
TEST_F(CollectionManagerTest, CollectionCreationWithMetadata) {
CollectionManager & collectionManager3 = CollectionManager::get_instance();