This content is archived. See latest version here

Last updated: Nov 16 2015

Querying scenarios

In addition to providing great, near-real-time search, Episerver Find can also be used to solve other types of querying needs for which we traditionally had to use the FindPagesWithCriteria method for. Below is a number of examples illustrating common querying scenarios specific to Episerver CMS 6 R2. See also Searching and Filtering for more examples related to filtering.

Filtering by category

C#
int categoryId = 3; //Category to match

var pages = SearchClient.Instance.Search<PageData>()
  .Filter(x => x.Category.Match(categoryId))
  .GetPagesResult();

Filtering by Author/ChangedBy

C#
var pages = SearchClient.Instance.Search<PageData>()
  .Filter(x => x.ChangedBy.Match("Zlatan"))
  .GetPagesResult();

Filtering by publish date

The below example finds pages published in October 2011.

C#
var pages = SearchClient.Instance.Search<PageData>()
  .Filter(x => x.StartPublish.MatchMonth(2011, 10))
  .GetPagesResult();
To instead find all pages published prior to October 2011 the below query could be used.
C#
var pages = SearchClient.Instance.Search<PageData>()
  .Filter(x => x.StartPublish.Before(new DateTime(2011, 10, 1)))
  .GetPagesResult();

Filtering by page type

When using Page Type Builder, the easiest way to filter by page type is to simply specify the desired type in the type parameter to the Client Search method. However, in that case pages of page types inheriting from the specified type also are matched. In other situations we may not know what type to filter by until at runtime. In both such cases we can filter by the PageData.PageTypeID.

C#
int pageTypeId = 42;

var pages = SearchClient.Instance.Search<PageData>()
  .Filter(x => x.PageTypeID.Match(pageTypeId))
  .GetPagesResult();

Filtering by Site ID

In an enterprise scenario with multiple sites, all sites utilize the same index. To handle this the return value of an extension method for PageData, named SiteId, is indexed. This can be used to filter for pages on a specific site.

C#
var pages = SearchClient.Instance.Search<PageData>()
  .Filter(x => x.SiteId().Match("MySiteId"))
  .GetPagesResult();

Do you have feedback on this documentation? Send an email to documentation@episerver.com. For development-related questions and discussions, refer to our Forums on https://world.episerver.com/forum/