This content is retired. See latest version here.

Last updated: Nov 16 2015

How it works

When searching with the general .NET API search queries are usually executed using a method named GetResult which returns matching objects or projections from matching objects by de-serializing them from JSON. However, when executing a query for Episerver pages and files stored in VPP folder we often want the returned objects to be the Episerver objects, such as PageData objects, returned from the Episerver APIs and not objects deserialized from the index.

In fact, deserializing PageData objects from the index will not even work out-of-the-box. Instead, by only retrieving a reference to matching objects and them fetching them from for instance Episerver DataFactory, we can be confident that they hold the very latest data from the database and we will also be able to update or delete them should we want to. The integration contains two extension methods that handles this process for us, GetContentResult and GetFilesResult.

Examples

To use GetContentResult or GetFilesResult, simply create a search query either for IContent objects or UnifiedFile objects, and use these methods instead of the regular GetResult method to execute the query and retrieve the result.

C#
SearchClient.Instance.Search<IContent>()
  .For("banana")
  .GetContentResult();

SearchClient.Instance.Search<UnifiedFile>()
  .For("banana")
  .GetFilesResult();

For scenarios where we don't want the whole IContent or UnifiedFile objects but rather a subset of their content, perhaps with highlighting we can instead use the regular GetResult method after first having created a projection using the Select method described in the Searching section of this documentation.

Language handling

The GetContentResult method automatically adds a filter to the search request to select content from the current language branch as determined by the Episerver LanguageSelector.Autodetect() method. To select pages from a specific language branch, an overload accepting a LanguageSelector instance can be used.

C#
SearchClient.Instance.Search<IContent>()
  .For("banana")
  .GetContentResult(new LanguageSelector("sv"));

Caching

As opposed to the GetResult method which does not do any caching by default, the GetContentResult method automatically adds caching for a minute. However, to make sure that query results are not updated it adds query results to the cache with a dependency on the Episerver master cache key. This means that the cache will be cleared whenever an Episerver content is saved or deleted. This is similar to how the Episerver output cache works.

Note: The cache key is generated from the query meaning that if we use queries containing dates, we should normalize dates to minutes or hours, as we will not otherwise benefit from caching while filling up the cache with unused data. In other words, avoid filtering using DateTime.Now.

Accessing the actual search results

GetContentResult and GetFilesResult both return instances of a type which contain the matching objects, such as matching content objects. These types are ContentResult and FilesResult. This is accomplished by fetching the matching objects IDs from the search engine and then fetching the actual objects from the CMS' API. Sometimes we may need to use the actual search results (of type SearchResults), for instance in order to track statistics. Both ContentResult and ContentResult expose these through a property named SearchResult.

Comments