Sitecore Content Search Computed Field Pass Custom Variables

One requirement of Content Search i’ve required recently, is the ability to pass in custom variables from the computed field config element. I needed this so multiple sites can use the same computed fields, but inject settings unique to that site. For example passing in the site name to generate site specific urls. Continue reading

Sitecore ContentSearch Language Based Search

Just a quick blog on using Sitecore content search within multilingual sites.

A very basic way of achieving a multilingual search,  i’ve used in the past involves building a query to filter on the language from the Sitecore.Context.Language. This method is explained in a previous tweet from Jamie Little and a blog post, however a comment from Stephen Pope shed more detail on content search, where you can actually pass in a culture into the content search.
Continue reading

Sitecore 7 Search ID Array Type Converter

This is a simple type converter that allows you to convert a collection of Sitecore IDs stored in a Lucene search index into a List.

public class ListIDConverter : TypeConverter
    {
        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
        {
            if (sourceType == typeof(List<ID>))
            {
                return true;
            }

            return base.CanConvertFrom(context, sourceType);
        }

        public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
        {
            if (destinationType == typeof(List<string>))
            {
                return true;
            }
            return base.CanConvertTo(context, destinationType);
        }

        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
        {
            var scIds = new List<ID>();

            var ids = (List<string>) value;

            foreach (var id in ids)
            {
                ID scId;
                if (ID.TryParse(id, out scId))
                {
                    scIds.Add(scId);
                }
            }

            return scIds;
        }
    }

To use the Type converter decorate your public property with the TypeConverter in your Search Result class.

    [TypeConverter(typeof(ListIDConverter))]
        [IndexField("_templates")]
        public List<ID> Templates { get; set; }

Sitecore 7 Search by base templates

Quick tip, when using Sitecore 7’s search API to build a general site search, you may want to return only pages rather than any other items within Sitecore. All my page items inherit from a base Item to implement page title, meta data and so on. Using linq we are able to check whether an item is based on a specific base template, to do this we need to tell Sitecore to index the base templates.

By default in Sitecore 7 indexing base templates is disabled to improve the indexing speed. This is very simple to switch on.

Inside the section within your index uncomment the line below.

 <field fieldName="_templates" storageType="yes" indexType="untokenized">Sitecore.ContentSearch.ComputedFields.AllTemplates, Sitecore.ContentSearch</field>

Rebuild your index and these base templates are now accessible in your search result class.