Don't miss out Virtual Happy Hour this Friday (April 26).

Try our conversational search powered by Generative AI!

Filter pages by specific page types and categories

Vote:
 

I'm trying to get only pages matching with specific page types (article, product pages for instance) and with specific categories list using Find. I couldn't make it work as per the samples available online. 

My questions are how are restrict the search to find only specific pages by matching specific page types and categories list. if any page type matches with more than one categories then search result should return one rather than returning same page type twice. 

Much appreciated if some one could please help me on this ?

Thanks,

Suresh 

#195342
Jul 20, 2018 12:44
Vote:
 

Check Find Documentation:

https://world.episerver.com/documentation/developer-guides/find/NET-Client-API/searching/Filtering/Custom-filtering-methods/

Filtering by Category:

int categoryId = 3; //Category to match

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

Filter by Content Type:
int contentTypeId = 42;

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


We have a similar requirement. Filtering documents using categories..
var pages =  SearchClient.Instance.Search<DocumentFile>()                .Filter(x => ((IContent)x).Ancestors().Match(pgdata.PageRoot.ID.ToString()))                 .Filter(x =>                     x.MatchType(typeof(DocumentFile))                     ).MatchCategories(Catlist)

MatchCategories is a custom filter- your own filter
#195345
Jul 20, 2018 14:38
Vote:
 

Thanks Ram Kumar for your response. 

I've made changes as follows and items in search result is always 0 for some reason. Please let me know if anything wrong in this code. 

public static IContentResult<PageData> MatchCategories(this ITypeSearch<PageData> search, CategoryList cats, string Id)
{
 search = search.Filter(x => ((IContent)x).Ancestors().Match(Id)) 
.Filter<PageData>(x => x.MatchType(typeof(PageType1)) | x.MatchType(typeof(PageType2)) | x.MatchType(typeof(PageType3))));
var catalogBuilder = search.Client.BuildFilter<PageData>();
foreach (var cat in cats)
{
catalogBuilder =
catalogBuilder.Or(x => x.Category.Match(cat));
}
return search.Filter(catalogBuilder).GetContentResult();
}

var result = Helper.MatchCategories(search, currentBlock.FilterCategories, currentBlock.SelectedPageRoot.ID.ToString());

//result.Items returns 0 always

Thanks,

Suresh

#195385
Jul 22, 2018 19:16
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.