#community-help

Resolving Error in Filter Parameters Due to Special Characters

TLDR Andrew encountered an error with filter parameters, suspecting it was due to many filters. Kishore Nallan pointed out the presence of parentheses may confuse the parser and suggested wrapping the string with backticks, which solved the issue. Andrew then asked about the potential impact of large filters on the cluster.

Powered by Struct AI
Jan 03, 2023 (11 months ago)
Andrew
Photo of md5-7766f890de99fa72a6d81315691a6758
Andrew
03:59 AM
This is a bit of a follow up to my earlier thread, i'm having an error in the filter parameters from what I assume are too many filters.


String ignore = StaticTSStrings.setBlockedUsers(user);
    String similar = StaticTSStrings.setSimilarUsers(user);

    final search = {
      'searches': [
        {
          'collection': 'users',
          'q': '*',
          'query_by': 'name',
          'page': '1',
          'per_page': '10',
          'sort_by': 'votes:desc',
          'filter_by': '($ignore) && ($similar) && finishedOnboarding:true'
        }
      ]
    };

The part that causes a problem is the $similar. Basically, it is a string formed from the following (apologies for the gross code):


static String setSimilarUsers(User user) {
    String similar = '';
    if (user.firstUndergrad != '') {
      similar += 'firstUndergrad:${user.firstUndergrad} || ';
    }
    if (user.secondUndergrad != '') {
      similar += 'secondUndergrad:${user.secondUndergrad} || ';
    }
    if (user.thirdUndergrad != '') {
      similar += 'thirdUndergrad:${user.thirdUndergrad} || ';
    }
    if (user.postGrad != '') {
      similar += 'postGrad:${user.postGrad} || ';
    }
    if (user.firstStudentOrg != '') {
      similar += 'firstStudentOrg:${user.firstStudentOrg} || ';
    }
    if (user.secondStudentOrg != '') {
      similar += 'secondStudentOrg:${user.secondStudentOrg} || ';
    }
    if (user.thirdStudentOrg != '') {
      similar += 'thirdStudentOrg:${user.thirdStudentOrg} || ';
    }
    if (user.fraternity != '') {
      similar += 'fraternity:!=\'\' || ';
    }
    if (user.favoriteBar != '') {
      similar += 'favoriteBar:${user.favoriteBar} || ';
    }
    if (user.favoriteSpot != '') {
      similar += 'favoriteSpot:${user.favoriteSpot} || ';
    }
    if (user.assosiatedDorm != '') {
      similar += 'assosiatedDorm:${user.assosiatedDorm} || ';
    }
    if (user.worstDorm != '') {
      similar += 'worstDorm:${user.worstDorm} || ';
    }
    if (user.intramuralSport != '') {
      similar += 'intramuralSport:${user.intramuralSport} || ';
    }

    //remove the last ' || ' if the string is not ''
    if (similar != '') {
      similar = similar.substring(0, similar.length - 4);
    }

    return similar;
  }


What this can end up producing is a single string that looks like:

firstUndergrad:Accounting || firstStudentOrg:Horticulture Club || fraternity:!='' || favoriteBar:House Parties(South Park) || favoriteSpot:None


and somewhere in that String, an error is being made. Specifically error code 400, '"error" -> "Could not parse the filter query."'. Am i doing something wrong?
Kishore Nallan
Photo of md5-4e872368b2b2668460205b409e95c2ea
Kishore Nallan
04:04 AM
👋 Are you using the 0.24 RC build?
04:06
Kishore Nallan
04:06 AM
I suspect the presence of ( in the filter value. The parantheses are used for nested filter expressions, so the parser might be getting confused.
04:06
Kishore Nallan
04:06 AM
Maybe try wrapping the string with backticks, like this:

favoriteBar:`House Parties(South Park)`
Andrew
Photo of md5-7766f890de99fa72a6d81315691a6758
Andrew
07:57 PM
Looks like that worked, thanks! But as a follow up to the previous thread, do you think large filters like this would impact the cluster at all?