I am sure that all of you have created a regular item index for Sitecore with Solr or Lucene in the past already. I got to the challenge that I needed to call an external service which is provided by a third-party system provided by the client. My first thought was to directly call the service and show the results of that call – but the response took too long.
So I decided to make a custom index containing the external data and thus having a great response time.
Credits where credits are due
After some search I found two blog posts on SitecoreStories and SitecoreizIt which helped me a lot. So thank you very much for this. In the following chapters I consolidated the information, so it suits my case.
Adding a new class representing the index data field
First we will create an IndexableDataField-class which will represent the field in the index document. I am building an index from a persons database so I named my class IndexablePersonDataField and it inherits from the IIindexableDataField-class.
As you can see this class is straight forward. We have a constructor that has two parameters, one is our Model of the Database and the other is the fieldInfo which describes what kind of field we are writing to the index.
Adding an indexable class
The next class we are going to create is the Indexable-class in my case the IndexablePerson-class. This inherits from IIndexable and describes the indexable object.
Building a custom crawler to index the external data
Finally we need a custom crawler that builds up our index. I named mine PersonCrawler. The crawler inherits from Sitcore's FlatDataCrawler.
Defining the item collection
Here we need to override some methods but basically I am just using the GetItemsToIndex()-method. Here we are returning a collection of items that should be in the index.
Adjusting the configuration file
Then we need to provide a configuration file for Lucene or Solr where we add our custom crawler.
So far so good. But I also needed a scheduled task that runs daily to rebuild my index.
Adding a scheduled task for the index job
This is really easy to do. We just need to create another class where we get the index and rebuild it,
and a task command in Sitecore that links to our newly create class and our execution method
and – finally – a task schedule that links to our command and schedules when to run the task.
That's it! Now the external data should be successfully indexed in the custom field & you can deliver it through Sitecore!