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 boolean and nullable boolean fields in a couple of ways. Below is a list of use cases and examples illustrating the available methods.

Existence

To search for documents where a boolean field has a value, we can use the Exists method. The below search will find blog posts that has an Approved property with a value. In other words, the below code is similar to the LINQ query Where(x => x.Approved.HasValue).

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

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

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

Matching true or false

For exact matching we can use the Match method. The below search will match blog posts whose Approved property is true. The LINQ equivalent would be Where(x => x.Approved).

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

Last updated: Sep 21, 2015

Recommended reading