Monday, February 29, 2016

Sitecore Lucene indexing child content against an item

This post is logic for indexing child items, please also see indexing sublayout or rendering data sources.

Sitecore Lucene search indexes and code can be simple to implement when a given item contains all of the fields which will need to be indexed against itself. This can get more complicated when it comes to sublayouts/renderings which contain content from a datasource (another item).

One solution is to create a custom index field which concatenates the content from child items/datasources into the Lucene index against the main item. This way all of the content that makes up a page is indexable against that page.

In this example, each item of page type is a Sitecore branch that automatically creates a child item called datasources. This can contain templates (with rich text fields) which are used as datasources for displaying additional content on the page. In this case we would want these datasources indexed against the parent item, because they only appear there (they don't have a layout assigned to them directly).

The code sample above contain the custom index field IndexChildContent. This looks for a child item of the datasources template type, then gets items inside that datasources folder which are of the template types which we wish to index. It then concatenates content from a rich text field called text (off of these indexable templates) into a string which is then the value stored in the index as the custom field.

The snippet from the Lucene index XML shows how the field is added as a computed index field stored locally with the name childcontent.

This is only a simple example, you would likely want to add logging and check if the item being indexed is of the correct type - for example media items won't have the datasources folder.

No comments:

Post a Comment