Dieses Blog durchsuchen

Mittwoch, 11. März 2015

Sitecore 7: Lucene index: How to index alias items?

Recently I had a special requirement from my customer. He created a lot of nice and useful alias items in here:
/sitecore/system/Aliases
Those alias items are great. I remember the time to apache rewrite rules config rodeo nightmares. My client set alias to important landing pages like jobs, media, exhibitions, factbooks,…
He was asking me why those alias items are not visible in the search results? So they might not in the index because they are in the systemroot and are excluded.

Solution:

So what I did to get them in.
  1. I added the default layout not sublayout to the alias standard values
  2. I added also a redirect sublayout. See code below. This will do a redirect to the target page (form the data field) when clicking on an alias result item.
  3. We have a custom index field called “_title” witch in our case is boosted. So we extend the code that set the _title field and added the item name of the alias item to it. If someone is search for an alias name it should appear on the top results if it fits to an alias item name. Repeat this step for any other custom field that you need. In our case it was subtitle

Code for crawler that fill the lucene _title field:

public class Title : IComputedIndexField
    {
        public string FieldName { get; set; }
        public string ReturnType { get; set; }

        public object ComputeFieldValue(IIndexable indexable)
        {
            //Alias exception
            var indexableItem = indexable as SitecoreIndexableItem;
            if (indexableItem == null) return null;

            var item = indexableItem.Item;

            if (item.TemplateID.ToString() == ConfigurationHelper.GetSitecoreSetting("TID.Alias"))
            {
                   return item.Name;
            }

            return SearchHelper.GetComputedFieldValue(indexable, "Title", "Page Title");
        }
    }

Here redirect code:


  public partial class Redirect : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Item currentItem = Sitecore.Context.Item;

            //Alias exception
            if (currentItem.TemplateID.ToString() == ConfigurationHelper.GetSitecoreSetting("TID.Alias"))
            {
                Sitecore.Data.Fields.LinkField lf = currentItem.Fields["Linked item"];
                if (null == lf)
                {
                    HttpContext.Current.Response.Redirect(UrlHelper.GetAbsoluteItemUrl(ItemHelper.GetItemByGUID(AppSettings.ItemIds.WebsiteRoot)));
                }

                HttpContext.Current.Response.Redirect(lf.Url);
            }
}
}