Collection summary end-point now shows the schema.

This commit is contained in:
Kishore Nallan 2017-12-16 18:43:08 +05:30
parent f3e630f9de
commit bae67169d6
4 changed files with 32 additions and 4 deletions

View File

@ -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

View File

@ -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<field> fields;
spp::sparse_hash_map<std::string, field> search_schema;
spp::sparse_hash_map<std::string, field> facet_schema;
@ -83,6 +85,8 @@ public:
std::vector<field> get_sort_fields();
std::vector<field> get_fields();
spp::sparse_hash_map<std::string, field> get_schema();
std::string get_token_ranking_field();

View File

@ -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\": \"<field_name>\", \"type\": \"<field_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<std::string>() + "` 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<field> & 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());
}

View File

@ -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<field> &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<field> Collection::get_sort_fields() {
return sort_fields_copy;
}
std::vector<field> Collection::get_fields() {
return fields;
}
spp::sparse_hash_map<std::string, field> Collection::get_schema() {
return search_schema;
};