Area: Optimizely Search & Navigation
Applies to versions: 12 and higher
Other versions:


Recommended reading 

Use filters to narrow down search results, or use them for database-like queries. This topic explains how to filter search results based on numerical fields, with the Filter method in Optimizely Search & Navigation. 

How it works

The Filter method can be used to filter numerical fields in a number of ways. Supported numerical types are int, double, float and long as well as their nullable equivalents. Below is a list of use cases and examples illustrating the available methods.


To search for documents where a numerical field has a value, use the Exists method. The following search finds blog posts that have an AuthorId property; it is similar to the LINQ query Where(x => x.AuthorId.HasValue).

var searchQuery = client.Search<BlogPost>()
    .Filter(x => x.AuthorId.Exists());

Like all filters, you can negate it with an exclamation mark (!); to instead find all blog posts without an AuthorId, use the following code.

var searchQuery = client.Search<BlogPost>()
    .Filter(x => !x.AuthorId.Exists());

Exact match

For exact matching, use the Match method. The following search matches blog posts with ID 42 but not those with ID 41 or 43. The LINQ equivalent is Where(x => x.Id == 42).

var searchQuery = client.Search<BlogPost>()
    .Filter(x => x.Id.Match(42));

Matching by range

To find documents with a numeric field that has a value within a given range, use the InRange method. The following search matches blog posts that have between zero and five comments; it matches blog posts that have one, two, three, four, or five comments but not those that have six or seven comments.

An equivalent in LINQ is Where(x => x.NumComments >= 0 && x.NumComments <= 5).

var searchQuery = client.Search<BlogPost>()
    .Filter(x => x.NumComments.InRange(0, 5));

For some numerical types, such as int, there are also the GreaterThan and LessThan methods, which make range filtering easier. For instance, given that NumComments cannot be negative, you can rewrite the previous search.

var searchQuery = client.Search<BlogPost>()
    .Filter(x => x.NumComments.LessThan(6));

Matching by a set of values

The In method allows filtering on int fields that match an explicit set of values. For instance, the following matches blog posts that have one, three, or five comments.

var searchQuery = client.Search<BlogPost>()
    .Filter(x => x.NumComments.In(new List<int> { 1, 3, 5 }));

Filtering on numerical collections

Use the Exists, In, and Match methods for properties that implement IEnumerable<int>. (Support for other numerical types is currently lacking but will be available in future versions.)

The following search, which uses Exists, matches blog posts that have at least one category ID.

var searchQuery = client.Search<BlogPost>()
    .Filter(x => x.CategoryIds.Exists());

The following search matches any blog post that has a category with ID 42. It does not, however, limit the result to blog posts that only have that category ID.

var searchQuery = client.Search<BlogPost>()
    .Filter(x => x.CategoryIds.Match(42));
Do you find this information helpful? Please log in to provide feedback.

Last updated: Oct 31, 2016

Recommended reading