This topic describes methods that return Optimizely (Episerver) objects (instead of deserialized objects) from the Optimizely Search & Navigation search index.
How it works
When searching with the general .NET API, search queries are usually executed using the GetResult method which returns, among other things, matching objects or projections from matching objects by deserializing them from JSON. However, when executing a query for Optimizely pages and files stored in the VPP folder, you often want Optimizely (Episerver) objects (such as PageData objects) returned from Optimizely (Episerver) APIs, not objects deserialized from the index. In fact, deserializing PageData objects from the index does not work out-of-the-box.
By retrieving only a reference to matching objects then fetching them from for instance DataFactory, you can be confident that they contain the very latest data from the database. You can also update or delete them if needed. Two extension methods handle this process: GetContentResult and GetFilesResult.
To use GetContentResult or GetFilesResult:
- Create a search query for IContent objects or UnifiedFile objects.
- Use GetContentResult or GetFilesResult to execute the query and retrieve the result.
SearchClient.Instance.Search<IContent>() .For("banana") .GetContentResult(); SearchClient.Instance.Search<UnifiedFile>() .For("banana") .GetFilesResult();
If you do not want the whole IContent or UnifiedFile objects but rather a subset of their content, perhaps with highlighting, create a projection using the Select method (see Projections), then use the regular GetResult method.
The GetContentResult method automatically filters search requests to select content from the current language, as determined by the LanguageSelector.Autodetect() method. To select pages from a different language branch, use an overload accepting a LanguageSelector instance.
SearchClient.Instance.Search<IContent>() .For("banana") .InLanguageBranch("sv") .GetContentResult()
As opposed to the GetResult method, which does no caching by default, the GetContentResult method automatically adds caching for a minute. However, to make sure that query results are not updated, GetContentResult adds query results to the cache with a dependency on the Optimizely master cache key. This means that the cache is cleared whenever Optimizely content is saved or deleted. This is similar to how the Optimizely output cache works.
Note: The cache key is generated from the query. This means that when using queries containing dates, normalize dates to minutes or hours. If you do not, you gain no 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 return instances of a type which contain matching objects, such as matching content objects. These types are ContentResult and FilesResult.
To accomplish this, fetch matching object IDs from the search engine then the actual objects from the CMS's API. Sometimes, you need to use actual search results (of type SearchResults), for instance to track statistics. Both ContentResult and FilesResult expose actual search results through the SearchResult property.
Last updated: Oct 31, 2016