The Zurich release has arrived! Interested in new features and functionalities? Click here for more

How to display Knowledge articles search result by decreasing order of view count in virtual chatbot.

pinki1
Tera Contributor

Hi,

I am creating topic in Virtual agent chat bot to search knowledge articles. I want to display search result by decreasing order of view count.

Could anyone please help to me with the script to display result by decreasing order of view count.

 

Regards,

Pinki

1 ACCEPTED SOLUTION

Mark Roethof
Tera Patron
Tera Patron

Seemed a lot easier and quicker then I thought. Just giving you the thought + full code below.

First, I thought, let's check what the Contextual Search actually gives us. Notice the viewCount property.

find_real_file.png

find_real_file.png

So could we sort on this? Actually, meta.viewCount I don't know... though looking at the solution which we got for Order an Item (and posted in the previous post), we could add a count. So started testing this with background script (easier and quicker to test then kicking of the Virtual Agent topic everytime).

find_real_file.png

WORKS!!!

Our fullcode for the Contextual Search step:

find_real_file.png

(function execute() {
    
    vaVars.indexInt = 0;
 
    var searchtermStr = vaInputs.search_term;
    var contextSysId = '2821675a5b30130070e4492c11f91a89'; // cxs_res_context_config
    
    var contextualSearch = new sn_itsm_va.VAContextualSearchUtil();
    var response = contextualSearch.search(contextSysId, searchtermStr);
    
    var success = contextualSearch.processSearchResponse(response);
    if(!success) {
        vaVars.countInt = 0;
        
        return '';
    }

    var searchresultsObj = response.results;

    for(var i = 0; i < searchresultsObj.length; i++) {
        searchresultsObj[i].count = searchresultsObj[i].meta.viewCount;
    }

    function rankingSorter(firstKey, secondKey) {
        return function(a, b) {  
            if(a[firstKey] > b[firstKey]) {  
                return -1;  
            } else if(a[firstKey] < b[firstKey]) {  
               return 1;  
            } else {
                if(a[secondKey] > b[secondKey]) {  
                    return 1;  
                } else if (a[secondKey] < b[secondKey]) {  
                   return -1;  
                } else {
                   return 0;
                }
            } 
        }  
    }

    var sortedObj = searchresultsObj.sort(rankingSorter("count", "title"))

    vaVars.countInt = sortedObj.length;
    vaVars.resultsObj = JSON.stringify(sortedObj);
 
    return '';

})()

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark

---

LinkedIn
Community article list

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

View solution in original post

2 REPLIES 2

Mark Roethof
Tera Patron
Tera Patron

Hi there,

Nice question! Somehow didn't thought of this myself... strangely enough we did exactly setup this for our Order an Item topic. Displaying the most viewed Catalog Items first. We added this logic to the Contextual Search step. Just quering the sp_log table (for Knowledge Articles obviously a little bit different), adding the count to the var response = contextualSearch.search(context, search_term);, and than sorting the results with a function.

Will look into this later, maybe also easy to implement for knowledge, nice enhancement which I didn't think of yet.

I don't know your level of scripting, though sharing below the changed contextual search step for our order an item topic. Maybe this is already enough for you to understand the logic we've implemented on that topic.

(function execute() {
    
    vaVars.index = 0;

    var search_term = vaInputs.search_term;
    var context = '6f7e3f565b30130070e4492c11f91aea'; // cxs_res_context_config

    var contextualSearch = new sn_itsm_va.VAContextualSearchUtil();
    var response = contextualSearch.search(context, search_term);

    var success = contextualSearch.processSearchResponse(response);
    if(!success) {
        vaVars.countInt = 0;
        
        return '';
    }

    var searchResults = response.results;
    var results = [];

    for (var i = 0; i < searchResults.length; i++) {
        var sys_id = searchResults[i].id.split(':')[1];

        var ga = new GlideAggregate('sp_log');
        ga.addEncodedQuery('id=' + sys_id + '^type=Cat Item View^ORtype=Cat Item Request');
        ga.addAggregate('COUNT');
        ga._query();

        var countInt = 0;
        if(ga._next()) {
            countInt = ga.getAggregate('COUNT');
        }

        results.push({
            'sys_id': sys_id,
            'title': searchResults[i].title,
            'image': searchResults[i].image.link,
            'count': parseInt(countInt)
        });
    }

    function rankingSorter(firstKey, secondKey) {
        return function(a, b) {  
            if (a[firstKey] > b[firstKey]) {  
                return -1;  
            } else if (a[firstKey] < b[firstKey]) {  
               return 1;  
           }  
           else {
               if (a[secondKey] > b[secondKey]) {  
                    return 1;  
                } else if (a[secondKey] < b[secondKey]) {  
                   return -1;  
               } else {
                   return 0;
               }
           } 
       }  
   }

   var sorted = results.sort(rankingSorter("count", "title"));

    vaVars.countInt = results.length;
    vaVars.results = JSON.stringify(sorted);

    return '';

})()

Obviously it is a customization, as this is not available in the out-of-the-box topic.

 

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark

---

LinkedIn
Community article list

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

Mark Roethof
Tera Patron
Tera Patron

Seemed a lot easier and quicker then I thought. Just giving you the thought + full code below.

First, I thought, let's check what the Contextual Search actually gives us. Notice the viewCount property.

find_real_file.png

find_real_file.png

So could we sort on this? Actually, meta.viewCount I don't know... though looking at the solution which we got for Order an Item (and posted in the previous post), we could add a count. So started testing this with background script (easier and quicker to test then kicking of the Virtual Agent topic everytime).

find_real_file.png

WORKS!!!

Our fullcode for the Contextual Search step:

find_real_file.png

(function execute() {
    
    vaVars.indexInt = 0;
 
    var searchtermStr = vaInputs.search_term;
    var contextSysId = '2821675a5b30130070e4492c11f91a89'; // cxs_res_context_config
    
    var contextualSearch = new sn_itsm_va.VAContextualSearchUtil();
    var response = contextualSearch.search(contextSysId, searchtermStr);
    
    var success = contextualSearch.processSearchResponse(response);
    if(!success) {
        vaVars.countInt = 0;
        
        return '';
    }

    var searchresultsObj = response.results;

    for(var i = 0; i < searchresultsObj.length; i++) {
        searchresultsObj[i].count = searchresultsObj[i].meta.viewCount;
    }

    function rankingSorter(firstKey, secondKey) {
        return function(a, b) {  
            if(a[firstKey] > b[firstKey]) {  
                return -1;  
            } else if(a[firstKey] < b[firstKey]) {  
               return 1;  
            } else {
                if(a[secondKey] > b[secondKey]) {  
                    return 1;  
                } else if (a[secondKey] < b[secondKey]) {  
                   return -1;  
                } else {
                   return 0;
                }
            } 
        }  
    }

    var sortedObj = searchresultsObj.sort(rankingSorter("count", "title"))

    vaVars.countInt = sortedObj.length;
    vaVars.resultsObj = JSON.stringify(sortedObj);
 
    return '';

})()

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark

---

LinkedIn
Community article list

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn