Hide menu Last updated: Oct 31 2016
Area: Episerver Find Applies to versions: 12 and higher
Other versions:

Nested queries

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

In this topic

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'). 

Adding 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.

client.Conventions.NestedConventions.ForType<Team>().Add(team => team.Players);

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

 client.Conventions.NestedConventions.ForInstancesOf<IPlayers>().Add(team => team.Players);

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

client.Conventions.ForInstancesOf<Team>().IncludeField(team => team.ForeignPlayers()); // mark metod/extension as indexed
client.Conventions.NestedConventions.ForType<Team>().Add(team => team.ForeignPlayers());

Searching

Having the following types (Team and Player).

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

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

Filtering

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.

Sorting

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 (i.e 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, us

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.

Comments