This content is archived. See latest version here

Last updated: Jun 10 2014

Introduction

There are two ways to search over multiple types. The first is implicit: the IClient.Search<T>() method supports inheritance by default. In other words, no extra effort is required to search over multiple types that share a common base class.

It is also possible to first create a search request for a type and then include additional types in the search request. This is done using the IncludeType method. When doing so we must provide a projection from the additional type to the return type of the search request, either the original type or some projection. In other words, we can create a search request for BlogPosts and also include the Article type in the request like this:

C#
result = client.Search<BlogPost>.For("Banana")
    .IncludeType<BlogPost, Article>(x => 
        new BlogPost 
        { Title = x.Title, Content = x.MainBody })
    .GetResult();

In this case we will search in for both BlogPosts and Articles and retrieve the result as BlogPosts. However, in most situations it is preferable to project both types to a third type more suitable for search results, like this:

C#
result = searchQuery.For("Banana")
    .Select(x => new SearchResult 
        { Title = x.Title, Content = x.Content })
    .IncludeType<SearchResult, Article>(x => 
        new SearchResult 
        { Title = x.Title, Content = x.MainBody })
    .GetResult();

Limitations

While the IncludeType method can be used to include one or several additional types the search request is built towards the first type. This means that if we limit the search request to search in specific fields and none of those exists in the other types we'll only get hits from the first type. In practice however, when we search through all content without limiting the search request to specific fields, or when the types share the same name for fields this is not a problem.


Do you have feedback on this documentation? Send an email to documentation@episerver.com. For development-related questions and discussions, refer to our Forums on https://world.episerver.com/forum/