Filters are a great tools for hiding results that you don't want your users to see.
There are a few different ways to add a filter to your search. The best method to use will depend on why you are adding the filter as each method has different pros and cons:
Option 1 - With a rule:
How to do it: We have a guide for this here
Pros:
- It's very easy to create a rule with a filter
- No need to do any coding to apply the filter, all you need is the rule
Cons:
- Theoretically a user can turn rules off so filters added with rules should not be used for “secret” data that you need to hide for security/privacy reasons
Option 2 - With your API key:
How to do it: We have details on this here
Pros:
- The user cannot change the filter
Cons:
- You need to make sure that you always use this secured key in your front end
- Less flexible as all searches with the key will have the filter regardless of what index they are searching
- If you want to remove the filter you will need to make a new key without it
Option 3 - By adding the filter to the Search Parameters
How to do it: For back end search you can add the filter to the search as a search parameter. If you use InstantSearch on the front end you can use the Configure widget
Pros:
- Very flexible, you can apply the filter exclusively on the searches you choose
Cons:
- Requires a code change (wherever you make the search you need to add the filter)
- Users can search without the filter if the filter is not bound to the API key, so this should not be used to hide data that needs to be kept secret for security/privacy reasons