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 hybrid search with filters

ozanarmagan2b154226cab4
diff --git a/src/index.cpp b/src/index.cpp
index f883c127..8be15837 100644
--- a/src/index.cpp
+++ b/src/index.cpp
@@ -3206,6 +3206,18 @@ Option<bool> Index::search(std::vector<query_tokens_t>& field_query_tokens, cons
// For hybrid search, we need to give weight to text match and vector search
const float VECTOR_SEARCH_WEIGHT = vector_query.alpha;
const float TEXT_MATCH_WEIGHT = 1.0 - VECTOR_SEARCH_WEIGHT;
+
+ bool no_filters_provided = (filter_tree_root == nullptr && filter_result.count == 0);
+
+ // list of all document ids
+ if (no_filters_provided) {
+ filter_result.count = seq_ids->num_ids();
+ filter_result.docs = seq_ids->uncompress();
+ }
+
+ curate_filtered_ids(curated_ids, excluded_result_ids,
+ excluded_result_ids_size, filter_result.docs, filter_result.count, curated_ids_sorted);
+ collate_included_ids({}, included_ids_map, curated_topster, searched_queries);
VectorFilterFunctor filterFunctor(filter_result.docs, filter_result.count);
auto& field_vector_index = vector_index.at(vector_query.field_name);
diff --git a/test/collection_vector_search_test.cpp b/test/collection_vector_search_test.cpp
index 72244efe..299b001c 100644
--- a/test/collection_vector_search_test.cpp
+++ b/test/collection_vector_search_test.cpp
@@ -2822,4 +2822,82 @@ TEST_F(CollectionVectorTest, TestSemanticSearchAfterUpdate) {
ASSERT_TRUE(result.ok());
ASSERT_EQ(1, result.get()["hits"].size());
ASSERT_EQ("potato", result.get()["hits"][0]["document"]["name"]);
+}
+
+
+TEST_F(CollectionVectorTest, TestHybridSearchHiddenHits) {
+ nlohmann::json schema = R"({
+ "name": "test",
+ "fields": [
+ {
+ "name": "name",
+ "type": "string"
+ },
+ {
+ "name": "embedding",
+ "type": "float[]",
+ "embed": {
+ "from": [
+ "name"
+ ],
+ "model_config": {
+ "model_name": "ts/e5-small"
+ }
+ }
+ }
+ ]
+ })"_json;
+
+ TextEmbedderManager::set_model_dir("/tmp/typesense_test/models");
+
+ auto collection_create_op = collectionManager.create_collection(schema);
+ ASSERT_TRUE(collection_create_op.ok());
+
+ auto coll = collection_create_op.get();
+
+ auto add_op = coll->add(R"({
+ "name": "soccer",
+ "id": "0"
+ })"_json.dump());
+
+ ASSERT_TRUE(add_op.ok());
+
+ add_op = coll->add(R"({
+ "name": "guitar",
+ "id": "1"
+ })"_json.dump());
+
+ ASSERT_TRUE(add_op.ok());
+
+ add_op = coll->add(R"({
+ "name": "typesense",
+ "id": "2"
+ })"_json.dump());
+
+ ASSERT_TRUE(add_op.ok());
+
+ add_op = coll->add(R"({
+ "name": "potato",
+ "id": "3"
+ })"_json.dump());
+
+ ASSERT_TRUE(add_op.ok());
+
+ auto results = coll->search("sports", {"name", "embedding"},
+ "", {}, {}, {2}, 10,
+ 1, FREQUENCY, {true},
+ 0, spp::sparse_hash_set<std::string>()).get();
+
+ ASSERT_EQ(4, results["hits"].size());
+ ASSERT_STREQ("0", results["hits"][0]["document"]["id"].get<std::string>().c_str());
+
+
+ // do hybrid search with hidden_hits
+ auto hybrid_results = coll->search("sports", {"name", "embedding"},
+ "", {}, {}, {2}, 10,
+ 1, FREQUENCY, {true},
+ 0, spp::sparse_hash_set<std::string>(), spp::sparse_hash_set<std::string>(), 10, "", 30, 4, "", 1, "", "0").get();
+
+ ASSERT_EQ(3, hybrid_results["hits"].size());
+ ASSERT_FALSE(hybrid_results["hits"][0]["document"]["id"] == 0);
}
\ No newline at end of file