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:
- Create a search query for
GetFilesResultto 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
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
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
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 CMS master cache key. This means that the cache is cleared whenever CMS content is saved or deleted. This is similar to how the CMS output cache works.
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.
GetFilesResult return instances of a type which contain matching objects, such as matching content objects. These types are
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
FilesResult expose actual search results through the
Updated 8 days ago