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

Review Changes

ozanarmagan4f728c98fe8b
diff --git a/include/field.h b/include/field.h
index bc4ae03c..9aae16c8 100644
--- a/include/field.h
+++ b/include/field.h
@@ -9,6 +9,7 @@
#include <sparsepp.h>
#include <tsl/htrie_map.h>
#include "json.hpp"
+#include "text_embedder_manager.h"
namespace field_types {
// first field value indexed will determine the type
@@ -317,9 +318,9 @@ struct field {
if (!field.reference.empty()) {
field_val[fields::reference] = field.reference;
}
- if(field.create_from.size() > 0) {
+ if(!field.create_from.empty()) {
field_val[fields::create_from] = field.create_from;
- if(field.model_path.size() > 0) {
+ if(!field.model_path.empty()) {
field_val[fields::model_path] = field.model_path;
}
}
@@ -408,7 +409,7 @@ struct field {
static Option<bool> json_field_to_field(bool enable_nested_fields, nlohmann::json& field_json,
std::vector<field>& the_fields,
- string& fallback_field_type, size_t& num_auto_detect_fields,const nlohmann::json& all_fields_json = nlohmann::json());
+ string& fallback_field_type, size_t& num_auto_detect_fields);
static Option<bool> json_fields_to_fields(bool enable_nested_fields,
nlohmann::json& fields_json,
@@ -418,8 +419,61 @@ struct field {
size_t num_auto_detect_fields = 0;
for(nlohmann::json & field_json: fields_json) {
+
+ if(field_json.count(fields::create_from) != 0) {
+ if(TextEmbedderManager::model_dir.empty()) {
+ return Option<bool>(400, "Text embedding is not enabled. Please set `model-dir` at startup.");
+ }
+
+ if(!field_json[fields::create_from].is_array()) {
+ return Option<bool>(400, "Property `" + fields::create_from + "` must be an array.");
+ }
+
+ if(field_json[fields::create_from].empty()) {
+ return Option<bool>(400, "Property `" + fields::create_from + "` must have at least one element.");
+ }
+
+ for(auto& create_from_field : field_json[fields::create_from]) {
+ if(!create_from_field.is_string()) {
+ return Option<bool>(400, "Property `" + fields::create_from + "` must be an array of strings.");
+ }
+ }
+
+ if(field_json[fields::type] != field_types::FLOAT_ARRAY) {
+ return Option<bool>(400, "Property `" + fields::create_from + "` is only allowed on a float array field.");
+ }
+
+
+ for(auto& create_from_field : field_json[fields::create_from]) {
+ bool flag = false;
+ for(const auto& field : fields_json) {
+ if(field[fields::name] == create_from_field) {
+ if(field[fields::type] != field_types::STRING) {
+ return Option<bool>(400, "Property `" + fields::create_from + "` can only be used with array of string fields.");
+ }
+ flag = true;
+ break;
+ }
+ }
+ if(!flag) {
+ for(const auto& field : the_fields) {
+ if(field.name == create_from_field) {
+ if(field.type != field_types::STRING) {
+ return Option<bool>(400, "Property `" + fields::create_from + "` can only be used with array of string fields.");
+ }
+ flag = true;
+ break;
+ }
+ }
+ }
+ if(!flag) {
+ return Option<bool>(400, "Property `" + fields::create_from + "` can only be used with array of string fields.");
+ }
+ }
+ }
+
auto op = json_field_to_field(enable_nested_fields,
- field_json, the_fields, fallback_field_type, num_auto_detect_fields, fields_json);
+ field_json, the_fields, fallback_field_type, num_auto_detect_fields);
if(!op.ok()) {
return op;
}
diff --git a/src/field.cpp b/src/field.cpp
index e73ee5bd..2971e257 100644
--- a/src/field.cpp
+++ b/src/field.cpp
@@ -503,7 +503,7 @@ Option<bool> filter::parse_filter_query(const std::string& filter_query,
Option<bool> field::json_field_to_field(bool enable_nested_fields, nlohmann::json& field_json,
std::vector<field>& the_fields,
- string& fallback_field_type, size_t& num_auto_detect_fields, const nlohmann::json& all_fields_json) {
+ string& fallback_field_type, size_t& num_auto_detect_fields) {
if(field_json["name"] == "id") {
// No field should exist with the name "id" as it is reserved for internal use
@@ -668,58 +668,6 @@ Option<bool> field::json_field_to_field(bool enable_nested_fields, nlohmann::jso
}
if(field_json.count(fields::create_from) != 0) {
- if(TextEmbedderManager::model_dir.size() == 0) {
- return Option<bool>(400, "Text embedding is not enabled. Please set `model-dir` at startup.");
- }
-
- if(!field_json[fields::create_from].is_array()) {
- return Option<bool>(400, "Property `" + fields::create_from + "` must be an array.");
- }
-
- if(field_json[fields::create_from].size() == 0) {
- return Option<bool>(400, "Property `" + fields::create_from + "` must have at least one element.");
- }
-
- for(auto& create_from_field : field_json[fields::create_from]) {
- if(!create_from_field.is_string()) {
- return Option<bool>(400, "Property `" + fields::create_from + "` must be an array of strings.");
- }
- }
-
- if(field_json[fields::type] != field_types::FLOAT_ARRAY) {
- return Option<bool>(400, "Property `" + fields::create_from + "` is only allowed on a float array field.");
- }
-
-
- std::unordered_set<std::string> found_fields;
- for(auto& create_from_field : field_json[fields::create_from]) {
- bool flag = false;
- for(const auto& field : all_fields_json) {
- if(field[fields::name] == create_from_field) {
- if(field[fields::type] != field_types::STRING) {
- return Option<bool>(400, "Property `" + fields::create_from + "` can only be used with array of string fields.");
- }
- found_fields.insert(create_from_field.get<std::string>());
- flag = true;
- break;
- }
- }
- if(!flag) {
- for(const auto& field : the_fields) {
- if(field.name == create_from_field) {
- if(field.type != field_types::STRING) {
- return Option<bool>(400, "Property `" + fields::create_from + "` can only be used with array of string fields.");
- }
- found_fields.insert(create_from_field.get<std::string>());
- break;
- }
- }
- }
- }
- if(found_fields.size() != field_json[fields::create_from].size()) {
- return Option<bool>(400, "Property `" + fields::create_from + "` must be an array of existing string fields.");
- }
-
// If the model path is not specified, use the default model and set the number of dimensions to 384 (number of dimensions of the default model)
field_json[fields::num_dim] = static_cast<unsigned int>(384);
if(!field_json[fields::model_path].is_null()) {
diff --git a/src/http_client.cpp b/src/http_client.cpp
index 4bf9f397..a06b8aea 100644
--- a/src/http_client.cpp
+++ b/src/http_client.cpp
@@ -375,10 +375,6 @@ long HttpClient::download_file(const std::string& url, const std::string& file_p
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT_MS, 4000);
- curl_easy_setopt(curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_PRIOR_KNOWLEDGE);
-
- // to allow self-signed certs
- curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
diff --git a/src/index.cpp b/src/index.cpp
index 9b1b844d..ce5fb65c 100644
--- a/src/index.cpp
+++ b/src/index.cpp
@@ -3010,12 +3010,9 @@ Option<bool> Index::search(std::vector<query_tokens_t>& field_query_tokens, cons
sort_order, field_values, geopoint_indices,
curated_ids_sorted, all_result_ids, all_result_ids_len, groups_processed);
- LOG(INFO) << "all_result_ids_len: " << all_result_ids_len;
if(!vector_query.field_name.empty()) {
VectorFilterFunctor filterFunctor(seq_ids->uncompress(), seq_ids->num_ids());
- LOG(INFO) << "vector_query.field_name: " << vector_query.field_name;
auto& field_vector_index = vector_index.at(vector_query.field_name);
- LOG(INFO) << "field_vector_index->vecdex->size():";
std::vector<std::pair<float, size_t>> dist_labels;
auto k = std::max<size_t>(vector_query.k, per_page * page);
diff --git a/src/typesense_server_utils.cpp b/src/typesense_server_utils.cpp
index 5acc2220..44565b61 100644
--- a/src/typesense_server_utils.cpp
+++ b/src/typesense_server_utils.cpp
@@ -454,8 +454,6 @@ int run_server(const Config & config, const std::string & version, void (*master
LOG(INFO) << "Failed to initialize rate limit manager: " << rate_limit_manager_init.error();
}
-
- LOG(INFO) << "MODEL DIR: " << config.get_model_dir();
if(config.get_model_dir().size() > 0) {
LOG(INFO) << "Loading text embedding models from " << config.get_model_dir();
TextEmbedderManager::get_instance().set_model_dir(config.get_model_dir());
diff --git a/test/collection_all_fields_test.cpp b/test/collection_all_fields_test.cpp
index 8bc2a656..43f8525d 100644
--- a/test/collection_all_fields_test.cpp
+++ b/test/collection_all_fields_test.cpp
@@ -1600,15 +1600,17 @@ TEST_F(CollectionAllFieldsTest, CreateFromFieldJSONInvalidField) {
std::vector<field> fields;
std::string fallback_field_type;
- size_t num_auto_detect_fields;
+ auto arr = nlohmann::json::array();
+ arr.push_back(field_json);
- auto field_op = field::json_field_to_field(false, field_json, fields, fallback_field_type, num_auto_detect_fields);
+ auto field_op = field::json_fields_to_fields(false, arr, fallback_field_type, fields);
ASSERT_FALSE(field_op.ok());
- ASSERT_EQ("Property `create_from` must be an array of existing string fields.", field_op.error());
+ ASSERT_EQ("Property `create_from` can only be used with array of string fields.", field_op.error());
}
TEST_F(CollectionAllFieldsTest, CreateFromFieldNoModelDir) {
+ TextEmbedderManager::model_dir = std::string();
nlohmann::json field_json;
field_json["name"] = "embedding";
field_json["type"] = "float[]";
@@ -1616,12 +1618,13 @@ TEST_F(CollectionAllFieldsTest, CreateFromFieldNoModelDir) {
std::vector<field> fields;
std::string fallback_field_type;
- size_t num_auto_detect_fields;
+ auto arr = nlohmann::json::array();
+ arr.push_back(field_json);
- auto field_op = field::json_field_to_field(false, field_json, fields, fallback_field_type, num_auto_detect_fields);
+ auto field_op = field::json_fields_to_fields(false, arr, fallback_field_type, fields);
ASSERT_FALSE(field_op.ok());
- ASSERT_EQ("Property `create_from` must be an array of existing string fields.", field_op.error());
+ ASSERT_EQ("Text embedding is not enabled. Please set `model-dir` at startup.", field_op.error());
}
TEST_F(CollectionAllFieldsTest, CreateFromNotArray) {
@@ -1633,9 +1636,10 @@ TEST_F(CollectionAllFieldsTest, CreateFromNotArray) {
std::vector<field> fields;
std::string fallback_field_type;
- size_t num_auto_detect_fields;
+ auto arr = nlohmann::json::array();
+ arr.push_back(field_json);
- auto field_op = field::json_field_to_field(false, field_json, fields, fallback_field_type, num_auto_detect_fields);
+ auto field_op = field::json_fields_to_fields(false, arr, fallback_field_type, fields);
ASSERT_FALSE(field_op.ok());
ASSERT_EQ("Property `create_from` must be an array.", field_op.error());
@@ -1650,10 +1654,10 @@ TEST_F(CollectionAllFieldsTest, ModelPathWithoutCreateFrom) {
std::vector<field> fields;
std::string fallback_field_type;
- size_t num_auto_detect_fields;
-
- auto field_op = field::json_field_to_field(false, field_json, fields, fallback_field_type, num_auto_detect_fields);
+ auto arr = nlohmann::json::array();
+ arr.push_back(field_json);
+ auto field_op = field::json_fields_to_fields(false, arr, fallback_field_type, fields);
ASSERT_FALSE(field_op.ok());
ASSERT_EQ("Property `model_path` can only be used with `create_from`.", field_op.error());
}