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

Improve facet query validation.

Jason Bosco66b679515a68
diff --git a/src/collection.cpp b/src/collection.cpp
index 73711cab..32fa3764 100644
--- a/src/collection.cpp
+++ b/src/collection.cpp
@@ -664,17 +664,25 @@ Option<nlohmann::json> Collection::search(const std::string & query, const std::
return Option<nlohmann::json>(400, error);
}
- // facet query field must be part of facet fields requested
- facet_query = { StringUtils::trim(facet_query_vec[0]), StringUtils::trim(facet_query_vec[1]) };
- if(std::find(facet_fields.begin(), facet_fields.end(), facet_query.field_name) == facet_fields.end()) {
- std::string error = "Facet query refers to a facet field `" + facet_query.field_name + "` " +
- "that is not part of `facet_by` parameter.";
+ if(facet_query_vec.size() == 1) {
+ // empty facet value, we will treat it as no facet query
+ facet_query = {"", ""};
+ } else if(facet_query_vec.size() > 2) {
+ std::string error = "Facet query must be in the `facet_field: value` format.";
return Option<nlohmann::json>(400, error);
- }
+ } else {
+ // facet query field must be part of facet fields requested
+ facet_query = { StringUtils::trim(facet_query_vec[0]), StringUtils::trim(facet_query_vec[1]) };
+ if(std::find(facet_fields.begin(), facet_fields.end(), facet_query.field_name) == facet_fields.end()) {
+ std::string error = "Facet query refers to a facet field `" + facet_query.field_name + "` " +
+ "that is not part of `facet_by` parameter.";
+ return Option<nlohmann::json>(400, error);
+ }
- if(facet_schema.count(facet_query.field_name) == 0) {
- std::string error = "Could not find a facet field named `" + facet_query.field_name + "` in the schema.";
- return Option<nlohmann::json>(404, error);
+ if(facet_schema.count(facet_query.field_name) == 0) {
+ std::string error = "Could not find a facet field named `" + facet_query.field_name + "` in the schema.";
+ return Option<nlohmann::json>(404, error);
+ }
}
}
diff --git a/test/collection_faceting_test.cpp b/test/collection_faceting_test.cpp
index 6390b371..4e545b18 100644
--- a/test/collection_faceting_test.cpp
+++ b/test/collection_faceting_test.cpp
@@ -432,6 +432,21 @@ TEST_F(CollectionFacetingTest, FacetCounts) {
ASSERT_STREQ("tags", results["facet_counts"][0]["field_name"].get<std::string>().c_str());
ASSERT_EQ(0, results["facet_counts"][0]["counts"].size());
+ // empty facet query value should return all facets without any filtering of facets
+ results = coll_array_fields->search("*", query_fields, "", facets, sort_fields, 0, 10, 1, FREQUENCY,
+ false, Index::DROP_TOKENS_THRESHOLD,
+ spp::sparse_hash_set<std::string>(),
+ spp::sparse_hash_set<std::string>(), 10, "tags: ").get();
+
+ ASSERT_EQ(5, results["hits"].size());
+
+ results = coll_array_fields->search("*", query_fields, "", facets, sort_fields, 0, 10, 1, FREQUENCY,
+ false, Index::DROP_TOKENS_THRESHOLD,
+ spp::sparse_hash_set<std::string>(),
+ spp::sparse_hash_set<std::string>(), 10, "tags:").get();
+
+ ASSERT_EQ(5, results["hits"].size());
+
// bad facet query syntax
auto res_op = coll_array_fields->search("*", query_fields, "", facets, sort_fields, 0, 10, 1, FREQUENCY,
false, Index::DROP_TOKENS_THRESHOLD,
@@ -468,6 +483,15 @@ TEST_F(CollectionFacetingTest, FacetCounts) {
ASSERT_FALSE(res_op.ok());
ASSERT_STREQ("Facet query refers to a facet field `name_facet` that is not part of `facet_by` parameter.", res_op.error().c_str());
+ // facet query with multiple colons
+ res_op = coll_array_fields->search("*", query_fields, "", facets, sort_fields, 0, 10, 1, FREQUENCY,
+ false, Index::DROP_TOKENS_THRESHOLD,
+ spp::sparse_hash_set<std::string>(),
+ spp::sparse_hash_set<std::string>(), 10, "tags:foo:bar");
+
+ ASSERT_FALSE(res_op.ok());
+ ASSERT_STREQ("Facet query must be in the `facet_field: value` format.", res_op.error().c_str());
+
collectionManager.drop_collection("coll_array_fields");
}