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

Boolean

Filter search results based on Boolean fields, with the Filter method in Optimizely Search & Navigation .

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

You can use the Filter method to filter Boolean and nullable Boolean fields in a couple of ways, as shown in the following use cases and examples.

Existence

To search for documents where a Boolean field has a value, use the Exists method. The following search finds blog posts with an Approved property with a value. The below code is similar to the LINQ query Where(x => x.Approved.HasValue).

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

You can negate the filter with an exclamation point (!). To find blog posts that lack an Approved property, use the following code.

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

Match true or false

For exact matching, use the Match method. The following search matches blog posts whose Approved property is true. The LINQ equivalent is Where(x => x.Approved).

var searchQuery = client.Search<BlogPost>()
    .Filter(x => x.Approved.Match(true));