How it works
When searching with the general .NET API, search queries are usually executed using a method named GetResult which returns, among other things, 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 folders, we often want the returned objects to be the Episerver objects, such as PageData objects, returned from the Episerver APIs rather than 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 then 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 Episerver CMS integration contains two extension methods that handles this process for us, GetPagesResult and GetFilesResult.
To use GetPagesResult or GetFilesResult, simply create a search query either for PageData objects or UnifiedFile objects and use these methods instead of the regular GetResult method to execute the query and retrieve the result.
SearchClient.Instance.Search<PageData>() .For("banana") .GetPagesResult(); SearchClient.Instance.Search<UnifiedFile>() .For("banana") .GetFilesResult();
For scenarios where we do not want the whole PageData 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.
The GetPagesResult method automatically adds a filter to the search request to select pages from the current language branch as determined by EPiServers LanguageSelector.Autodetect() method. To select pages from a specific language branch an overload accepting a LanguageSelector instance can be used.
SearchClient.Instance.Search<PageData>() .For("banana") .GetPagesResult(new LanguageSelector("sv"));
As opposed to the GetResult method which does not do any caching by default, the GetPagesResult method automatically adds caching for a minute. However, to make sure that query results are not stale (not updated), it adds query results to the cache with depending on the Episerver master cache key. This means that the cache will be cleared whenever an EPiServer page 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 you should normalize dates to minutes or hours, as you will not reap the benefits of caching while filling up the cache with unused data. In other words, avoid filtering using DateTime.Now.
Accessing the actual search results
GetPagesResult and GetFilesResult both return instances of a type which contain the matching objects, such as matching PageData objects. These types are PagesResult 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 PagesResult and PagesResult expose these through a property named SearchResult.