Skip to main content
Sometimes, specific terms can act as cues that you can use to filter search results. You can use positive, negative, or numerical filters:
  • Positive filters include a specific subset of matching records in the results. For example, if a user types “diet” on a restaurant website, return every record that has “low-carb” or “low-fat”.
  • Negative filters exclude matching records from results. For example, if a user types “gluten-free” on a restaurant website, you could filter out any gluten-containing meal.
  • Numerical filters convert text queries into a numerical range. For example, if a user types “cheap” on a website for kitchen appliances, you could filter out anything costing more than $50.

Positive filters

If you want to filter out every non-diet-friendly meal whenever user’s search queries contain the term “diet”, you could use the _tags attribute to categorize meals depending on their individual qualities:
JSON
[
  {
    "name": "Chicken Stuffed Baked Avocados",
    "restaurant": "The Hive",
    "_tags": ["low-carb"]
  },
  {
    "name": "Spinach Quiche",
    "restaurant": "Bert's Inn",
    "_tags": ["low-carb", "vegetarian"]
  },
  {
    "name": "Pizza Chicken Bake",
    "restaurant": "Millbrook Deli",
    "_tags": ["cheese"]
  },
  {
    "name": "Strawberry Sorbet",
    "restaurant": "The Hive",
    "_tags": ["low-fat", "vegetarian", "vegan"]
  }
]
When users include the term “diet” in their search, you want to automatically return every record that has “low-carb” or “low-fat” in their _tags attribute. Because _tags is already optimized for filtering, you don’t have to set it as an attribute for faceting. You can directly create a new Rule that detects the term “diet” in a query and applies a positive filter on tags “low-carb” and “low-fat”.
To use the term “diet” only for filtering and not as a search term, add a consequence in your rule to remove the word from your query.

Using the API

To add a rule, use the saveRule method. When creating a rule, you must define a condition and a consequence.
var response = await client.SaveRuleAsync(
  "ALGOLIA_INDEX_NAME",
  "diet-rule",
  new Rule
  {
    ObjectID = "diet-rule",
    Conditions = new List
    {
      new Condition { Pattern = "diet", Anchoring = Enum.Parse("Contains") },
    },
    Consequence = new Consequence
    {
      Params = new ConsequenceParams
      {
        Filters = "'low-carb' OR 'low-fat'",
        Query = new ConsequenceQuery(
          new ConsequenceQueryObject
          {
            Edits = new List
            {
              new Edit { Type = Enum.Parse("Remove"), Delete = "diet" },
            },
          }
        ),
      },
    },
  }
);

Using the dashboard

You can also add rules from the Algolia dashboard.
  1. Go to the Algolia dashboard and select your Algolia application.
  2. On the left sidebar, select Search.
  3. Select your Algolia index.
  4. On the Rules page, click Create your first rule or New rule and select Manual Editor.
  5. In the Condition(s) sections, keep Query contains and enter “diet” in the input.
  6. In the Consequence(s) section:
    1. Click the Add consequence button and select Add Query Parameter.
    2. In the input, enter the JSON search parameter you want to add. For example: { "filters": "'low-carb' OR 'low-fat'" }.
    3. Click the Add consequence button again and select Remove Word.
    4. Type or select “diet” in the input field.
  7. Save your changes.

Negative filters

To exclude gluten-containing foods from the search results, when a user searches for gluten-free meals: To do this:
  1. Create an allergens attribute (with “gluten” as one of the potential values).
  2. Create a rule that filters out records with “gluten” in that attribute.

Example records

JSON
[
  {
    "name": "Pasta Bolognese",
    "restaurant": "Millbrook Deli",
    "allergens": ["eggs", "lactose"]
  },
  {
    "name": "Breakfast Waffles",
    "restaurant": "The Hive",
    "allergens": ["gluten", "lactose"]
  }
]

Using the API

Set allergens as an attributesForFaceting in your index:
var response = await client.SetSettingsAsync(
  "ALGOLIA_INDEX_NAME",
  new IndexSettings { AttributesForFaceting = new List { "allergens" } }
);
  • Use the saveRule method to create a rule that detects the term “gluten-free” in a query and applies a negative filter on facet value allergens:gluten.
  • Add a consequence in your rule to remove the word “gluten-free” from your query. This way, it won’t be used as a search term, only for filtering purposes.
var response = await client.SaveRuleAsync(
  "ALGOLIA_INDEX_NAME",
  "gluten-free-rule",
  new Rule
  {
    ObjectID = "gluten-free-rule",
    Conditions = new List
    {
      new Condition { Pattern = "gluten-free", Anchoring = Enum.Parse("Contains") },
    },
    Consequence = new Consequence
    {
      Params = new ConsequenceParams
      {
        Filters = "NOT allergens:gluten",
        Query = new ConsequenceQuery(
          new ConsequenceQueryObject
          {
            Edits = new List
            {
              new Edit { Type = Enum.Parse("Remove"), Delete = "gluten-free" },
            },
          }
        ),
      },
    },
  }
);

Using the dashboard

You can also add rules from the Algolia dashboard.
  1. Go to the Algolia dashboard and select your Algolia application.
  2. On the left sidebar, select Search.
  3. Select your Algolia index and open the Configuration tab.
  4. On the Filtering and Faceting > Facets page, click Add an attribute and select the allergens attribute.
  5. On the Rules tab, click Create your first rule or New rule and select Manual Editor.
  6. In the Condition(s) section, keep Query toggled on, select Contains, and enter gluten-free in the input.
  7. In the Consequence(s) section:
    1. Click the Add consequence button and select Add Query Parameter.
    2. In the input field that appears, enter the JSON search parameter you want to add. For example: { "filters": "NOT allergens:gluten" }
    3. Click the Add consequence button again and select Remove Word.
    4. Enter gluten-free in the input.
  8. Save your changes.

Numerical filters

Consider the query “cheap toaster 800w”. You can use Rules to filter the results by “toaster” and “prices between 0 and 25” so that the only textual search is the remaining term, “800w”, which could further be used to limit the results with that wattage.

Rule

If query = "cheap toaster" then price < 10 and type=toaster.
This requires two rules.

Using the API

var response = await client.SaveRuleAsync(
  "ALGOLIA_INDEX_NAME",
  "cheap",
  new Rule
  {
    ObjectID = "cheap",
    Conditions = new List
    {
      new Condition { Pattern = "cheap", Anchoring = Enum.Parse("Contains") },
    },
    Consequence = new Consequence
    {
      Params = new ConsequenceParams
      {
        Query = new ConsequenceQuery(
          new ConsequenceQueryObject { Remove = new List { "cheap" } }
        ),
        Filters = "price < 10",
      },
    },
  }
);

Using the dashboard

Since there are two rules, you must set up both separately.

Preparation

  1. Go to the Algolia dashboard and select your Algolia application.
  2. On the left sidebar, select Search.
  3. Select your Algolia index and open the Configuration tab.
  4. On the Filtering and Facets page, click Add an attribute and select the product_type attribute.

For the first rule

  1. Go to the Rules in the Algolia dashboard.
  2. Select Create your first rule or New rule and select Manual Editor.
  3. In the Condition(s) section, keep Query toggled on, select Contains, and enter toaster in the input.
  4. In the Consequence(s) section:
    1. Click Add consequence and select Add Query Parameter.
    2. In the input, add the JSON parameters you want to apply when the user’s query matches the Rule: { "filters": "product_type:toaster" }
    3. Click Add consequence again and select Remove Word.
    4. Enter “toaster” in the input.
  5. Save your changes.

For the second rule

  1. On the Rules page, click New rule and select Manual Editor.
  2. In the Condition(s) section, keep Query toggled on, select Contains, and enter cheap in the input.
  3. In the Consequence(s) section:
    1. Click Add consequence and select Add Query Parameter.
    2. In the input, add the JSON parameters you want to apply when the user’s query matches the Rule: { "filters": "price<10" }
    3. Click Add consequence again and select Remove Word.
    4. Enter cheap in the input.
  4. Save your changes.

See also

I