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

Synonym queries should preserve prefix option.

Kishore Nallana9a2d8cc11ec
diff --git a/include/index.h b/include/index.h
index ee667353..da64f7ed 100644
--- a/include/index.h
+++ b/include/index.h
@@ -905,7 +905,9 @@ public:
const size_t typo_tokens_threshold, const size_t group_limit,
const std::vector<std::string>& group_by_fields, bool prioritize_exact_match,
const bool prioritize_token_position,
- const bool exhaustive_search, const size_t concurrency, size_t min_len_1typo,
+ const bool exhaustive_search, const size_t concurrency,
+ const std::vector<bool>& prefixes,
+ size_t min_len_1typo,
size_t min_len_2typo, const size_t max_candidates, const std::set<uint32_t>& curated_ids,
const std::vector<uint32_t>& curated_ids_sorted, const uint32_t* exclude_token_ids,
size_t exclude_token_ids_size,
diff --git a/src/index.cpp b/src/index.cpp
index 6162a3ef..3be32394 100644
--- a/src/index.cpp
+++ b/src/index.cpp
@@ -2436,7 +2436,7 @@ void Index::search(std::vector<query_tokens_t>& field_query_tokens, const std::v
// do synonym based searches
do_synonym_search(the_fields, filters, included_ids_map, sort_fields_std, curated_topster, token_order,
0, group_limit, group_by_fields, prioritize_exact_match, prioritize_token_position,
- exhaustive_search, concurrency,
+ exhaustive_search, concurrency, prefixes,
min_len_1typo, min_len_2typo, max_candidates, curated_ids, curated_ids_sorted,
excluded_result_ids, excluded_result_ids_size, topster, q_pos_synonyms, syn_orig_num_tokens,
groups_processed, searched_queries, all_result_ids, all_result_ids_len,
@@ -3481,7 +3481,9 @@ void Index::do_synonym_search(const std::vector<search_field_t>& the_fields,
const size_t typo_tokens_threshold, const size_t group_limit,
const std::vector<std::string>& group_by_fields, bool prioritize_exact_match,
const bool prioritize_token_position,
- const bool exhaustive_search, const size_t concurrency, size_t min_len_1typo,
+ const bool exhaustive_search, const size_t concurrency,
+ const std::vector<bool>& prefixes,
+ size_t min_len_1typo,
size_t min_len_2typo, const size_t max_candidates, const std::set<uint32_t>& curated_ids,
const std::vector<uint32_t>& curated_ids_sorted, const uint32_t* exclude_token_ids,
size_t exclude_token_ids_size,
@@ -3504,7 +3506,7 @@ void Index::do_synonym_search(const std::vector<search_field_t>& the_fields,
exclude_token_ids_size, filter_ids, filter_ids_length, curated_ids_sorted,
sort_fields_std, {0}, searched_queries, qtoken_set, actual_topster, groups_processed,
all_result_ids, all_result_ids_len, group_limit, group_by_fields, prioritize_exact_match,
- prioritize_token_position, query_hashes, token_order, {0}, typo_tokens_threshold,
+ prioritize_token_position, query_hashes, token_order, prefixes, typo_tokens_threshold,
exhaustive_search, max_candidates, min_len_1typo,
min_len_2typo, syn_orig_num_tokens, sort_order, field_values, geopoint_indices);
}
diff --git a/test/collection_synonyms_test.cpp b/test/collection_synonyms_test.cpp
index e6068b73..db83f288 100644
--- a/test/collection_synonyms_test.cpp
+++ b/test/collection_synonyms_test.cpp
@@ -574,8 +574,6 @@ TEST_F(CollectionSynonymsTest, ExactMatchVsSynonymMatchCrossFields) {
auto res = coll1->search("cmo", {"title", "description"}, "", {}, {},
{0}, 10, 1, FREQUENCY, {false}, 0).get();
- LOG(INFO) << res;
-
ASSERT_EQ(2, res["hits"].size());
ASSERT_EQ(2, res["found"].get<uint32_t>());
@@ -780,3 +778,41 @@ TEST_F(CollectionSynonymsTest, SynonymExpansionAndCompressionRanking) {
collectionManager.drop_collection("coll1");
}
+
+TEST_F(CollectionSynonymsTest, SynonymQueriesMustHavePrefixEnabled) {
+ Collection *coll1;
+
+ std::vector<field> fields = {field("title", field_types::STRING, false),
+ field("points", field_types::INT32, false),};
+
+ coll1 = collectionManager.get_collection("coll1").get();
+ if(coll1 == nullptr) {
+ coll1 = collectionManager.create_collection("coll1", 1, fields, "points").get();
+ }
+
+ std::vector<std::vector<std::string>> records = {
+ {"Nonstick Cookware", "100"},
+ };
+
+ for(size_t i=0; i<records.size(); i++) {
+ nlohmann::json doc;
+
+ doc["id"] = std::to_string(i);
+ doc["title"] = records[i][0];
+ doc["points"] = std::stoi(records[i][1]);
+
+ ASSERT_TRUE(coll1->add(doc.dump()).ok());
+ }
+
+ synonym_t synonym1{"syn-1", {"ns"}, {{"nonstick"}}};
+ coll1->add_synonym(synonym1);
+
+ auto res = coll1->search("ns cook", {"title"}, "", {}, {}, {2}, 10, 1, FREQUENCY, {true}, 0).get();
+ ASSERT_EQ(1, res["hits"].size());
+ ASSERT_EQ(1, res["found"].get<uint32_t>());
+
+ res = coll1->search("ns cook", {"title"}, "", {}, {}, {2}, 10, 1, FREQUENCY, {false}, 0).get();
+ ASSERT_EQ(0, res["hits"].size());
+
+ collectionManager.drop_collection("coll1");
+}
\ No newline at end of file