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 overrides with semantic search (#1906)

Krunal Gandhi9255c4e459fa
diff --git a/src/collection.cpp b/src/collection.cpp
index b2be4cbb..7e36814a 100644
--- a/src/collection.cpp
+++ b/src/collection.cpp
@@ -2055,12 +2055,6 @@ Option<nlohmann::json> Collection::search(std::string raw_query,
}
}
- // Set query to * if it is semantic search
- if(!vector_query.field_name.empty() && processed_search_fields.empty()) {
- query = "*";
- }
-
-
if(!vector_query.field_name.empty() && vector_query.values.empty() && num_embed_fields == 0) {
std::string error = "Vector query could not find any embedded fields.";
return Option<nlohmann::json>(400, error);
@@ -2333,6 +2327,11 @@ Option<nlohmann::json> Collection::search(std::string raw_query,
}
*/
+ // Set query to * if it is semantic search
+ if(!vector_query.field_name.empty() && processed_search_fields.empty()) {
+ query = "*";
+ }
+
// validate sort fields and standardize
sort_fields_guard_t sort_fields_guard;
diff --git a/src/index.cpp b/src/index.cpp
index 7af87362..6af2dbb6 100644
--- a/src/index.cpp
+++ b/src/index.cpp
@@ -2957,8 +2957,6 @@ Option<bool> Index::search(std::vector<query_tokens_t>& field_query_tokens, cons
(filter_id_count >= vector_query.flat_search_cutoff && filter_result_iterator->validity == filter_result_iterator_t::valid)) {
dist_results.clear();
- VectorFilterFunctor filterFunctor(filter_result_iterator);
-
std::vector<std::pair<float, size_t>> pairs;
if(field_vector_index->distance_type == cosine) {
std::vector<float> normalized_q(vector_query.values.size());
diff --git a/test/collection_override_test.cpp b/test/collection_override_test.cpp
index 2d14421e..541c4a2d 100644
--- a/test/collection_override_test.cpp
+++ b/test/collection_override_test.cpp
@@ -4434,3 +4434,77 @@ TEST_F(CollectionOverrideTest, AvoidTypoMatchingWhenOverlapWithCuratedData) {
ASSERT_EQ("3", results["hits"][0]["document"]["id"].get<std::string>());
ASSERT_EQ("4", results["hits"][1]["document"]["id"].get<std::string>());
}
+
+TEST_F(CollectionOverrideTest, OverridesWithSemanticSearch) {
+ 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/clip-vit-b-p32"
+ }
+ }
+ }
+ ]
+ })"_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 = {"Cell Phone", "Laptop", "Desktop", "Printer", "Keyboard", "Monitor", "Mouse"};
+ nlohmann::json doc;
+ for (auto product: products) {
+ doc["product_name"] = product;
+ ASSERT_TRUE(coll->add(doc.dump()).ok());
+ }
+
+ auto results = coll->search("phone", {"embedding"}, "", {}, {}, {0}, 10, 1, FREQUENCY, {true}, Index::DROP_TOKENS_THRESHOLD,
+ spp::sparse_hash_set<std::string>(),
+ {"embedding"}).get();
+
+ ASSERT_EQ(results["found"], 7);
+
+ nlohmann::json override_json = {
+ {"id", "exclude-rule"},
+ {
+ "rule", {
+ {"query", "phone"},
+ {"match", override_t::MATCH_CONTAINS}
+ }
+ }
+ };
+ override_json["excludes"] = nlohmann::json::array();
+ override_json["excludes"][0] = nlohmann::json::object();
+ override_json["excludes"][0]["id"] = "0";
+
+ override_t override;
+ override_t::parse(override_json, "", override);
+
+ ASSERT_TRUE(coll->add_override(override).ok());
+
+ results = coll->search("phone", {"embedding"}, "", {}, {}, {0}, 10, 1, FREQUENCY, {true}, Index::DROP_TOKENS_THRESHOLD,
+ spp::sparse_hash_set<std::string>(),
+ {"embedding"}).get();
+
+ ASSERT_EQ(results["found"], 6);
+
+ ASSERT_EQ(results["hits"][0]["document"]["id"], "4");
+ ASSERT_EQ(results["hits"][1]["document"]["id"], "6");
+ ASSERT_EQ(results["hits"][2]["document"]["id"], "1");
+ ASSERT_EQ(results["hits"][3]["document"]["id"], "5");
+ ASSERT_EQ(results["hits"][4]["document"]["id"], "2");
+ ASSERT_EQ(results["hits"][5]["document"]["id"], "3");
+}
\ No newline at end of file