Hide menu Last updated: Sep 21 2015

Content searches come in the following distinct forms:

Full-text search

Episerver stores information about CMS content (pages and blocks) in a searchable index. To find items in the index, compose search queries from different subqueries. The following samples show how you can search in that index.

Note: File search is not supported by default when you use SearchDataSource.

How to search for pages

    public IEnumerable<PageData> FindPages(string searchQuery, ContentReference searchRoot, string cultureId, int pagingNumber, int pagingSize)
    {
        // The group query is needed to combine the different criteria
        GroupQuery groupQuery = new GroupQuery(LuceneOperator.AND);

        // The content query makes sure we only get hits that are of the type PageData
        groupQuery.QueryExpressions.Add(new ContentQuery<PageData>());

        // The field query contains the search phrase
        groupQuery.QueryExpressions.Add(new FieldQuery(searchQuery));

        // The virtual path query makes sure that we only get hits for children of the specified search root
        VirtualPathQuery pathQuery = new VirtualPathQuery();
        pathQuery.AddContentNodes(searchRoot);
        groupQuery.QueryExpressions.Add(pathQuery);

        // The access control list query will remove any pages the user doesn't have read access to
        AccessControlListQuery aclQuery = new AccessControlListQuery();
        aclQuery.AddAclForUser(PrincipalInfo.Current, HttpContext.Current);
        groupQuery.QueryExpressions.Add(aclQuery);

        // Add a specific field query for the Culture field in order to search for a specific culture
        groupQuery.QueryExpressions.Add(new FieldQuery(cultureId, Field.Culture));

var searchHandler = ServiceLocator.Current.GetInstance<SearchHandler>(); SearchResults results = searchHandler.GetSearchResults(groupQuery, pagingNumber, pagingSize); ContentSearchHandler contentSearchHandler = ServiceLocator.Current.GetInstance<ContentSearchHandler>(); foreach (var hit in results.IndexResponseItems) { // Use the content search handler to convert the page hit into a PageData yield return contentSearchHandler.GetContent<PageData>(hit); } }

How to search for files

    public IEnumerable<string> FindFiles(string searchQuery, VersioningDirectory searchRoot, int pagingNumber, int pagingSize)
    {
        // The group query is needed to combine the different criteria
        GroupQuery groupQuery = new GroupQuery(LuceneOperator.AND);

        // The unified file query makes sure we only get hits that are files
        groupQuery.QueryExpressions.Add(new UnifiedFileQuery());

        // The field query contains the search phrase
        groupQuery.QueryExpressions.Add(new FieldQuery(searchQuery));

        // The virtual path query makes sure that we only get hits for children of the specified search root
        VirtualPathQuery pathQuery = new VirtualPathQuery();
        pathQuery.AddDirectoryNodes(searchRoot);
        groupQuery.QueryExpressions.Add(pathQuery);

        // The access control list query will remove any files the user doesn't have read access to
        AccessControlListQuery aclQuery = new AccessControlListQuery();
        aclQuery.AddAclForUser(PrincipalInfo.Current, HttpContext.Current);
        groupQuery.QueryExpressions.Add(aclQuery);

var searchHandler = ServiceLocator.Current.GetInstance<SearchHandler>(); SearchResults results = searchHandler.GetSearchResults(groupQuery, pagingNumber, pagingSize); foreach (var hit in results.IndexResponseItems) { // Return the virtual path for each matching file. yield return hit.Uri.ToString(); } }

How CMS types are stored in the index

Episerver stores CMS types in the index by mapping values from the objects onto an IndexRequestItem, which is then sent to the indexing service. PageData and VersioningFile are two of the most common indexed CMS types that are stored in the index.

When you store a PageData in the index, its properties are mapped to the fields on the index request item in the following way.

IndexRequestItem itemPageData page
item.Id page.PageGuid|page.LanguageBranch
item.Uri page.LinkUrl
item.Title page.Name
item.Created page.Created
item.Modified page.Changed
item.Culture page.LanguageBranch
item.ItemType A comma separated list of the type of the page, and any inherited types
item.Authors page.CreatedBy
item.DisplayText Content from all searchable properties on the page
item.AccessControlList page.ACL, but only with regards to read access
item.Categories page.Category
item.VirtualPathNodes The ancestors to the page, ordered with the oldest first
item.ItemStatus Always approved
item.PublicationEnd page.StopPublish

When you store a VersioningFile in the index, its properties are mapped to the fields in the index the following way.

IndexRequestItem itemVersioningFile file
item.Id file.Guid
item.Title file.Name
item.DataUri file.LocalPath
item.Uri file.PermanentLinkVirtualPath or file.VirtualPath
item.Authors file.Summary.Author
item.Categories file.Summary.Category split by character ','
item.ItemStatus Always approved
item.Metadata file.Name without extension and all values from file.Summary.Dictionary
item.AccessControlList All users and roles with read access from file.Parent.ACL
item.VirtualPathNodes The guid of each parent directory ending with file.Guid
item.DisplayText The content of the file, if required IFilter is installed

Property-based search

Property-based search provides an efficient and powerful tool to retrieve pages based on any property values, typically of types other than string. From markup (or the visual design mode in Visual Studio), the Episerver SearchDataSource web control can support both types of searches; also different criteria can be applied to both types of searches.

From code-behind files, you also can call the FindPagesWithCriteria() method of the EPiServer.DataFactory class to perform property-based searches. The FindPagesWithCriteria method is used by the SearchDataSource control when you perform a property-based search.

From markup or visual design mode, add PropertyCriteria controls to the SearchDataSource tag. From code-behind files, create EPiServer.PropertyCriteria instances and insert them into a PropertyCriteriaCollection that is passed to the FindPagesWithCriteria method. You must add the SearchDataSource control's DataSourceID attribute to the control that renders the data.

Data can be sorted, filtered, and configured through markup or visual designer use:

  • MaxCount, SortOrder, EnableVisibleInMenu, PageTypeID, and other attritbutes.
  • LanguageBranches collection to limit the search.
  • Criteria collection, used for property search or as filters.
  • SelectParameters collection to set attribute values from other sources (Control, Cookie, Form, Profile, QueryString, Session, EPiServer Property).

Comments

It is very annoying when you just post your "Locate."  without any reference to the datatype you resolved.

Eric: Point taken, we'll update the code example.