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 distance comparison bug

ozanarmagana0cdaaa676ae
diff --git a/src/collection.cpp b/src/collection.cpp
index f18adb03..9cb4c52c 100644
--- a/src/collection.cpp
+++ b/src/collection.cpp
@@ -2089,7 +2089,7 @@ Option<nlohmann::json> Collection::search(std::string raw_query,
wrapper_doc["geo_distance_meters"] = geo_distances;
}
- if(!vector_query.field_name.empty() && field_order_kv->vector_distance > 0) {
+ if(!vector_query.field_name.empty() && field_order_kv->vector_distance >= 0) {
wrapper_doc["vector_distance"] = field_order_kv->vector_distance;
}
diff --git a/test/collection_vector_search_test.cpp b/test/collection_vector_search_test.cpp
index 299b001c..886bec5b 100644
--- a/test/collection_vector_search_test.cpp
+++ b/test/collection_vector_search_test.cpp
@@ -2900,4 +2900,37 @@ TEST_F(CollectionVectorTest, TestHybridSearchHiddenHits) {
ASSERT_EQ(3, hybrid_results["hits"].size());
ASSERT_FALSE(hybrid_results["hits"][0]["document"]["id"] == 0);
-}
\ No newline at end of file
+}
+
+TEST_F(CollectionVectorTest, Test0VectorDistance) {
+ auto schema_json =
+ R"({
+ "name": "colors",
+ "fields": [
+ {"name": "rgb", "type":"float[]", "num_dim": 3}
+ ]
+ })"_json;
+
+ auto collection_create_op = collectionManager.create_collection(schema_json);
+ ASSERT_TRUE(collection_create_op.ok());
+
+ auto coll = collection_create_op.get();
+
+ auto add_op = coll->add(R"({
+ "rgb": [0.9, 0.9, 0.9]
+ })"_json.dump());
+
+ ASSERT_TRUE(add_op.ok());
+
+ auto results = coll->search("*", {}, "", {}, {}, {0}, 10, 1, FREQUENCY, {true}, Index::DROP_TOKENS_THRESHOLD,
+ spp::sparse_hash_set<std::string>(),
+ spp::sparse_hash_set<std::string>(), 10, "", 30, 5,
+ "", 10, {}, {}, {}, 0,
+ "<mark>", "</mark>", {}, 1000, true, false, true, "", false, 6000 * 1000, 4, 7, fallback,
+ 4, {off}, 32767, 32767, 2,
+ false, true, "rgb:([0.5, 0.5, 0.5])").get();
+
+ ASSERT_EQ(results["hits"].size(), 1);
+ ASSERT_EQ(results["hits"][0].count("vector_distance"), 1);
+ ASSERT_EQ(results["hits"][0]["vector_distance"], 0);
+}