Custom Field indexing with the new Sitecore 7 content search API

Sitecore 7 comes with it’s new Content Search API. The title of this post is self explanatory for what i’m about to share.

Scenario

I assume I have a website that contains information about different publishers books. These books are imported into Sitecore in XML format. I assume that on the website a user should be able to find content from these documents by performing a search. So I want to create an index that will index the contents of these books. Later I want to perform searches on Authors, Book names, Publishers, and clear text. A possibility is to index the pages of the books separately, so when the results are returned you know not only the book where the specific word or phrase has been found but also the exact page.

My XML looks like this.

This XML has been imported into a a Multiline Text Field on a Sitecore Item. (there can be many items that have catalogs XML imported into).

Step1 – Configure the index

Manually add a CustomIndexConfiguration.config file in App_Config/Include folder. (* it is mandatory that the Sitecore.ContentSearch.Lucene.DefaultIndexConfiguration.config file is before this file).

Note that my index name is book_index. 

You can see that i have added a custom crawler called BookCrawler which inherits from SitecoreItemCrawler class. For this example I only had to override the DoAdd method in your BookCrawler.

The CustomIndexOperations class implements the IIndexOperations interface. The only thing you need to implement is the Add method. Mine looks like this:

My Book class implements the IIndexable interface. In fact this class has, at this point, 2 utilities:

  • It is used for indexing
  • It is used for searching

To use the Book class for indexing it must implement IIndexable interface.

To use it for searching the properties must be decorated with [IndexField].

Searching

Searching is the easy part. Getting all the Books from the index is done like this:

To search for the books by author I do this:

That is pretty much it. I hope this blog post helps some of you out there.

If you liked my post please comment. If you have questions, please ask them.

Avatar photo

Alin Parjolea

System Developer at Pentia. I have been involved in small and large scale projects using Sitecore and .Net for the last 4 years. I am Sitecore certified developer since 2009. I have a Master degree in Computer Science from University of Southern Denmark. More detailed info about me can be found on my Facebook and LinkedIn pages.

3 thoughts on “Custom Field indexing with the new Sitecore 7 content search API

  1. Nice article, however this looks a lot like how you would do it before with the Search framework in 6.3 and up. Wouldn’t it be better to use the new Computed Index Field? Just create a new index configuration which would be used by your index and then add a field definition in the
    Just a thought.

    • Thank you for your comment Patrick. When i post i like writing about concrete examples. In my scenario here, i assumed i have some books imported in Sitecore in XML format and i want to index their content for ease of search. For that this example worked for me nicely. But you are right, you can compute fields as you wish and index them for other purposes.

Comments are closed.