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

Add tests

ozanarmagan278c29b3ea0c
diff --git a/test/collection_specific_more_test.cpp b/test/collection_specific_more_test.cpp
index 3e5b43a7..7d44351c 100644
--- a/test/collection_specific_more_test.cpp
+++ b/test/collection_specific_more_test.cpp
@@ -2582,4 +2582,117 @@ TEST_F(CollectionSpecificMoreTest, HybridSearchTextMatchInfo) {
ASSERT_EQ(0, results["hits"][0]["text_match_info"]["tokens_matched"].get<size_t>());
ASSERT_EQ(0, results["hits"][1]["text_match_info"]["tokens_matched"].get<size_t>());
+}
+
+
+TEST_F(CollectionSpecificMoreTest, SemanticSearchReturnOnlyVectorDistance) {
+ auto schema_json =
+ R"({
+ "name": "Products",
+ "fields": [
+ {"name": "product_name", "type": "string", "infix": true},
+ {"name": "category", "type": "string"},
+ {"name": "embedding", "type":"float[]", "embed":{"from": ["product_name", "category"], "model_config": {"model_name": "ts/e5-small"}}}
+ ]
+ })"_json;
+
+
+ TextEmbedderManager::set_model_dir("/tmp/typesense_test/models");
+
+ auto collection_create_op = collectionManager.create_collection(schema_json);
+ ASSERT_TRUE(collection_create_op.ok());
+ auto coll1 = collection_create_op.get();
+
+ auto add_op = coll1->add(R"({
+ "product_name": "moisturizer",
+ "category": "beauty"
+ })"_json.dump());
+
+ ASSERT_TRUE(add_op.ok());
+
+ auto results = coll1->search("moisturizer", {"embedding"},
+ "", {}, {}, {2}, 10,
+ 1, FREQUENCY, {true},
+ 0, spp::sparse_hash_set<std::string>()).get();
+
+ ASSERT_EQ(1, results["hits"].size());
+
+ // Return only vector distance
+ ASSERT_EQ(0, results["hits"][0].count("text_match_info"));
+ ASSERT_EQ(0, results["hits"][0].count("hybrid_search_info"));
+ ASSERT_EQ(1, results["hits"][0].count("vector_distance"));
+}
+
+TEST_F(CollectionSpecificMoreTest, KeywordSearchReturnOnlyTextMatchInfo) {
+ auto schema_json =
+ R"({
+ "name": "Products",
+ "fields": [
+ {"name": "product_name", "type": "string", "infix": true},
+ {"name": "category", "type": "string"},
+ {"name": "embedding", "type":"float[]", "embed":{"from": ["product_name", "category"], "model_config": {"model_name": "ts/e5-small"}}}
+ ]
+ })"_json;
+
+
+ TextEmbedderManager::set_model_dir("/tmp/typesense_test/models");
+
+ auto collection_create_op = collectionManager.create_collection(schema_json);
+ ASSERT_TRUE(collection_create_op.ok());
+ auto coll1 = collection_create_op.get();
+ auto add_op = coll1->add(R"({
+ "product_name": "moisturizer",
+ "category": "beauty"
+ })"_json.dump());
+ ASSERT_TRUE(add_op.ok());
+
+ auto results = coll1->search("moisturizer", {"product_name"},
+ "", {}, {}, {2}, 10,
+ 1, FREQUENCY, {true},
+ 0, spp::sparse_hash_set<std::string>()).get();
+
+
+ ASSERT_EQ(1, results["hits"].size());
+
+ // Return only text match info
+ ASSERT_EQ(0, results["hits"][0].count("vector_distance"));
+ ASSERT_EQ(0, results["hits"][0].count("hybrid_search_info"));
+ ASSERT_EQ(1, results["hits"][0].count("text_match_info"));
+}
+
+TEST_F(CollectionSpecificMoreTest, HybridSearchReturnAllInfo) {
+ auto schema_json =
+ R"({
+ "name": "Products",
+ "fields": [
+ {"name": "product_name", "type": "string", "infix": true},
+ {"name": "category", "type": "string"},
+ {"name": "embedding", "type":"float[]", "embed":{"from": ["product_name", "category"], "model_config": {"model_name": "ts/e5-small"}}}
+ ]
+ })"_json;
+
+ TextEmbedderManager::set_model_dir("/tmp/typesense_test/models");
+
+ auto collection_create_op = collectionManager.create_collection(schema_json);
+ ASSERT_TRUE(collection_create_op.ok());
+ auto coll1 = collection_create_op.get();
+
+ auto add_op = coll1->add(R"({
+ "product_name": "moisturizer",
+ "category": "beauty"
+ })"_json.dump());
+ ASSERT_TRUE(add_op.ok());
+
+
+ auto results = coll1->search("moisturizer", {"product_name", "embedding"},
+ "", {}, {}, {2}, 10,
+ 1, FREQUENCY, {true},
+ 0, spp::sparse_hash_set<std::string>()).get();
+
+ ASSERT_EQ(1, results["hits"].size());
+
+ // Return all info
+ ASSERT_EQ(1, results["hits"][0].count("vector_distance"));
+ ASSERT_EQ(1, results["hits"][0].count("text_match_info"));
+ ASSERT_EQ(1, results["hits"][0].count("hybrid_search_info"));
}
\ No newline at end of file