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
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
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.
Below is the result of this query
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.