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

This commit is contained in:
Krunal Gandhi 2024-05-14 12:22:29 +00:00 committed by GitHub
parent 43a9bb75d1
commit f17f6f690e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 24 additions and 1 deletions

View File

@ -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()){

View File

@ -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) {

View File

@ -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) {