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

Merge branch 'v0.24-nested' into v0.25

Kishore Nallaneda8f2f0f288
diff --cc src/collection.cpp
index 9f9540ee,4b874fc5..5af7a5b6
--- a/src/collection.cpp
+++ b/src/collection.cpp
@@@ -4161,140 -4078,7 +4161,140 @@@ std::string Collection::get_fallback_fi
bool Collection::get_enable_nested_fields() {
return enable_nested_fields;
--};
++}
+
+Option<bool> Collection::parse_facet(const std::string& facet_field, std::vector<facet>& facets) const{
+ const std::regex base_pattern("[a-z]+\\(.*\\)");
+ const std::regex range_pattern("[[a-zA-Z]+:\\[([0-9]+)\\, ([0-9]+)\\]");
+
+ if(facet_field.find(":") != std::string::npos) { //range based facet
+
+ if(!std::regex_match(facet_field, base_pattern)){
+ std::string error = "Range string base pattern not matched.";
+ return Option<bool>(400, error);
+ }
+
+ auto startpos = facet_field.find("(");
+ auto field_name = facet_field.substr(0, startpos);
+
+ const field& a_field = search_schema.at(field_name);
+ if(!a_field.is_int32() && !a_field.is_int64()){
+ std::string error = "Range facet is restricted to Numeric fields only.";
+ return Option<bool>(400, error);
+ }
+
+ facet a_facet(field_name);
+
+ //starting after "(" and excluding ")"
+ auto range_string = std::string(facet_field.begin() + startpos + 1, facet_field.end() - 1);
+
+ //split the ranges
+ std::vector<std::string> result;
+ startpos = 0;
+ int index=0;
+ int commaFound = 0, rangeFound = 0;
+ bool range_open=false;
+ while(index < range_string.size()){
+ if(range_string[index] == ']'){
+ if(range_open == true){
+ std::string range = range_string.substr(startpos, index + 1 - startpos);
+ range=StringUtils::trim(range);
+ result.emplace_back(range);
+ rangeFound++;
+ range_open=false;
+ }
+ else{
+ result.clear();
+ break;
+ }
+ }
+ else if(range_string[index] == ',' && range_open == false){
+ startpos = index+1;
+ commaFound++;
+ }
+ else if(range_string[index] == '['){
+ if((commaFound == rangeFound) && range_open==false){
+ range_open=true;
+ }
+ else{
+ result.clear();
+ break;
+ }
+ }
+
+ index++;
+ }
+
+ if((result.empty()) || (range_open==true)){
+ std::string error = "Error splitting the range string.";
+ return Option<bool>(400, error);
+ }
+
+ std::vector<std::tuple<int64_t, int64_t, std::string>> tupVec;
+
+ auto& range_map = a_facet.facet_range_map;
+ for(const auto& range : result){
+ //validate each range syntax
+ if(!std::regex_match(range, range_pattern)){
+ std::string error = "Range String range pattern not matched.";
+ return Option<bool>(400, error);
+ }
+
+ auto pos1 = range.find(":");
+ std::string range_val = range.substr(0, pos1);
+
+ auto pos2 = range.find(",");
+ auto pos3 = range.find("]");
+
+ int64_t lower_range = std::stoll(range.substr(pos1 + 2, pos2));
+ int64_t upper_range = std::stoll(range.substr(pos2 + 1, pos3));
+
+ tupVec.emplace_back(std::make_tuple(lower_range, upper_range, range_val));
+ }
+
+ //sort the range values so that we can check continuity
+ sort(tupVec.begin(), tupVec.end());
+
+ for(const auto& tup : tupVec){
+
+ int64_t lower_range = std::get<0>(tup);
+ int64_t upper_range = std::get<1>(tup);
+ std::string range_val = std::get<2>(tup);
+ //check if ranges are continous or not
+ if((!range_map.empty()) && (range_map.find(lower_range)== range_map.end())){
+ std::string error = "Ranges in range facet syntax should be continous.";
+ return Option<bool>(400, error);
+ }
+
+ range_map[upper_range] = range_val;
+ }
+
+ a_facet.is_range_query = true;
+
+ facets.emplace_back(std::move(a_facet));
+ } else if (facet_field.find('*') != std::string::npos) { // Wildcard
+ // Trim * from the end.
+ auto prefix = facet_field.substr(0, facet_field.size() - 1);
+ auto pair = search_schema.equal_prefix_range(prefix);
+
+ // Collect the fields that match the prefix and are marked as facet.
+ for (auto field = pair.first; field != pair.second; field++) {
+ if (field->facet) {
+ facets.emplace_back(facet(field->name));
+ }
+ }
+ } else {//normal facet
+ facets.emplace_back(facet(facet_field));
+ }
+
+ if(search_schema.count(facets.back().field_name) == 0 || !search_schema.at(facets.back().field_name).facet) {
+ std::string error = "Could not find a facet field named `" + facets.back().field_name + "` in the schema.";
+ facets.pop_back();
+ return Option<bool>(404, error);
+ }
+
+ return Option<bool>(true);
+}
Option<bool> Collection::populate_include_exclude_fields(const spp::sparse_hash_set<std::string>& include_fields,
const spp::sparse_hash_set<std::string>& exclude_fields,