Anders G. Nordby

Lead Software Engineer at Sopra Steria

EPiServer Find: Indexing MetaData with PageData

Sometimes, there are additional meta data that should be indexed together with your PageData objects. In my example, I have a separate database containing meta data for the CoursePage page type. So whenever I index or update a course, I would like to also index the meta data relevant for the particular course (as the meta data should of course also be searchable).

My solution to this problem is a bit “hackish”, as I get and index the meta data as part of my check for whether the CoursePage item should be indexed or not. Here is the working example code:

using System;
using MyProject.Delegate.Models.Pages;
using EPiServer.Core;
using EPiServer.Find;
using EPiServer.Find.Cms;
using EPiServer.Find.Cms.Conventions;
using EPiServer.Find.Framework;

namespace MyProject.Delegate.Configuration.EPiServer
{
    public class EPiServerFindInitialization
    {
        public static void Initialize()
        {
            ContentIndexer.Instance.Conventions.ForInstancesOf<PageData>)
                .ShouldIndex(x => x is IShouldIndexWithFind);

            ContentIndexer.Instance.Conventions.ForInstancesOf<CoursePage>()
                .ShouldIndex(CoursePageIndexingWithMetaData);
        }

        private static bool CoursePageIndexingWithMetaData(CoursePage coursePage)
        {
            // Make sure we delete old versions of the metadata (if there are any)
            var client = Client.CreateFromConfig();
            client.Delete<CourseMetaData>(x => x.CourseGuid.ToString()
                .Match(coursePage.KursId));

            // Get and index new metadata
            var metaData = GetMetaData(coursePage);
            SearchClient.Instance.Index(metaData);
            return true;
        }

        //TODO: Get real meta data here; this is just example data
        private static CourseMetaData GetMetaData(CoursePage coursePage)
        {
            return new CourseMetaData()
            {
                CourseName = coursePage.Name,
                CourseGuid = new Guid(coursePage.KursId)
            };
        }

        public class CourseMetaData
        {
            public string CourseName { get; set; }

            public Guid CourseGuid { get; set; }

            // Providing a nice title for the search result item
            public string SearchTitle { get { return "MetaData for " + CourseName; } }
        }
    }
}

And here is the result:
find-metadata

Advertisements

One response to “EPiServer Find: Indexing MetaData with PageData

  1. Pingback: More on Indexing MetaData with Find | Anders G. Nordby

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: