Episerver Find: Get search results from page and subpages

Vote:
 

I'm creating a search function that should return page specific search results.

I.e. if I have a News page I would like to search only in that page and its children Article pages.

Does a filter for such function exist in the Episerver Find API? 

#205108
Jun 28, 2019 14:24
Vote:
 

You can pass page types to filter, into your search query as shown below

var contentTypeIds = new List<int>
{
    this._contentTypeRepository.Load<StoryPage>().ID,
    this._contentTypeRepository.Load<GeneralPage>().ID
};
var searchQuery = this._findClient;
var contentTypeFilter = this._findClient.BuildFilter<IContent>();
if (contentTypeIds != null && contentTypeIds.Any())
{
    foreach (var contentTypeId in contentTypeIds)
    {
        contentTypeFilter = contentTypeFilter.Or(x => x.ContentTypeID.Match(contentTypeId));
    }
}

searchQuery = searchQuery.Filter(contentTypeFilter);

var results = searchQuery.GetContentResult();

Let me know if you need more information.

#205112
Edited, Jun 28, 2019 14:50
Vote:
 

You can also filter to get only below a specific content (like your news listing)

.Filter(x => x.Ancestors().Match(newsListingPage.ContentLink.ToReferenceWithoutVersion().ToString())
#205115
Jun 28, 2019 15:12
Vote:
 

Hi,

If I read your post correctly, you're looking to return a set of results filtered to be only descendents of a given page (e.g. news page). To do this you can filter on Ancestors() which contains a string array of the IDs of all of the ancestors of a given page as follows:

var parentId = parentPage.ContentLink.ID.ToString();
var results = searchClient.Search<ArticlePage>().Filter(x => x.Ancestors().Match(parentId)).GetContentResult();
#205116
Jun 28, 2019 15:18
Vote:
 

Thank you for your answers. I will try them out and see which suggestion matches my requirements!

#205117
Jun 28, 2019 15:20
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* 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.