Try our conversational search powered by Generative AI!

Loading...
Area: Optimizely Search & Navigation
ARCHIVED This content is retired and no longer maintained. See the latest version here.

Recommended reading 

Introduction

The Filter method can be used to filter string fields in a number of ways. Below is a list of use cases and examples illustrating the available methods.

Existence

To search for documents where a string field has a value, we can use the Exists method. The below search will find blog posts that has a title. In other words, the below code is similar to the LINQ query Where(x => x.Title != null).

C#
var searchQuery = client.Search<BlogPost>()
    .Filter(x => x.Title.Exists());

Like all filters it can be negated using an exclamation mark (!). In other words, to instead find all blog posts that doesnt have a title we could use the below code.

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

Exact match

For exact matching we can use the Match method. The below search will match blog posts titled Find but not those titled find, fInd, Find rocks! or  Hello Find. The LINQ equivalent would be Where(x => x.Title == Find).

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

Case insensitive match

For exact matching disregarding casing we can use the MatchCaseInsensitive method. The below search will match blog posts titled Find, find and fInd but not those titled Find rocks! or Hello Find. In other words, the below search is similar to the LINQ query Where(x => x.Title.ToLower() == Find.ToLower()).

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

Matching by beginning of a string (startsWith)

The Prefix method allows to match by the beginning of a string. The below search will match blog posts titled Find and Find rocks! but not find, Finding or Hello Find.

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

To instead match by the beginning of a string in a case insensitive way we can use the PrefixCaseInsensitive method. The below search will match blog posts titled Find, Find rocks! and Find but not Finding or Hello Find.

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

Matching by beginning of any word (wildcard, autocomplete)

In some situations, such as when building autocomplete or search-as-you-type functionality, we want to filter on whether any word in a string starts with a certain string. This can be achieved by using the AnyWordBeginsWith method.

C#
var searchQuery = client.Search<BlogPost>()
    .Filter(x => x.Title.AnyWordBeginsWith("Ban"));

The above query will match blog posts titled Banana, banana, Banana split or Yellow bananas. It wont however match blog posts titled Abandon as theres no word in that title that starts with ban (although it does contain those characters). Note that while the Match and Prefix methods are case sensitive and have their case insensitive counterparts the AnyWordBeginsWith method isnt case sensitive and theres no case sensitive counterpart to it.

Matching by range

The InRange method allows to filter on string fields that match a specific range. For instance, the below search will match blog posts titled A, Aa, and B but not those titled Bb or C.

C#
var searchQuery = client.Search<BlogPost>()
    .Filter(x => x.Title.InRange("A", "B"));

Filtering on string collections

The Exists, Match and MatchCaseInsensitive can also be used for properties the implement IEnumerable<string>. The below search that uses Exists will for instance match blog posts that has at least one tag.

C#
var searchQuery = client.Search<BlogPost>()
    .Filter(x => x.Tags.Exists());

And the below search will match any blog post that has the tag Find. Note that it will not limit the result to blog posts that only are tagged with Find though.

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

Last updated: Jun 10, 2014

Recommended reading