Try our conversational search powered by Generative AI!

Episerver unified search check if current page ancestor has name



I am trying to write a query where I want to search that if any of the ancestors has a particular name. I wrote this but doesn't work

query = query.Filter(i =>
!i.MatchTypeHierarchy(typeof(InfoPage)) |
    _contentLoader.GetAncestors(((InfoPage)i).ContentLink).MatchItem(k => k.Name.Match("foresatte"))

I tried this as well

query = query.Filter(i =>
!i.MatchTypeHierarchy(typeof(InfoPage)) |
    _contentLoader.GetAncestors(((InfoPage)i).ContentLink).MatchContainedCaseInsensitive(k => k.Name, "foresatte")

When I do this


Then I get back ids, but I want page name to search on.

Oct 04, 2022 11:55

Try something like this 

                    .Filter(x => ((InfoPage)x).Ancestors().Match(currentPage.PageLink.ToString()))
					.Filter(x => x.SearchTitle("foresatte"))
Oct 04, 2022 13:22

You can also project the Search Title from any property i.e. 

.ProjectTitleFrom(x => x.Name); 

Edited, Oct 04, 2022 13:27

I assume you meant Search and Navigation. You can simply add this

        public static IEnumerable<string> AncestorNames(this IContent content)
            return _contentLoader.GetAncestors(content.ContentLink.ToReferenceWithoutVersion()).Select(x => x.Name);

then include it in your convention then use in your query.

Edited, Oct 05, 2022 9:47

Thanks a lot for the answers. 

Oct 06, 2022 8:03
Quan Mai - Oct 06, 2022 14:06
please don't forget to mark an answer as accepted if it solved your problem :)
* 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.