Introduction
This document describes the built-in search options in EPiServer CMS.
Content searches come in the following distinct forms:
- Full-text search
- Property-based search
Full-Text Search
Information about CMS content (pages, blocks and files) are stored in a searchable index. In order to find items in the index you can do so by composing search queries, from different sub queries. Here are two samples on how you could perform searches in that index.
How to search for pages
C#
public class PageSearchSample : TemplatePage
{
public IEnumerable<PageData> FindPages(string searchQuery, ContentReference searchRoot, string cultureId, int pagingNumber, int pagingSize)
{
GroupQuery groupQuery = new GroupQuery(LuceneOperator.AND);
groupQuery.QueryExpressions.Add(new ContentQuery<PageData>());
groupQuery.QueryExpressions.Add(new FieldQuery(searchQuery));
VirtualPathQuery pathQuery = new VirtualPathQuery();
pathQuery.AddContentNodes(searchRoot, Locate.ContentLoader());
groupQuery.QueryExpressions.Add(pathQuery);
AccessControlListQuery aclQuery = new AccessControlListQuery();
aclQuery.AddAclForUser(PrincipalInfo.Current, HttpContext.Current);
groupQuery.QueryExpressions.Add(aclQuery);
groupQuery.QueryExpressions.Add(new FieldQuery(cultureId, Field.Culture));
SearchResults results = Locate.SearchHandler().GetSearchResults(groupQuery, pagingNumber, pagingSize);
ContentSearchHandler contentSearchHandler = Locate.ContentSearchHandler();
foreach (var hit in results.IndexResponseItems)
{
yield return contentSearchHandler.GetContent<PageData>(hit);
}
}
}
How to search for files
C#
public class FileSearchSample : TemplatePage
{
public IEnumerable<string> FindFiles(string searchQuery, VersioningDirectory searchRoot, int pagingNumber, int pagingSize)
{
GroupQuery groupQuery = new GroupQuery(LuceneOperator.AND);
groupQuery.QueryExpressions.Add(new UnifiedFileQuery());
groupQuery.QueryExpressions.Add(new FieldQuery(searchQuery));
VirtualPathQuery pathQuery = new VirtualPathQuery();
pathQuery.AddDirectoryNodes(searchRoot);
groupQuery.QueryExpressions.Add(pathQuery);
AccessControlListQuery aclQuery = new AccessControlListQuery();
aclQuery.AddAclForUser(PrincipalInfo.Current, HttpContext.Current);
groupQuery.QueryExpressions.Add(aclQuery);
SearchResults results = Locate.SearchHandler().GetSearchResults(groupQuery, pagingNumber, pagingSize);
foreach (var hit in results.IndexResponseItems)
{
yield return hit.Uri.ToString();
}
}
}
How CMS types are stored in the index
CMS types are stored in the index by mapping values from the objects onto an IndexRequestItem, which is then sent to the indexing service. Two of the most common indexed CMS types that are stored in the index are PageData and VersioningFile.
When a PageData is stored in the index its properties are mapped to the fields on the index request item in the following way.
IndexRequestItem item | PageData 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 a VersioningFile is stored in the index its properties are mapped to the fields in the index the following way.
IndexRequestItem item | VersioningFile 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 |
Full-Text Search - Limitations
Known limitations:
- A query based on pages will not generate hits in blocks that are added in a Content Area *
*) Supported in EPiServer Find
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 can also call the FindPagesWithCriteria() method of the EPiServer.DataFactory class to perform property-based searches. The FindPagesWithCriteria method is used by the SearchDataSource control when performing 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 are required to 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:
- Attributes MaxCount, SortOrder, EnableVisibleInMenu, PageTypeID, etc.
- 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).
See Also
For detailed information about search features of the EPiServer platform, see the Search section in EPiServer Framework SDK.
Do you find this information helpful? Please log in to provide feedback.