mirror of
https://github.com/typesense/typesense.git
synced 2025-05-22 14:55:26 +08:00
Merge branch 'master' into distinct-grouping
# Conflicts: # test/collection_test.cpp
This commit is contained in:
commit
444633ce85
@ -217,6 +217,8 @@ public:
|
||||
|
||||
Option<uint32_t> to_doc(const std::string & json_str, nlohmann::json & document);
|
||||
|
||||
nlohmann::json get_summary_json();
|
||||
|
||||
Option<nlohmann::json> add(const std::string & json_str);
|
||||
|
||||
Option<nlohmann::json> add_many(const std::string & json_str);
|
||||
|
@ -127,6 +127,29 @@ Option<uint32_t> Collection::to_doc(const std::string & json_str, nlohmann::json
|
||||
return Option<uint32_t>(seq_id);
|
||||
}
|
||||
|
||||
nlohmann::json Collection::get_summary_json() {
|
||||
nlohmann::json json_response;
|
||||
|
||||
json_response["name"] = name;
|
||||
json_response["num_documents"] = num_documents;
|
||||
json_response["created_at"] = created_at;
|
||||
|
||||
nlohmann::json fields_arr;
|
||||
|
||||
for(const field & coll_field: 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;
|
||||
field_json[fields::optional] = coll_field.optional;
|
||||
fields_arr.push_back(field_json);
|
||||
}
|
||||
|
||||
json_response["fields"] = fields_arr;
|
||||
json_response["default_sorting_field"] = default_sorting_field;
|
||||
return json_response;
|
||||
}
|
||||
|
||||
Option<nlohmann::json> Collection::add(const std::string & json_str) {
|
||||
nlohmann::json document;
|
||||
Option<uint32_t> doc_seq_id_op = to_doc(json_str, document);
|
||||
|
@ -9,29 +9,6 @@
|
||||
#include "system_metrics.h"
|
||||
#include "logger.h"
|
||||
|
||||
nlohmann::json collection_summary_json(Collection *collection) {
|
||||
nlohmann::json json_response;
|
||||
|
||||
json_response["name"] = collection->get_name();
|
||||
json_response["num_documents"] = collection->get_num_documents();
|
||||
json_response["created_at"] = collection->get_created_at();
|
||||
|
||||
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["default_sorting_field"] = collection->get_default_sorting_field();
|
||||
return json_response;
|
||||
}
|
||||
|
||||
bool handle_authentication(http_req& req, const route_path& rpath, const std::string& auth_key) {
|
||||
CollectionManager & collectionManager = CollectionManager::get_instance();
|
||||
|
||||
@ -55,7 +32,7 @@ bool get_collections(http_req & req, http_res & res) {
|
||||
nlohmann::json json_response = nlohmann::json::array();
|
||||
|
||||
for(Collection* collection: collections) {
|
||||
nlohmann::json collection_json = collection_summary_json(collection);
|
||||
nlohmann::json collection_json = collection->get_summary_json();
|
||||
json_response.push_back(collection_json);
|
||||
}
|
||||
|
||||
@ -150,7 +127,7 @@ bool post_create_collection(http_req & req, http_res & res) {
|
||||
collectionManager.create_collection(req_json["name"], fields, default_sorting_field);
|
||||
|
||||
if(collection_op.ok()) {
|
||||
nlohmann::json json_response = collection_summary_json(collection_op.get());
|
||||
nlohmann::json json_response = collection_op.get()->get_summary_json();
|
||||
res.set_201(json_response.dump());
|
||||
return true;
|
||||
}
|
||||
@ -169,7 +146,7 @@ bool del_drop_collection(http_req & req, http_res & res) {
|
||||
return false;
|
||||
}
|
||||
|
||||
nlohmann::json collection_json = collection_summary_json(collection);
|
||||
nlohmann::json collection_json = collection->get_summary_json();
|
||||
Option<bool> drop_result = collectionManager.drop_collection(req.params["collection"]);
|
||||
|
||||
if(!drop_result.ok()) {
|
||||
@ -522,7 +499,7 @@ bool get_collection_summary(http_req & req, http_res & res) {
|
||||
return false;
|
||||
}
|
||||
|
||||
nlohmann::json json_response = collection_summary_json(collection);
|
||||
nlohmann::json json_response = collection->get_summary_json();
|
||||
res.set_200(json_response.dump());
|
||||
|
||||
return true;
|
||||
|
@ -295,7 +295,8 @@ Option<uint32_t> Index::validate_index_in_memory(const nlohmann::json &document,
|
||||
return Option<>(400, "Field `" + field_name + "` must be a float array.");
|
||||
}
|
||||
|
||||
if(document[field_name].size() > 0 && !document[field_name][0].is_number_float()) {
|
||||
if(document[field_name].size() > 0 && !document[field_name][0].is_number()) {
|
||||
// allows integer to be passed to a float array field
|
||||
return Option<>(400, "Field `" + field_name + "` must be a float array.");
|
||||
}
|
||||
} else if(field_pair.second.type == field_types::BOOL_ARRAY) {
|
||||
|
@ -1122,7 +1122,8 @@ TEST_F(CollectionTest, FilterOnFloatFields) {
|
||||
std::string json_line;
|
||||
|
||||
while (std::getline(infile, json_line)) {
|
||||
coll_array_fields->add(json_line);
|
||||
auto add_op = coll_array_fields->add(json_line);
|
||||
ASSERT_TRUE(add_op.ok());
|
||||
}
|
||||
|
||||
infile.close();
|
||||
@ -2256,6 +2257,19 @@ TEST_F(CollectionTest, OptionalFields) {
|
||||
Option<std::string> remove_op = coll1->remove("1");
|
||||
ASSERT_TRUE(remove_op.ok());
|
||||
|
||||
// try fetching the schema (should contain optional field)
|
||||
nlohmann::json coll_summary = coll1->get_summary_json();
|
||||
LOG(INFO) << coll_summary;
|
||||
ASSERT_STREQ("title", coll_summary["fields"][0]["name"].get<std::string>().c_str());
|
||||
ASSERT_STREQ("string", coll_summary["fields"][0]["type"].get<std::string>().c_str());
|
||||
ASSERT_FALSE(coll_summary["fields"][0]["facet"].get<bool>());
|
||||
ASSERT_FALSE(coll_summary["fields"][0]["optional"].get<bool>());
|
||||
|
||||
ASSERT_STREQ("description", coll_summary["fields"][1]["name"].get<std::string>().c_str());
|
||||
ASSERT_STREQ("string", coll_summary["fields"][1]["type"].get<std::string>().c_str());
|
||||
ASSERT_TRUE(coll_summary["fields"][1]["facet"].get<bool>());
|
||||
ASSERT_TRUE(coll_summary["fields"][1]["optional"].get<bool>());
|
||||
|
||||
// default sorting field should not be declared optional
|
||||
fields = {
|
||||
field("title", field_types::STRING, false),
|
||||
|
@ -1,5 +1,5 @@
|
||||
{"name": "Jeremy Howard", "top_3": [1.09, 1.88, 0.001], "rating": 1.09, "age": 24, "years": [2014, 2015, 2016], "timestamps": [1390354022, 1421890022, 1453426022], "tags": ["gold", "silver"]}
|
||||
{"name": "Jeremy Howard", "top_3": [9.999, 8.89, 7.713], "rating": 9.999, "age": 44, "years": [2015, 2016], "timestamps": [1421890022, 1453426022], "tags": ["FINE PLATINUM"]}
|
||||
{"name": "Jeremy Howard", "top_3": [7.812, 7.770, 6.66], "rating": 7.812, "age": 21, "years": [2016], "timestamps": [1453426022], "tags": ["bronze", "gold"]}
|
||||
{"name": "Jeremy Howard", "top_3": [0.0, 0.0, 0.0], "rating": 0.0, "age": 63, "years": [1981, 1985], "timestamps": [348974822, 475205222], "tags": ["silver"]}
|
||||
{"name": "Jeremy Howard", "top_3": [0, 0.0, 0.0], "rating": 0.0, "age": 63, "years": [1981, 1985], "timestamps": [348974822, 475205222], "tags": ["silver"]}
|
||||
{"name": "Jeremy Howard", "top_3": [5.5, 5.431, 1.001], "rating": 5.5, "age": 32, "years": [1999, 2000, 2001, 2002], "timestamps": [916968422, 948504422, 980126822, 1011662822], "tags": ["silver", "gold", "bronze"]}
|
Loading…
x
Reference in New Issue
Block a user