Foreign Keys and Computed Fields in Coveo for Sitecore

Posted 07/29/2014 by Akshay Sura

Coveo JS Framework allows us to deploy search pages with minimal coding. Most of the functionality can be achieved via configuration from the page/content editor. Eventually you would need to add in facets or fields that require you to define relationships between templates. For instance a News items has News Categories, and while displaying the News items in the search we would also like to display the category/categories the news item belongs to.

To achieve this functionality, we can use Foreign Keys. There is some documentation on the Coveo website at this Link. Foreign Keys are pretty straight forward, we need to know the parent/main Template, in our case its News and the Field (guid(s)), which link to the child template, which is News Categories. We then need the child template, which is News Category, and its ID and Value fields, id and Category Name respectively.

Knowing the above, you would add it to your custom config, here is a sample:

News Categories News Category id Category Name

Foreign Keys work great, they do not increase the indexing time but are not suitable in a multilingual scenario. For a site running on multiple languages you would need to use computed fields or modify the REST pipeline to get the values in the context language. We will cover the REST pipeline for Coveo in future posts.

Creating a computed field is very easy and you can find some documentation on the Coveo website at this Link.  

A computed field allows us to store a computed/calculated value in an additional field in the index. So for instance if you are storing products, we can have a computed field which automatically calculates the shipping cost based on the product weight and dimensions and stores it. This way the UI does not have to do this calculation.

Similarly, in a multilingual scenario, we can use the computed field to store the language specific value(s) for foreign key relationships.

Here is an example of a computed field that clears any html from a summary field and stores it in the index for display purposes:

Here is the sample configuration in your custom config:

YOURNAMESPACE.ComputedFields.CleanSummaryField, YOURNAMESPACE