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

fix _vector_query parsing in sort_by (#1915)

Krunal Gandhi028a90173e74
diff --git a/src/collection_manager.cpp b/src/collection_manager.cpp
index 79d1c8ae..0f332652 100644
--- a/src/collection_manager.cpp
+++ b/src/collection_manager.cpp
@@ -970,6 +970,7 @@ bool parse_nested_join_sort_by_str(const std::string& sort_by_str, uint32_t& ind
bool CollectionManager::parse_sort_by_str(std::string sort_by_str, std::vector<sort_by>& sort_fields) {
std::string sort_field_expr;
char prev_non_space_char = '`';
+ int brace_open_count = 0;
for(uint32_t i=0; i < sort_by_str.size(); i++) {
if (sort_field_expr.empty()) {
@@ -1055,7 +1056,9 @@ bool CollectionManager::parse_sort_by_str(std::string sort_by_str, std::vector<s
}
}
- if(i == sort_by_str.size()-1 || (sort_by_str[i] == ',' && !isdigit(prev_non_space_char))) {
+ if(i == sort_by_str.size()-1 || (sort_by_str[i] == ',' && !isdigit(prev_non_space_char)
+ && brace_open_count == 0)) {
+
if(i == sort_by_str.size()-1) {
sort_field_expr += sort_by_str[i];
}
@@ -1089,6 +1092,12 @@ bool CollectionManager::parse_sort_by_str(std::string sort_by_str, std::vector<s
i++;
}
} else {
+ if(sort_by_str[i] == '(') {
+ brace_open_count++;
+ } else if(sort_by_str[i] == ')') {
+ brace_open_count--;
+ }
+
sort_field_expr += sort_by_str[i];
}
diff --git a/test/collection_sorting_test.cpp b/test/collection_sorting_test.cpp
index 80d016fa..9bf3e5d2 100644
--- a/test/collection_sorting_test.cpp
+++ b/test/collection_sorting_test.cpp
@@ -2640,4 +2640,63 @@ TEST_F(CollectionSortingTest, TestVectorQueryQsSorting) {
ASSERT_EQ(2, results["hits"].size());
ASSERT_EQ("0", results["hits"][0]["document"]["id"]);
ASSERT_EQ("1", results["hits"][1]["document"]["id"]);
+}
+
+TEST_F(CollectionSortingTest, TestVectorQueryDistanceThresholdSorting) {
+ auto schema_json = R"({
+ "name": "products",
+ "fields":[
+ {
+ "name": "product_name",
+ "type": "string"
+ },
+ {
+ "name": "embedding",
+ "type": "float[]",
+ "embed": {
+ "from": [
+ "product_name"
+ ],
+ "model_config": {
+ "model_name": "ts/e5-small"
+ }
+ }
+ }
+ ]
+ })"_json;
+
+ EmbedderManager::set_model_dir("/tmp/typesense_test/models");
+
+ auto coll_op = collectionManager.create_collection(schema_json);
+ ASSERT_TRUE(coll_op.ok());
+ auto coll = coll_op.get();
+
+ std::vector<std::string> products = {"Mobile Phone", "Cell Phone", "Telephone"};
+ nlohmann::json doc;
+ for (auto product: products) {
+ doc["product_name"] = product;
+ ASSERT_TRUE(coll->add(doc.dump()).ok());
+ }
+
+ // when eval condition is empty
+ std::map<std::string, std::string> req_params = {
+ {"collection", "products"},
+ {"q", "phone"},
+ {"query_by", "product_name"},
+ {"sort_by", "_text_match:desc,_vector_query(embedding:([],distance_threshold:0.3)):asc"},
+ {"exclude_fields", "embedding"}
+ };
+ nlohmann::json embedded_params;
+ std::string json_res;
+ auto now_ts = std::chrono::duration_cast<std::chrono::microseconds>(
+ std::chrono::system_clock::now().time_since_epoch()).count();
+ auto search_op = collectionManager.do_search(req_params, embedded_params, json_res, now_ts);
+
+ auto res = nlohmann::json::parse(json_res);
+
+ ASSERT_EQ(2, res["hits"].size());
+ ASSERT_EQ("Mobile Phone", res["hits"][0]["document"]["product_name"]);
+ ASSERT_EQ(0.07853113859891891, res["hits"][0]["vector_distance"].get<float>());
+ ASSERT_EQ("Cell Phone", res["hits"][1]["document"]["product_name"]);
+ ASSERT_EQ(0.08472149819135666, res["hits"][1]["vector_distance"].get<float>());
}
\ No newline at end of file