Use filters to narrow down search results, or use them for database-like queries.
The REST API and .NET client API support varying degrees of filtering on complex objects of different types. Supported complex objects can be divided into these types:
- Collections of native objects such as
- Complex objects that have properties of native objects and complex objects, for example, an
- Collections of complex objects, such as
The documentation covers the first type 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 straightforward. You can filter on its native properties as if they were a part of the type you are searching for. For instance, to find blog posts authored by Winston Churchill, you 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 is thrown. The filter does not match.
The final type of complex objects that you can filter is a collection of complex objects, such as
IEnumerable<Author>. The .NET client API has a filter method named MatchContained, which you can use to filter on properties of native types in objects in collections. Imagine that the
BlogPost class no longer has an
Author property but instead an
Authors property of type
IEnumerable<Author>. Next, use the
MatchContained method to find blog posts whose
Author property contains an
Author with a specific name.
var searchQuery = client.Search<BlogPost>() .Filter(x => x.Authors.MatchContained(a => a.Name, "Winston Churchill"));
MatchContainedsyntax is a bit different from other filtering methods. The first argument is an expression identifying the property that we want to filter on. The second argument is the value.
Also, while the
MatchContained method lets you filter on a single field in an object in a list, you cannot use it to express multiple conditions on the same object in the list. Use it to find blog posts with an author named Winston Churchill and an author with a specific email address. You cannot use it to find blog posts with an author's exact name and email address.
Updated 5 days ago