HomeDev GuideRecipesAPI ReferenceGraphQL
Dev GuideAPI ReferenceUser GuideGitHubNuGetDev CommunityDoc feedbackLog In
GitHubNuGetDev CommunityDoc feedback


Nested queries in Optimizely Search & Navigation let you query an object and filter it on a collection marked as "nested." This is particularly useful in solutions with Optimizely Customized Commerce and Optimizely Search & Navigation, to manage search queries in large catalog structures.

## Nested field and types

Note

Nested queries are only allowed on _complex types_. If you want to query a value type or a string collection, use the Match or Exist filter.

A nested field is defined as a list of complex types (`IEnumerable<TListItem>` where `TListItem:class`) and that allows for querying the parent object using filters matching individual items in the list (that is, query for all **Teams** having a **Player** with first name 'Cristiano' and last name 'Ronaldo').

### Add fields as nested types

To add fields as nested, use one of these conventions, depending on your requirements:

**Example:** Nested property on a class, for example Team.



**Example:** Nested property on an interface, for example, `IPlayers` (all implementations of this interface are marked as nested).



**Example:** Nested method/extension on a class, for example `ForeignPlayers()` on Team.



## Search

Assume we have the following types (`Team` and `Player`).



To query on a nested object property use the nested `InField()`-extension (for example query for all players having first name 'Cristiano'):



or by a native property and a nested object property (for example query for team name or player first or last name):



### Filter

To filter on nested object properties use the nested `Filter()`-extension (i.e. filter for all teams having a player with first name 'Cristiano' and last name 'Ronaldo').



Or the `MatchItem()`-extension.



## Sort

To sort by nested properties, use the `OrderBy()` extension and sort by specifying the nested object property to sort on and an optional filter to filter out objects in the list (that is, order teams by players last name for players with first name 'Cristiano').



or



For `int/DateTime`, specify a `SortMode (Min/Max/Avg/Sum)` to determine how to treat multiple sort values. For example, to sort by maximum player salary on a team,



## Facets

To create facets on nested object properties use the `TermsFacetFor/HistogramFacetFor/DateHistogramFacetFor`-extensions by specifying a nested field and a property on the nested object (i.e. facet for first names of the players).



or, using an optional filter to filter out nested objects (i.e. facet for first names for players having last name 'Ronaldo')...



and fetch the result.



To create multiple facets on the same nested field and property, a custom facet name can be used...



and fetch the result.