EPiServer Find to Search Blocks in Content Areas and Text

EPiServer Find to Search Blocks in Content Areas and Text

EPiServer Find, allows to search content in your CMS easily and fast, but sometimes there are more complex search requirements which requires a little bit more of research. In this case, I am going to show you how to implement a search helper for documents which will allow you to search for pages which have specific blocks in a content area field of the page and at the same time search for pages which have specific text fields using wildcards.

epi_server_logo_detail

First, we will create a block which will be used in the page we want to search. The block will represent an author of an article with an image, name, job position and a description field.


[ContentType(GUID = "c1a7bff1-5951-abc2-ba6b-b55aca1f4a7b")]
public class AuthorBlock : BlockData
{
[CultureSpecific]
[UIHint(UIHint.MediaFile)]
[AllowedTypes(typeof(MediaData))]
[Required]
[Display(GroupName = SystemTabNames.Content, Order = 100)]
public virtual ContentReference Image { get; set; }

[CultureSpecific]
[Required]
[Display(GroupName = SystemTabNames.Content, Order = 200)]
public virtual string Name { get; set; }

[CultureSpecific]
[Display(GroupName = SystemTabNames.Content, Order = 300)]
public virtual string JobPosition { get; set; }

[CultureSpecific]
[Display(GroupName = SystemTabNames.Content, Order = 400)]
public virtual XhtmlString Description { get; set; }
}

Then, we will create the structure for the page that we are going to search. This page will represent an Article detail page with a content area field which will allow only Author blocks, and a text field for the article page title


[SiteContentType(GUID = "5c2a6ae1-6cc0-408a-a72f-effe7e8ba65a")]
public class ArticleDetailPage : PageData
{
[CultureSpecific]
[AllowedTypes(typeof(AuthorBlock))]
[Display(GroupName = SystemTabNames.Content, Order = 100)]
public virtual ContentArea ArticleAuthors { get; set; }

[Display(GroupName = SystemTabNames.Content, Order = 200)]
public virtual string ArticleTitle { get; set; }
}

Finally, we will add the search helper class which will search for the article title and authors in the content area article authors field. We will use wild cards for the title and GUID references for the authors.


public static class SearchHelper
{
public static IClient Client => SearchClient.Instance;

public static IEnumerable GetArticles(out int totalHits,
string title = "", string author = "")
{
var query = SearchClient.Instance.Search();

var titleWildCard = !string.IsNullOrEmpty(title) ? $"*{title}*" : ""; // *data* wild card
var authorFilter = !string.IsNullOrEmpty(author) ? $"{author}" : "";

if (!string.IsNullOrEmpty(titleWildCard))
{
query = query.WildCardQuery(titleWildCard, x => x.ArticleTitle);
}

if (!string.IsNullOrEmpty(authorFilter))
{
query = query.Filter(x => x.ArticleAuthors.ReferencedPermanentLinkIds.Match(new Guid(authorFilter)));
}

var batch = query.Take(1000).GetContentResult();
totalHits = batch.TotalMatching;

return batch;
}
}

In the class above,the wild card filter is applied to the article title property of the article detail if and only if it is not null or empty, the same applies to the author filter, but in this case we use the property ReferencedPermanentLinkIds to try to find a match in the content area of the page, based on the GUID of the block, finally total hits returns the number of pages found with the search criteria. If one of the fields is not set, it will not apply the filter to the search.

And that is all. With this simple helper you can query the article detail page of this example, by simple fields like a string and more complex one like a content area. I hope it will help someone and as always keep learning !!!

Written by:

Jorge Cardenas

Developer with several years of experience who is passionate about technology and how to solve problems through it.

View All Posts

Leave a Reply