Enhancing Coveo Search with Custom Computed Fields in Sitecore
Problem
When working with custom data in search, you often need custom computed fields. For example, I had a scenario where I needed to convert a zip code into latitude and longitude calling Google Location API to do the conversion, so it could be used in geo search. In another case, if a page was marked as "no index, no follow," I needed to make sure it didn’t show up in search results. To handle that, I used a showInCoveo computed field to respect those settings.
Solution
The solution of creating this is handled in few steps implement a class, define the field in the configurarion, indexing and consuming the new field.
Implement Class
Create a class library for Coveo or add it to your existing project. Create a custom computed fields class definition following the example code. There are three key properties
FieldName
FieldName property is typically left blank and configured from the configuration file.
ReturnType
ReturnType property is the one defines the field type in Coveo. If not specified by default it is a string type. It could hold few other types.
- String
- Integer
- Floating point
- Date/time
DateTime format for Coveo should be in yyyy/MM/dd@HH:mm:ssZ
ComputedFieldValue
Every time an item is indexed in Sitecore this method would be invoked with a Sitecore Indexable Item
Ensure you check the template when code enters the custom computed field and return. The reason to do this is every item indexed in Sitecore will pass through this code. So to have better efficiency and faster indexing times.
If the computed value is not available or blank always retun null.
Add to configuration
Use the coveo custom config named Coveo.SearchProvider.Custom.Config or a custom patch located in App_Config\Include\Coveo.
Define the field and add the computed field to the configuration. Refer the sample configuration
- Computed Index Field configuration
- Field Definition
Rebuild the index
When you rebuild the index in Sitecore it will do a synchronization that will define the new fields in Coveo. Navigate to the cloud platform and validate if the field succesfully created.
Subscribe to Bala Blog | Sitecore MVP | Coveo MVP | DX MVP | Sitecore | Coveo | Microsoft
Get the latest posts delivered right to your inbox