Introduction

The ContentService class includes a very powerful search functionality which can be used not only for a regular search of pages on a web site, but for any type of content search in the system. The search is bases on a ContentQuery which has properties like OrderBy, Depth and Text. The search result is returned as a PagedList<Content> which is a paged list of content items.

As an exmaple, you can use the Search function to get all pages of the content type News published between two specified dates and containing the phrase "customer". More examples below.

Examples

Example 1. Search for pages with the phrase "customer"

ContentQuery query = new ContentQuery();
query.Text = "customer"
PagedList<Content> result = ContentService.Search(query);

Example 2. Search for all pages of content type MyProject.ContentType.News order by created date

ContentQuery query = new ContentQuery();
query.ContentTypes.Add(ContentTypeService.Get<MyProject.ContentType.News>());
query.OrderBy.Add(new SortItem(ContentColumn.Created, SortDirection.Decending));
PagedList<Content>result = ContentService.Search(query);
            

Example 3. Search for all Products containing the phrase "car". Set the page size to 10 results and get the first page.

ContentQuery query = new ContentQuery();
query.Text = "car*";
query.ContentTypes.Add(ContentTypeService.Get<MyProject.ContentTypes.Product>());
query.PageSize = 10;
query.PageIndex = 0;
PagedList<Content> result = ContentService.Search(query);

Example 4. Search for all pages containing the phrase "car". The pages must be a descendant to the page with id 25.

ContentQuery query = new ContentQuery();
query.ParentID = 25;
query.Depth = 10;
query.Text = "car*";
PagedList<Content> result = ContentService.Search(query);

Example 5. Search for pages with a metadata value which is product = 1.

ContentQuery query = new ContentQuery();
query.MetaDataValue = new PersistedValue() { Name = "product", Type = typeof(string), Value = "1" };
PagedList<Content> result = ContentService.Search(query);

ContentQuery

The ContentQuery class contains the following properties that are useful when executing a search. For a full list of properties and methods, please visit http://help.lemoon.com/ref/core/10/

Property Description 
ContentTypes Gets or sets the ContentType objects to search, an empty list (the default) means search all content types. 
CreatedByID Finds Content created by the specified User, null to ignore the CreatedBy property. Default is null.  
Depth Gets or sets the depth of the search. The default value is null which searches the entire content database. To search only root items set Depth to 0. To search among a specific item and it's immediate children set a ParentID and set Depth to 1.  
FromDate 
Gets or sets a date that limits the result to items created on or after this date.  
ToDate 
Gets or sets a date that limits the result to items created on or before this date.  
LanguageID  Gets or sets the language ID of the Site to search, when LanguageID is null, all sites and languages are searched. Default value is the current site.
MetaDataValue Gets or sets a meta data property to search for. 
OrderBy Gets or sets the sort order of the result 
PageIndex Gets or sets the page index at which to start reading. 
PageSize Gets or sets the page size of the result set (the number of items to return). When PageSize = 0, the result is not paged. 
ParentIDGets or sets the parent item in the content tree to search from. The default value is null which searches the entire content database 
PropertyValue Gets or sets a content property to search for 
Text Gets or sets the full text search expression used in the query. The query can contain single words or phrases, e.g. (stockholm and "baseball cap"), and search for part of words by using * (e.g. "car*" will return a match for both "car" and "carpark"). The query supports AND, AND NOT, OR and NEAR operators. 


There are no comments on this page.
   
 

Customize this section

The widgets presented in this column can be added, removed and sorted - just click the icons to the right of the caption.
 
     
   
 

Working copies