Service Portal: Change the sort order of search results [typeahead-search]

Sean78
Kilo Contributor

Hi everybody,

So I'm currently messing with a clone of the 'typeahead-search' widget as well as the Search bar on our main Service Portal. We just upgraded to New York, and the type ahead widget returns a lot of results that I need to sort.

Some other sources I've considered are listed below, but I can't quite find the solution I need:

In the widget, I need to complete the sorting of the following:

  • Service Catalog Items sort first [Order: 100]
    • Record Producers, Requests, and so on
  • Knowledge Base articles [Order: 200]
  • Anything else [Order: 300]

I just need a little push or direction on how to corral the data in order, because I have a strong feeling that our end-users will increasingly send us tickets saying, "I can't find the 'xyz' form!" It will still be there, just not the first result...

 

Thank you for any advice!

2 REPLIES 2

Prasad Dhule
Tera Contributor

Hi Sean,

There is no way of defining the sort order as the results are returned by the search api.

To do this we have to sort the result and then return the object to the typeahead.

// Client script

// Define a result variable and an Order variable
$scope.result = [];
$scope.resultOrder = ['qa','sc','kb']; // desired order of the search sources

// Add the fllowing in the getResult function

c.getResults = function(query) {
..
  return $http.post("/api/now/sp/search", payload).then(function(response) {
  ..
    $scope.result = result.results.map(function(item) {
      ..
      //Add the following line at the end of the map to set the sort order
      item.order = $scope.resultOrder.indexOf(item.__search_source_id__); 
      return item;
    });
 
    // Sort the result
    $scope.result.sort(function(a, b){
      return a.order - b.order
    });
    return $scope.result;
  });
}

Hope this helps 🙂

Hello @Prasad Dhule , Thank you very much for the solution. Can you please little more elaborate this solution. like where we should paste this script and how to implement it. I'm bit new to this widget scripting concept.

Thanks

Viraj Deshmukh