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

Make fields in schema completely optional.

kishorenc1618007cd812
diff --git a/src/core_api.cpp b/src/core_api.cpp
index 34650c25..d179609a 100644
--- a/src/core_api.cpp
+++ b/src/core_api.cpp
@@ -142,9 +142,13 @@ bool post_create_collection(http_req & req, http_res & res) {
std::vector<field> fields;
- if(!req_json["fields"].is_array() || req_json["fields"].size() == 0) {
- res.set_400("Wrong format for `fields`. It should be an array like: "
- "[{\"name\": \"<field_name>\", \"type\": \"<field_type>\"}]");
+ if(req_json.count("fields") == 0) {
+ req_json["fields"] = nlohmann::json::array();
+ }
+
+ if(!req_json["fields"].is_array()) {
+ res.set_400("Wrong format for `fields`. It should be an array of objects containing "
+ "`name`, `type` and optionally, `facet` properties.");
return false;
}
diff --git a/src/index.cpp b/src/index.cpp
index 98205b0e..ea78907f 100644
--- a/src/index.cpp
+++ b/src/index.cpp
@@ -2360,6 +2360,7 @@ void Index::refresh_schemas(const std::vector<field>& new_fields) {
for(const auto & new_field: new_fields) {
search_schema.emplace(new_field.name, new_field);
+ sort_schema.emplace(new_field.name, new_field);
if(search_index.count(new_field.name) == 0) {
if(new_field.is_string()) {
diff --git a/test/collection_all_fields_test.cpp b/test/collection_all_fields_test.cpp
index a5e34765..664ef652 100644
--- a/test/collection_all_fields_test.cpp
+++ b/test/collection_all_fields_test.cpp
@@ -38,15 +38,13 @@ TEST_F(CollectionAllFieldsTest, IndexDocsWithoutSchema) {
Collection *coll1;
std::ifstream infile(std::string(ROOT_DIR)+"test/multi_field_documents.jsonl");
- std::vector<field> fields = {
- field("points", field_types::INT32, false)
- };
+ std::vector<field> fields = {};
std::vector<sort_by> sort_fields = { sort_by("points", "DESC") };
coll1 = collectionManager.get_collection("coll1").get();
if(coll1 == nullptr) {
- coll1 = collectionManager.create_collection("coll1", 1, fields, "points", 0, true).get();
+ coll1 = collectionManager.create_collection("coll1", 1, fields, "", 0, true).get();
}
std::string json_line;
@@ -138,5 +136,11 @@ TEST_F(CollectionAllFieldsTest, IndexDocsWithoutSchema) {
ASSERT_FALSE(add_op.ok());
ASSERT_STREQ("Field `title` must be a string.", add_op.error().c_str());
+ // try querying using an non-existing sort field
+ sort_fields = { sort_by("not-found", "DESC") };
+ auto res_op = coll1->search("*", {}, "", {}, sort_fields, 0, 10, 1, FREQUENCY, false);
+ ASSERT_FALSE(res_op.ok());
+ ASSERT_EQ("Could not find a field named `not-found` in the schema for sorting.", res_op.error());
+
collectionManager.drop_collection("coll1");
}