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

Only allow prefix matching with wildcard.

Harpreet Sangarbf4c3d39dca0
diff --git a/src/collection.cpp b/src/collection.cpp
index 14b9bc5d..208583a8 100644
--- a/src/collection.cpp
+++ b/src/collection.cpp
@@ -4297,6 +4297,10 @@ Option<bool> Collection::parse_facet(const std::string& facet_field, std::vector
facets.emplace_back(std::move(a_facet));
} else if (facet_field.find('*') != std::string::npos) { // Wildcard
+ if (facet_field[facet_field.size() - 1] != '*') {
+ return Option<bool>(404, "Only prefix matching with a wildcard is allowed.");
+ }
+
// Trim * from the end.
auto prefix = facet_field.substr(0, facet_field.size() - 1);
auto pair = search_schema.equal_prefix_range(prefix);
diff --git a/test/collection_faceting_test.cpp b/test/collection_faceting_test.cpp
index fc01c5e9..94856770 100644
--- a/test/collection_faceting_test.cpp
+++ b/test/collection_faceting_test.cpp
@@ -363,6 +363,15 @@ TEST_F(CollectionFacetingTest, FacetCounts) {
ASSERT_FALSE(res_op.ok());
ASSERT_STREQ("Could not find a facet field named `foobar` in the schema.", res_op.error().c_str());
+ // only prefix matching is valid
+ res_op = coll_array_fields->search("*", query_fields, "", {"*_facet"}, sort_fields, {0}, 10, 1, FREQUENCY,
+ {false}, Index::DROP_TOKENS_THRESHOLD,
+ spp::sparse_hash_set<std::string>(),
+ spp::sparse_hash_set<std::string>(), 10);
+
+ ASSERT_FALSE(res_op.ok());
+ ASSERT_STREQ("Only prefix matching with a wildcard is allowed.", res_op.error().c_str());
+
// unknown wildcard facet field
res_op = coll_array_fields->search("*", query_fields, "", {"foo*"}, sort_fields, {0}, 10, 1, FREQUENCY,
{false}, Index::DROP_TOKENS_THRESHOLD,