Computed Fields in Sitecore Search Using Azure

20 September 2017
522 Views

Hi All,

This is the second blog in Sitecore Search Series. In this blog we will see how to add computed fields in search index. Before going through this blog, I request you to kindly visit the first blog of this series.

What is computed fields?

In Sitecore, an item is made up of fields, which are defined by its template. By default index contain all the fields which are available in the item. Mostly we want to have some custom logic on the item field and store that in the index as well. For e.g. there are two fields available in the item, one is Price and other is discount percent. We want a field in our index which will have “PriceAfterDiscount”. The logic for this field should be Price- (Price * Discount)/100
So whenever we need some custom logic on the field, we create a custom field.

In the first blog of this series, we have created an item “Nitish” just under home. Item contains one field “Title”. In this blog I will write a computed field whose task is to store the “Title” data in lower case. I will name this field as “titleinlowercase”.

First start by writing code for Custom Field.

  • Add a class and name it ToLowerCase
  • Inherit IComputedIndexField and implement its method
  • Define the logic as below

code_computed_field

Build the solution and publish.

We have written code for this computed field. We need to add this field in config file.

Go to include folder of your website and open Sitecore.ContentSearch.Azure.DefaultIndexConfiguration.config

Search for COMPUTED INDEX FIELDS in the file, and add entry for your code in fields section

adding_custom_fields

We need to enter field name, class file and dll name. Please note, field name which you will provide here, will get indexed in azure.

That’s it. We are all set. Open Sitecore instance and rebuild “Web” indexes.

Once done, open azure portal, select Search Service, Navigate to “Web” index and search for newly created field.

I have executed a query on this new field using Postman tool.

query_field

Below is the result of this query

result_computed_field

That’s all about Computed Fields in Sitecore Azure Search. Got Question ? Post a comment or contact me. I will be happy to answer you.

2 Comments

  • Tom Neij says:

    Great post and just to add as a tip, i would try to avoid updating these configs of sitecore itself and use patchfiles instead. It could break or get’s overwritten after updating sitecore. Next to that it’s easier to debug potential indexing issues when you can easily remove all your custom configs.

    Cheers!

    • jiet.nitish@gmail.com says:

      Sure we can do that. This blog is just for the intro purpose of how to implement computed fields. Thanks for the comment :)

Leave a Reply

Your email address will not be published. Required fields are marked *