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

allow fields containing sort keyword with range facets (#1730)

Krunal Gandhif17f6f690e32
diff --git a/src/collection.cpp b/src/collection.cpp
index 3b36af6f..c792cb82 100644
--- a/src/collection.cpp
+++ b/src/collection.cpp
@@ -6208,7 +6208,7 @@ Option<bool> Collection::parse_facet(const std::string& facet_field, std::vector
const std::string _alpha = "_alpha";
if ((facet_field.find(":") != std::string::npos)
- && (facet_field.find("sort") == std::string::npos)) { //range based facet
+ && (facet_field.find("sort_by") == std::string::npos)) { //range based facet
if (!std::regex_match(facet_field, base_pattern)) {
std::string error = "Facet range value is not valid.";
@@ -6223,6 +6223,13 @@ Option<bool> Collection::parse_facet(const std::string& facet_field, std::vector
return Option<bool>(404, error);
}
+ if((field_name.find("sort") == std::string::npos)
+ && (facet_field.find("sort") != std::string::npos)) {
+ //sort keyword is found in facet string but not in facet field
+ std::string error = "Invalid sort format.";
+ return Option<bool>(400, error);
+ }
+
const field& a_field = search_schema.at(field_name);
if(!a_field.is_integer() && !a_field.is_float()){
diff --git a/test/collection_faceting_test.cpp b/test/collection_faceting_test.cpp
index 53d5819c..7c4f18f2 100644
--- a/test/collection_faceting_test.cpp
+++ b/test/collection_faceting_test.cpp
@@ -1230,6 +1230,7 @@ TEST_F(CollectionFacetingTest, FacetParseTest){
field("rank", field_types::INT32, true),
field("range", field_types::INT32, true),
field("review", field_types::FLOAT, true),
+ field("sortindex", field_types::INT32, true),
field("scale", field_types::INT32, false),
};
@@ -1355,6 +1356,13 @@ TEST_F(CollectionFacetingTest, FacetParseTest){
FAIL();
}
}
+
+ //facetfield containing sort keyword should parse successfully
+ std::vector<facet> range_facets_with_sort_as_field;
+ auto facet_range = "sortindex(Top:[85, 100], Average:[60, 85])";
+
+ coll1->parse_facet(facet_range, range_facets_with_sort_as_field);
+ ASSERT_EQ(1, range_facets_with_sort_as_field.size());
}
TEST_F(CollectionFacetingTest, RangeFacetTest) {
diff --git a/test/collection_optimized_faceting_test.cpp b/test/collection_optimized_faceting_test.cpp
index cb465021..3f2d91d7 100644
--- a/test/collection_optimized_faceting_test.cpp
+++ b/test/collection_optimized_faceting_test.cpp
@@ -957,6 +957,7 @@ TEST_F(CollectionOptimizedFacetingTest, FacetParseTest){
field("grade", field_types::INT32, true),
field("rank", field_types::INT32, true),
field("range", field_types::INT32, true),
+ field("sortindex", field_types::INT32, true),
field("scale", field_types::INT32, false),
};
@@ -1053,6 +1054,13 @@ TEST_F(CollectionOptimizedFacetingTest, FacetParseTest){
ASSERT_EQ("rank", mixed_facets_ptr[2]->field_name);
ASSERT_EQ("range", mixed_facets_ptr[1]->field_name);
+
+ //facetfield containing sort keyword should parse successfully
+ std::vector<facet> range_facets_with_sort_as_field;
+ auto facet_range = "sortindex(Top:[85, 100], Average:[60, 85])";
+
+ coll1->parse_facet(facet_range, range_facets_with_sort_as_field);
+ ASSERT_EQ(1, range_facets_with_sort_as_field.size());
}
TEST_F(CollectionOptimizedFacetingTest, RangeFacetTest) {