Five New Optimizely Certifications are Here! Validate your expertise and advance your career with our latest certification exams. Click here to find out more

Episerver unified search check if current page ancestor has name

Vote:
 

Hello.

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

(((InfoPage)i).Ancestors()

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

#288627
Oct 04, 2022 11:55
Vote:
 

Try something like this 

  SearchClient.Instance.UnifiedSearch()
                    .Filter(x => ((InfoPage)x).Ancestors().Match(currentPage.PageLink.ToString()))
					.Filter(x => x.SearchTitle("foresatte"))
                    .GetResult()
#288630
Oct 04, 2022 13:22
Vote:
 

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

.ProjectTitleFrom(x => x.Name);

https://docs.developers.optimizely.com/digital-experience-platform/v1.1.0-search-and-navigation/docs/customizing-default-projections 

#288631
Edited, Oct 04, 2022 13:27
Vote:
 

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.

#288675
Edited, Oct 05, 2022 9:47
Vote:
 

Thanks a lot for the answers. 

#288728
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.