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 folder you 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, de-serializing 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, you can be confident that they hold the very latest data from the database and you will also be able to update or delete them should you want to. The integration contains two extension methods that handles this process for us, GetContentResult and GetFilesResult.
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.
SearchClient.Instance.Search<IContent>() .For("banana") .GetContentResult(); SearchClient.Instance.Search<UnifiedFile>() .For("banana") .GetFilesResult();
For scenarios where you don't want the whole IContent or UnifiedFile objects but rather a subset of their content, perhaps with highlighting you 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 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.
SearchClient.Instance.Search<IContent>() .For("banana") .GetContentResult(new LanguageSelector("sv"));
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 is cleared whenever an EPiServer content is saved or deleted. This is similar to how the EPiServer output cache works.
Note that the cache key is generated from the query meaning that if you use queries containing dates, you should normalize dates to minutes or hours, as you 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 you 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.