This content is archived. See latest version here.

Last updated: Jun 10 2014

Introduction

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 7/7.5 CMS. For more examples and documentation related to filtering, refer to the Searching/Filtering section under .NET API in this documentation.

Filtering by category

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

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

Filtering by Author/ChangedBy

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

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

Filtering by page type

When using typed content, the easiest way to filter by content 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 will also be 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 IContent.ContentTypeID.

C#
int contentTypeId = 42;

var pages = SearchClient.Instance.Search<IContent>()
  .Filter(x => x.ContentTypeID.Match(contentTypeId))
  .GetContentResult();

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 IContent, named SiteId, is indexed. This can be used to filter for content on a specific site.

C#
var content = SearchClient.Instance.Search<IContent>()
  .Filter(x => x.SiteId().Match("MySiteId"))
  .GetContentResult();

Comments

Also..  

  1. If you want to filter blocks by site id we need to cast to IContent otherwise the SiteId extension method (in EPiServer.Fin.Cms.ContentExtensions) is not available
  2. If we also need to include content which is available in all sites then we need to check for no/null site id against the content.

E.g.

.Filter(x => (x as IContent).SiteId().Match(SiteDefinition.Current.Id.ToString()) | !(x as IContent).SiteId().Exists()) 

Pls note that this returns on 10 result by default and you need to use .Take(pageSize) to have more than 10 results