HomeDev GuideAPI Reference
Dev GuideAPI ReferenceLegal TermsDev CommunityOptimizely AcademySubmit a ticketLog In
Dev Guide

Filters

Describes how to use filters in Optimizely Search & Navigation.

Filters narrow down search results, limiting a free-text search for books to a specific category or for database-like queries.

Search requests consist primarily of queries and filters. Queries and filters are similar. Filters perform better but do not affect scoring (sorting by relevance).

The .NET client API supports most filter types supported by the REST API and a fluent API for common filtering operations.

Assume a BlogPost class with a Title string property. The following code finds BlogPosts titled Find.

var searchQuery = client.Search<BlogPost>()
  .Filter(x => x.Title.Match("Find"));

The Search<TSource> method returns an instance of ITypeSearch<TSource>, which acts as a builder class for a search request. Using the Filter extension method, you can add a filter to the search request by supplying an expression that returns an instance of the Filter class. The client API has several extension methods for value types and strings that return filters, such as the Match method above.

Group filters

You can group filters using Boolean operators. For example, the following code finds blog posts titled Find or Search.

var searchQuery = client.Search<BlogPost>()
  .Filter(x => x.Title.Match("Find") 
             | x.Title.Match("Search"));

Assuming the BlogPost class has a Tag property, the following code finds blog posts titled Search and tagged with Find, or titled Find (with no requirements for tags).

var searchQuery = client.Search<BlogPost>()
  .Filter(x => (x.Title.Match("Search")
              & x.Tags.Match("Find")) 
              | x.Title.Match("Find"));

Call Filter multiple times

You can call the filter method numerous times, which is useful at runtime if you determine the filters you want to apply, such as filters that depend on user input. For example, you can modify the first part of the filter expression above.

var searchQuery = client.Search<BlogPost>()
  .Filter(x => x.Title.Match("Search"))
  .Filter(x => x.Tags.Match("Find"));

OrFilter method

If you invoke the Filter method multiple times, the latter invocations' filters are combined with previous invocations' filters using AND. To combine them using OR, use the following code.

var searchQuery = client.Search<BlogPost>()
  .Filter(x => x.Title.Match("Search"))
  .Filter(x => x.Tags.Match("Find"))
  .OrFilter(x.Title.Match("Find"));

If the OrFilter method is called before the Filter method, it is the same as calling the Filter method, because there is no existing filter to OR-group it.

Complex and dynamically created filters

Sometimes, the Filter and OrFilter methods are not enough. For example, you build complex search queries dynamically at runtime based on user input. While the methods can handle most scenarios at compile time, creating multiple grouped Orfilters at runtime is tricky and results in complex code. To build filters without regard to other filters and apply them to a search query, using the IClient extension method BuildFilter.

The BuildFilter method requires a generic type parameter to which you apply the filter. The method returns an object of type FilterBuilder. The FilterBuilder class has And and Or methods, which you use to add filter expressions like the Filter and OrFilter methods. The FilterBuilder class is implicitly convertible to the Filter class so that you can pass instances of it as arguments to the Filter and OrFilter methods and in filter expressions.

For example, you manage a restaurant search that uses two variables: cuisines and countries of type IEnumerable<string>. Each may be null, but if not, you want to add a filter so that search results return only restaurants matching those cuisines or countries. Use the BuildFilter method to accomplish this.

var q = ...some search string

ITypeSearch<Restaurant> query = client.Search<Restaurant>().For(q);

if (cuisines != null) {
  var cuisineFilter = client.BuildFilter<Restaurant>();
  foreach(var cuisine in cuisines) {
    cuisineFilter = cuisineFilter.Or(x => x.Cuisine.Match(cuisine));
  }
  query = query.Filter(cuisineFilter);
}

if (countries != null) {
  var countryFilter = client.BuildFilter<Restaurant>();
  foreach(var country in countries) {
    countryFilter = countryFilter.Or(x => x.Country.Match(country));
  }
  query = query.Filter(countryFilter);
}

var results = query.GetResult();

Filter methods

When filtering with the Filter method, you provide an expression that returns a filter. To do so, use extension methods for properties that return filters. The above examples used the Match extension method, but several other methods exist.

You can also create custom extension methods to enhance the filtering functionality.