diff --git a/TODO.md b/TODO.md index ed388415..a42d300b 100644 --- a/TODO.md +++ b/TODO.md @@ -76,7 +76,7 @@ - ~~Validate before string to int conversion in the http api layer~~ - ~~art bool support~~ - ~~Export collection~~ -- get collection should show schema +- ~~get collection should show schema~~ - handle hyphens (replace them) - clean special chars before indexing - NOT operator support diff --git a/include/collection.h b/include/collection.h index 879eab8c..f472df08 100644 --- a/include/collection.h +++ b/include/collection.h @@ -33,6 +33,8 @@ private: // Auto incrementing record ID used internally for indexing - not exposed to the client uint32_t next_seq_id; + std::vector fields; + spp::sparse_hash_map search_schema; spp::sparse_hash_map facet_schema; @@ -83,6 +85,8 @@ public: std::vector get_sort_fields(); + std::vector get_fields(); + spp::sparse_hash_map get_schema(); std::string get_token_ranking_field(); diff --git a/src/api.cpp b/src/api.cpp index a2fab3b5..2f667b35 100644 --- a/src/api.cpp +++ b/src/api.cpp @@ -71,8 +71,13 @@ void post_create_collection(http_req & req, http_res & res) { 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()) { - return res.send_400("Wrong format for `fields`. It should be an array like: " - "[{\"name\": \"\", \"type\": \"\"}]"); + return res.send_400("Wrong format for `fields`. It should be an array of objects containing " + "`name`, `type` and optionally, `facet` properties."); + } + + if(field_json.count("facet") != 0 && !field_json.at(fields::facet).is_boolean()) { + return res.send_400(std::string("The `facet` property of the field `") + + field_json.at(fields::name).get() + "` should be a boolean."); } if(field_json.count("facet") == 0) { @@ -256,6 +261,21 @@ void get_collection_summary(http_req & req, http_res & res) { json_response["name"] = collection->get_name(); json_response["num_documents"] = collection->get_num_documents(); + + const std::vector & coll_fields = collection->get_fields(); + nlohmann::json fields_arr; + + for(const field & coll_field: coll_fields) { + nlohmann::json field_json; + field_json[fields::name] = coll_field.name; + field_json[fields::type] = coll_field.type; + field_json[fields::facet] = coll_field.facet; + fields_arr.push_back(field_json); + } + + json_response["fields"] = fields_arr; + json_response["prefix_sort_field"] = collection->get_token_ranking_field(); + res.send_200(json_response.dump()); } diff --git a/src/collection.cpp b/src/collection.cpp index 19e2e3f3..8eabb432 100644 --- a/src/collection.cpp +++ b/src/collection.cpp @@ -10,7 +10,7 @@ Collection::Collection(const std::string name, const uint32_t collection_id, const uint32_t next_seq_id, Store *store, const std::vector &fields, const std::string & token_ranking_field): name(name), collection_id(collection_id), next_seq_id(next_seq_id), store(store), - token_ranking_field(token_ranking_field) { + fields(fields), token_ranking_field(token_ranking_field) { for(const field& field: fields) { search_schema.emplace(field.name, field); @@ -735,6 +735,10 @@ std::vector Collection::get_sort_fields() { return sort_fields_copy; } +std::vector Collection::get_fields() { + return fields; +} + spp::sparse_hash_map Collection::get_schema() { return search_schema; };