Mediashop - Building an awesome search experince in Sitecore - Part 2, Defining the index

This is part two in a series on making an awesome search experience in Sitecore. In the first part, I showed the basics of getting some demo data, which can be used for the search application. I've decided on using "The Movie DB" and its open API for that purpose. Check out this first part

In this part, it's time to define the index, making the data available for the Sitecore Content search API. To do this, lets first look at what fields are in items that should be indexed. The items are based on the Movie template, which has these fields:

The ones that are a little special are the ones that should be treated as integers and doubles. I've defined them in the index as System.Int32 and System.Double. They are Vote average, Vote Count, tmdb id and runtime. Also notice that fields like release date (which is a date), and the multilist fields (genres, production Companies and production countries) are stored as a string. Sitecore has converters that will help keeping track on the fields and their types. One of the cool things about that is that when you define your custom classes for searching, Sitecore will handle the conversion between the index in Lucene and your .NET classes. Let's look at that.

I've created a class that drives from Sitecore.ContentSearch.SearchTypes.SearchResultItem, and that implements the fields like this:

The class defines which fields from the index that should be mapped to which .NET properties. When you define your .NET classes, simply specifying the property you need, Sitecore will do the conversion.

With the code above, and the search index defined, we can now utilize search in our application. I.e. the following will give us top 10 of items sorted by average votes.

In the next part of these blogposts, I'll show a custom field for an imageurl. Custom fields are dead simple to implement, and very powerful, as it takes some custom data (meaning not a field value), and storing it in the index, making it searchable.