Coveo Search Results Customization in Sitecore

Nov 09, 2011
Juan Arias

You may encounter the case when the fields you have indexed in Coveo are not enough or need some processing before they can be used in your searches; for these cases, Coveo offers the option to create conversion scripts which can be done either in vbscript or jscript.

Some examples that can be done with conversion scripts:

  • Override the language of the document;
  • Override or modify the content of the document;
  • Override or modify the HTML rendering of the document;
  • Add or modify metadata to the document;
  • Update the permissions of the document;
  • Reject documents;
  • Modify the ranking value of a document.

For this post, we are going to have a Coveo field with the name of authors of songs, a field can contain more than one author but separated by “;” and we need to do the search but only taking the first author of every song, not all the authors. Example:

List of songs and authors:

Song1: Peter Breen
Song2: John Smith; Laura Brown
Song3: Joseph Busby; Peter Watson
Song4: Kevin Peter Porter; David Holman

A search for “Peter” will return the songs: Song1 and Song4

First, create your file, it can have any extension you want (txt, vbs, js, etc) for this example, it will be called “ExtractFirstAuthor.txt”, and it will be a vbscript. Inside the file, declare a variable called “AllAuthors”:

Dim allAuthors : allAuthors = DocumentInfo.GetFieldValue("Authors")

'Notice that this variable is reading the Sitecore field, not the Coveo
'field. We can access that field with the function
'DocumentInfo.GetFieldValue(). Next, we will add the corresponding
'algorithm to extract the first author:

Dim authorsArray : authorsArray = Split(allAuthors, ";")
Dim singleAuthor
Dim iteration : iteration = 1

For Each singleAuthor in authorsArray
   if(iteration = 1) then
      Exit For
   end if

   iteration = iteration + 1
Next

 

And finally, we will update our Coveo field:

Call DocumentInfo.SetFieldValue("RBFirstAuthor", singleAuthor)

 

Save the file. Then, you have to create the Coveo field and mapping file like in the previous post. Then go to the Coveo admin page, select Configuration, Coverters, Conversion Scripts and then Add in the Postconversion Scripts section:



Enter a name, put the location of the file, make sure you select the correct language for the script you are creating and then click Apply Changes:



Then go to your index configuration, select Conversion Scripts, and then in Postconversion Script, select your script. Apply changes and Rebuild the index.



When the rebuild is done, you will be able to check your changes, open the Indexed Documents as shown in the previous post, and you will see something like this:



Post created by Juan Arias, Oshyn Inc. employee. Some information gathered from Coveo website.