HomeDev GuideAPI Reference
Dev GuideAPI ReferenceUser GuideGitHubNuGetDev CommunitySubmit a ticketLog In
GitHubNuGetDev CommunitySubmit a ticket

Strings

Describes how to filter search results based on strings, with the Filter method in Optimizely Search & Navigation.

Use filters to narrow down search results, or use them for database-like queries. 

Use the Filter method to filter string fields in several ways. Below are use cases and examples illustrating the methods.

Existence

To search for documents where a string field has a value, use the Exists method. The following search finds blog posts that have a title. The following code is similar to the LINQ query Where(x => x.Title != null).

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

Like other filters, it can be negated using an exclamation point (!). For example, use the following code to find blog posts that lack a title.

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

Exact match

For exact matching, use the Match method. The following search matches blog posts titled Find but not those titled find, fInd, Find rocks! or  Hello Find. The LINQ equivalent is Where(x => x.Title == Find).

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

Case-insensitive match

For exact matching without regard to case, use the MatchCaseInsensitive method. The following search matches blog posts titled Find, find and fInd but not those titled Find rocks! or Hello Find. The following search is similar to the LINQ query Where(x => x.Title.ToLower() == Find.ToLower()).

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

Match by beginning of a string (startsWith)

The Prefix method lets you match by the beginning of a string. The following search matches blog posts titled Find and Find rocks! but not find, Finding, or Hello Find.

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

Use the PrefixCaseInsensitive method to match by the beginning of a string in a case-insensitive way. The following search matches blog posts titled Find, Find rocks! and Find but not Finding or Hello Find.

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

Match by beginning of any word (wildcard, autocomplete)

In some situations, such as when building autocomplete or search-as-you-type functionality, you want to filter whether any word in a string starts with a certain string. Achieve this with the AnyWordBeginsWith method.

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

The above query matches blog posts titled Banana, banana,Banana split, or Yellow bananas. It does not match blog posts titled Abandon because no word in that title starts with ban (although it contains those characters).

📘

Note

While the Match and Prefix methods are case-sensitive and have case-insensitive counterparts, the AnyWordBeginsWith method is not case-sensitive and has no case-sensitive counterpart.

Match by range

The InRange method lets you filter on string fields that match a specific range. For instance, the following search matches blog posts titled A, Aa, and B but not those titled Bb or C.

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

Match by a set of values

The In method filters on string fields that match an explicit list of values. For instance, the following search matches blog entries titled A, B, and C.

var searchQuery = client.Search<BlogPost>()
  .Filter(x => x.Title.In(new List<string> { "A", "B", "C" }));

Filter on string collections

You also can use the Exists, Match, In, and MatchCaseInsensitive methods for properties that implement IEnumerable<string>. The following search uses Exists and matches blog posts with at least one tag.

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

The following search matches any blog post with the tag Find. It does not limit the results to only blog posts tagged with Find.

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