The REST API and .NET client API support filtering on complex objects of different types to varying degree. Supported complex objects can be divided into three types:
- Collections of native objects such as IEnumerable<int>.
- Complex objects that have properties of native objects and/or complex objects. Such as an Author class.
- Collections of complex objects, such as IEnumerable<Author>.
The first type is covered by the documentation for each native type, for instance if you want to know how to filter on a property of type IEnumerable<string> see Strings. The second type, complex objects, such as a property of type Author is straight forward, we can filter on its native properties just as if they were apart of the type we are searching for. For instance, to find blog posts authored by Winston Churchill we could use the below code.
var searchQuery = client.Search<BlogPost>() .Filter(x => x.Author.Name.Match("Winston Churchill"));
One might wonder what happens if the Author property is null. Contrary to LINQ no exception will be thrown and the filter will simply not match.
The final type of complex objects that we can filter on is collections of complex objects, such as IEnumerable<Author>. For this type things start to get a little tricky. The .NET client API comes with a filter method named MatchContained which we can use to filter on properties of native types in objects contained in collections. Imagine that the BlogPost class no longer has an Author property but instead an Authors property of type IEnumerable<Author>. We can then use the MatchContained method to find blog posts whose Author property contains an Author with a specific name. Like this:
var searchQuery = client.Search<BlogPost>() .Filter(x => x.Authors.MatchContained( a => a.Name, "Winston Churchill"));
Note that the syntax for MatchContained is a bit different than other filtering methods. The first argument is an expression identifying the property that we want to filter on and the second is the value.
Also note that while the MatchContained method allows us to filter on a single field in an object in a list we cant use it to express multiple conditions on the same object in the list. That is, we can use it to find blog posts that have an author named Winston Churchill and that have an author with a specific e-mail address, but we cant use it to find blog posts with a specific author that both have a specific name and a specific e-mail address.