November Happy Hour will be moved to Thursday December 5th.

Loading...
Area: Optimizely Search & Navigation
ARCHIVED This content is retired and no longer maintained. See the latest version here.

Recommended reading 

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 Episerver pages and files stored in the VPP folder, you often want Episerver objects (such as PageData objects) returned from 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, the Episerver 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.

Example

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.
C#
SearchClient.Instance.Search<IContent>()
  .For("banana")
  .GetContentResult();

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

If you don't want the whole IContent or UnifiedFile objects but rather a subset of their content, perhaps with highlighting, create a projection using the Select method (described in 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 Episerver LanguageSelector.Autodetect() method. To select pages from a different language branch, use an overload accepting a LanguageSelector instance.

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

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 Episerver master cache key. This means that the cache is cleared whenever 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. 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.

Do you find this information helpful? Please log in to provide feedback.

Last updated: Nov 16, 2015

Recommended reading