Getting a Parent's name in a Smart Search Transformation

Mike Bilz asked on October 5, 2017 00:19

Hello Kentico Team,

I am working to improve the search results on my site.

Since many pages on the site have the same page name I would like to add the name of the parent document to the link to make them easier to understand.

So that instead of saying "Homepage"

It said "Department Name: Homepage"

All of the code I have found so far is in the form of Macros, like this one: {% Documents[NodeAliasPath].Parent.DocumentName %} which have not been working for me.

I know that smart search transformations use their own special transformation methods, so I was wondering if there is a specific piece of code I need to get the page name or "Title" of the search results parent?

Thanks in advance.

-mike

Correct Answer

Peter Mogilnitski answered on October 8, 2017 18:53

Well it depends how your system is setup.

  1. If all your page titles configured manually. There is one option you can try: configure titles at the global level. The default page title format is: {%prefix%} - {%pagetitle_orelse_name%}. You can overwrite for example with {%CurrentDocument.Parent.DocumentName#%} - {%pagetitle_orelse_name%}, certainly instead of {%CurrentDocument.Parent.DocumentName#%} you can create a custom macro that will returns different based on document type, position in the tree etc.

  2. You can try as well update page properties i.e. Page Title using API according to your business logic. This way you will probably cover 99% of your pagers. The rest you can do manually.

But I see you kinda leaning towards getting it in transformation (not recommended), but if you are really need it - here it is:

<script runat="server">
string ParentName = "";

  protected override void OnInit(EventArgs e) {
       int DocumentId = Convert.ToInt32(GetSearchValue("DocumentId"));
       int NodeParentID = Convert.ToInt32(GetSearchValue("NodeParentId"));
       var ParentNode =  CMS.DocumentEngine.DocumentHelper.GetDocuments()
                        .CombineWithDefaultCulture(true)
                        .Published(true)
                        .Where("NodeID = " + NodeParentID.ToString())
                        .FirstOrDefault();
        if  (ParentNode !=null)   ParentName = ParentNode.GetValue("DocumentName").ToString();                       
  }

</script>
 <h1>ParentName:<%# ParentName%> </h1>

Cheers, Peter

0 votesVote for this answer Unmark Correct answer

Recent Answers


Peter Mogilnitski answered on October 5, 2017 03:33 (last edited on October 5, 2017 15:31)

Hi Mike,

I think getting parent document name in search result transformation might be quite inefficient: i.e. if you have search result page contains let say with 10 documents - you might end up with 10 queries to get a parent name. What you can do instead is to form your Document Page Title correctly for example:

Image Text

This way it will be also good for SEO purposes so you dont have duplicated page titles (very important for google). Then you can basically say for your smart search that the title field is documentpagetitle: Image Text

and get it in trasformation like <%# GetSearchValue("title") %> It is a good practice to check your index with Lucene Index Toolbox. All you smart search indexes located in CMS\App_Data\CMSModules\SmartSearch. You can see what are the fiedl names right away, instead of guessing.

P.S. I was under impression the documentpagetitle should be in index by default, by I dont see it in kentico 9, may in 10 it is already there.

1 votesVote for this answer Mark as a Correct answer

Mike Bilz answered on October 7, 2017 00:44

Hi Peter,

This definitely makes sense, but at this point I am looking at well over 1000 pages on my website, so manually altering each one would be an overwhelming amount of work.

Is there any other way to pull in this information?

I understand that the SQL call is inefficient, but it certainly seems like the more reasonable solution at the moment.

Do the search transformation methods offer any way to access to this information?

Thanks.

-mike

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.