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 field-wise prefix searching edge case.

Kishore Nallan77efb5c0ed51
diff --git a/src/index.cpp b/src/index.cpp
index 804147d2..7a584d45 100644
--- a/src/index.cpp
+++ b/src/index.cpp
@@ -1171,7 +1171,7 @@ void Index::search_all_candidates(const size_t num_search_fields,
LOG(INFO) << "token_candidates_vec.size(): " << token_candidates_vec.size();
}*/
- std::unordered_set<std::string> trimmed_candidates;
+ std::set<std::string> trimmed_candidates;
if(token_candidates_vec.size() > 1 && token_candidates_vec.back().candidates.size() > max_candidates) {
std::vector<uint32_t> temp_ids;
@@ -1187,7 +1187,7 @@ void Index::search_all_candidates(const size_t num_search_fields,
const bool prefix_search = query_tokens.back().is_prefix_searched;
const uint32_t token_num_typos = query_tokens.back().num_typos;
- const bool token_prefix = query_tokens.back().is_prefix_searched;
+ const bool token_prefix = (token_str.size() > token_candidates_vec.back().token.value.size());
auto token_c_str = (const unsigned char*) token_str.c_str();
const size_t token_len = token_str.size() + 1;
diff --git a/test/collection_specific_more_test.cpp b/test/collection_specific_more_test.cpp
index ecb0d50d..fb916320 100644
--- a/test/collection_specific_more_test.cpp
+++ b/test/collection_specific_more_test.cpp
@@ -631,3 +631,50 @@ TEST_F(CollectionSpecificMoreTest, SplitTokensCrossFieldMatching) {
ASSERT_EQ(1, results["hits"].size());
collectionManager.drop_collection("coll1");
}
+
+TEST_F(CollectionSpecificMoreTest, PrefixSearchOnSpecificFields) {
+ std::vector<field> fields = {field("name", field_types::STRING, false),
+ field("brand", field_types::STRING, false),};
+
+ Collection* coll1 = collectionManager.create_collection("coll1", 1, fields).get();
+
+ // atleast 4 tokens that begin with "girl" to trigger this regression
+ std::vector<std::string> names = {
+ "Jungle Girl", "Jungle Girlz", "Jam Foo1", "Jam Foo2", "Jam Foo3", "Jam Foo4", "Jam Foo"
+ };
+
+ std::vector<std::string> brands = {
+ "Foobar", "Foobar2", "Girlx", "Girly", "Girlz", "Girlz", "Girlzz"
+ };
+
+ for(size_t i = 0; i < names.size(); i++) {
+ nlohmann::json doc1;
+ doc1["name"] = names[i];
+ doc1["brand"] = brands[i];
+ ASSERT_TRUE(coll1->add(doc1.dump()).ok());
+ }
+
+ auto results = coll1->search("jungle girl", {"name", "brand"},
+ "", {}, {}, {0}, 10,
+ 1, FREQUENCY, {false, true},
+ 0).get();
+
+ ASSERT_EQ(1, results["hits"].size());
+
+ results = coll1->search("jam foo", {"name"},
+ "", {}, {}, {0}, 10,
+ 1, FREQUENCY, {true},
+ 0).get();
+
+ ASSERT_EQ(4, results["hits"].size());
+ ASSERT_EQ("6", results["hits"][0]["document"]["id"].get<std::string>());
+
+ results = coll1->search("jam foo", {"name"},
+ "", {}, {}, {0}, 10,
+ 1, FREQUENCY, {false},
+ 0).get();
+
+ ASSERT_EQ(1, results["hits"].size());
+
+ collectionManager.drop_collection("coll1");
+}