Render computed index field value in Coveo Hive search result template

There may come a time when you need to render the value of a custom field you created in the search results of your Coveo Hive search interface. Specifically, you would want to edit the search result template – hopefully you created a new one based on the file provided out of the box. How do you make the value appear though? I found this to be much more involved than I expected.

The isExternal setting

When it comes to rendering the value of the custom field, one setting is very important. In the fieldMap where you defined your custom field, you can choose whether you wish the field to be treated like a Coveo for Sitecore field (and to consequently appear in the content browser in a format such as fmyfield99999), or to be treated as an external (non-Sitecore) field, but still work with Coveo (and thus, appear without any characters added, like myfield). The setting you need to add or check: isExternal. Example:

 <fieldType fieldName="resultDate" settingType="Coveo.Framework.Configuration.FieldConfiguration, Coveo.Framework" isExternal="true" />

The underscore code required will depend on whether the value of that setting is true or false (false being the default). Whether you use {{ }} or <% %> syntax is up to you.

Coveo for Sitecore Fields

If the setting is false or not supplied, your field is a Coveo for Sitecore field, and could be rendered in one of the following ways:

1. Using the coveoFieldValue helper.

{{- coveoFieldValue("myfield") }}

2. Using the coveoFieldName helper in a div (WordPress didn’t like HTML in my blog post so I had to strip out the tag syntax):

div class="CoveoFieldValue" data-field="{{- coveoFieldName("contentType")}}" /div

If these don’t work, try adding @@ before the field name.

External Fields

If you provide the isExternal setting with a value of true, you can render the value of it using raw.

{{- raw.myfield }}

Using the coveoFieldName helper in a div also works.

Note that when using raw, the name of your field needs to be all lowercase, or the value will not be rendered. I was confused by this, because I entered my field name in the Coveo.SearchProvider.Custom.config as myField. Coveo support answered:

All the fields are actually translated to the index’s format, restricted to only lowercase letters, numbers and underscores:

https://onlinehelp.coveo.com/en/cloud/add_edit_fields.htm

During the indexing process, any field name that do not meet those requirements will be translated to that format (Coveo for Sitecore fields also get a unique hash added so that they are unique to each index).

Completion

You should be able to use the image() and date() helpers just fine now that you are using the correct syntax. Just note that if your field is external and you are using raw, your field name must be in lowercase. From what I can see, it doesn’t need to be all lowercase if you are using the other helpers. Hope this helps!

Advertisements