HomeDev guideAPI ReferenceGraphQL
Dev guideUser GuideGitHubNuGetDev CommunitySubmit a ticketLog In
GitHubNuGetDev CommunitySubmit a ticket

Return Optimizely objects

Describes methods that return Optimizely (Episerver) objects (instead of deserialized objects) from the Optimizely Search & Navigation search index.

When searching with the general .NET API, search queries usually execute using the GetResult method, which returns 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. DeserializingPageData objects from the index do not work out-of-the-box.

By retrieving only a reference to matching objects and then fetching them from DataFactory, you can be confident that they contain the 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:

  1. Create a search query for IContent objects or UnifiedFile objects.
  2. 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.

Language handling

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()

Caching

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 CMS master cache key. It clears the cache whenever it saves or deletes CMS content, similar to the CMS output cache.

📘

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.

Access the actual search results

GetContentResult and GetFilesResult return instances of a type that 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 and the actual objects from the CMS's API. Sometimes, you need to use actual search results (of type SearchResults), to track statistics. ContentResult and FilesResult expose actual search results through the SearchResult property.