Adding Synonym Sets to Help Center search for Guide



Gepostet 13. Okt. 2017

I posted a question earlier this week on how to perform synonym searches in Guide. It was suggested that there wasn't anything currently that does this, so I decided to make my own to share. 

For us, we have some products that are frequently misspelled but no way (unless we tag all of them) to have them return in the search results or direct the user to the correct spelling. This solution enables you to add synonyms to your Help Center search.

Add this to the js file for your Help Center/Guide. You can add multiple synonym sets by copying and pasting the following line of code to handle both synonyms and spelling mistakes. 

 

 NewSynonymSet('sleep', ['dream', 'hybernate', 'nap', 'sleeep']);

//Start Code

$(document).ready(function() {
   InitializeSynonyms()
});

var synonymsets = new Array();
function NewSynonymSet(key, synonyms) {
   var synonym = new Object();
   synonym.primary = key;
   synonym.keywords = synonyms;
   synonymsets.push(synonym);
}

function ChangeSearch(val)
{
   var q = $(val).val();
   var query = q.toLowerCase().split(' ');
   for (var i = 0; i < query.length; i++) {
      for (var x = 0; x < synonymsets.length; x++) {
         var primary = synonymsets[x].primary;
         var keywords = synonymsets[x].keywords;
         if ($.inArray(query[i], keywords) > -1) {
            query[i] = primary;
         }
      }
   }
   var newQuery = '';
   for (var i = 0; i < query.length; i++) {
      newQuery += ' ' + query[i];
   }
   $('#query').val(newQuery.substring(1));
}

function InitializeSynonyms() {
   //always lowercase and can handle misspellings too.
   //Enter as many of these lines below as needed with different synonym sets
   NewSynonymSet('sleep', ['dream', 'hybernate', 'nap', 'sleeep']);
   $('#query').on('keyup', function () {
      ChangeSearch(this);
   });
   $('#query').on('paste', function () {
      ChangeSearch(this);
   });
}

//End Code


1

7

0 Kommentare

Anmelden, um einen Kommentar zu hinterlassen.

Sie finden nicht, wonach Sie suchen?

Neuer Post