Filters in Algolia

Hi!

How do I best implement this filter in algolia?

Supposing I have this conditions:

[
 {
   showId: 1,
   countryCode: "CA",
   isAllowed: "yes"
 },
 {
   showId: 1,
   countryCode: "US",
   isAllowed: "no"
 },
 {
   showId: 2,
   countryCode: "PH",
   isAllowed: "yes"
 },
]

Based on the JSON above, I’m trying to use the filters attribute, here are the conditions:

  1. Searching ShowId 1 from country “CA” should obviously return result
  2. Searching ShowId 1 from country “US” should obviously not return result
  3. Searching ShowId 1 from country “AE” should return result
  4. Searching ShowId 2 should only return result from country “PH”, this applies if showId has “yes” value on allowed field to atleast one country but no “no” value present on allowed field.

Thank you very much for the usual assistance!

EDIT:
filters: NOT isAllowed: “no” AND (countryCode: ‘CA’ OR countryCode: ‘–’)

Note that countryCode ‘–’ means available to all country

filter query above partially suit my need but still need revision for the fourth requirement. I don’t know how much filter is recommended by algolia so I’m asking this question. I read about being limited to disjunction of conjunction anyway.

Also, additional edit, if the object has no value, it will show all.

Hi @markdaniel_tamayo,

I have trouble to understand the fourth requirement, maybe could describe a bit more what is your use case and what is the UI you’re expecting for implementing it?

Thanks!

Hi @marielaure.thuret

[
 {
   showId: 1,
   countryCode: "CA",
   restrictionTypeId: 1
 },
 {
   showId: 1,
   countryCode: "US",
   restrictionTypeId: 2
 },
 {
   showId: 2,
   countryCode: "--",
   restrictionTypeId: 1
 },
]

The object in JSON format above is included as an object in our shows index. Based on the object above, here are the use cases that must be implemented in the filter, note that the countryCode parameter will be provided upon using the algolia client.

We have this data in the database:

ShowId   CountryCode   RestrictionTypeId   StatusId
1             "PH"                 1            1
1             "US"                 1            1
1             "AE"                 1            1
2             "AE"                 1            1
3             "--"                 1            1
3             "--"                 2            1

Note: RestrictionTypeId 1 = allowed and RestrictionTypeId 2 = blocked

So we have this logic in the backend using C# code:

supposing OnlineAllowedCountries and OnlineBlockedCountries are List<string> of countryCode

public bool IsOnlineAllowed(string countryCode)
        {
            bool isAllowed = true;

            // check if explicitly allowed
            if ((OnlineAllowedCountries.FirstOrDefault(c => String.Compare(c.CountryCode, countryCode, true) == 0 && c.StatusId == 1) != null) || (OnlineAllowedCountries.FirstOrDefault(c => String.Compare(c.CountryCode, "--", true) == 0 && c.StatusId == 1) != null))
                isAllowed = true;
            else
            {
                // check if explicitly blocked
                if ((OnlineBlockedCountries.FirstOrDefault(c => String.Compare(c.CountryCode, "--", true) == 0 && c.StatusId == 1) != null) || (OnlineBlockedCountries.FirstOrDefault(c => String.Compare(c.CountryCode, countryCode, true) == 0 && c.StatusId == 1) != null))
                    isAllowed = false;
                else if (OnlineAllowedCountries.FirstOrDefault(c => c.StatusId == 1) != null && OnlineBlockedCountries.FirstOrDefault(c => c.StatusId == 1) == null)
                    isAllowed = false;
            }

            return (isAllowed);
        }

Currently, I perform a logic to ingest the data to match the index structure shown above.

My question is, how do we implement this in filter? Will it be ok to include multiple filters?

Or is it also best to formulate the logic before I ingest the data to algolia index for simpler filter?

Thank you very much!

You can use multiple filters but here are some limitations:

  • you can have ANDs of ORs but not ORs of ANDs
  • you can have a disjunction (OR) of different filters
  • you can only negate an individual filter

If you find yourself in those limitations, then you’ll need to precompute at indexing time an attribute that already contains the logic and can be use with filter.

I still don’t understand this part: “Searching ShowId 2 should only return result from country “PH”, this applies if showId has “yes” value on allowed field to atleast one country but no “no” value present on allowed field”.

If the results can vary based on other record, I think it would be better to precompute it.

Also is the country selectable by the user or they are just searching for showId by typing 1,2…3?

Having the UI that you are developing would help me know what it’s better for you to use.

Hi Marie,

Thank you for this! Actually, I changed the implementation of the indices and made it simpler while also applying the filter rules in the query.

Thank you very much!