
- Post History
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
on 01-25-2020 10:23 PM
Hi there,
Out-of-the-box the ServiceNow conversation plugins for Virtual Agent come with loads of nice Topics. Most should be seen as Templates, though some could almost go 1on1 into your Production environment. One of those topics, "Search Knowledge Base". Though still, let's enhance the topic a little bit:
Sorting results by Most Viewed
Out-of-the-box
Out-of-the-box the articles returned from a search are always in the same order. Though, not in a way like Highest rated first, Most viewed first, etcetera.
When digging into the results returned from the search, we actually do have the Most Viewed available.
var searchtermStr = 'order a laptop';
var contextSysId = '2821675a5b30130070e4492c11f91a89';
var contextualSearch = new sn_itsm_va.VAContextualSearchUtil();
var response = contextualSearch.search(contextSysId, searchtermStr);
var success = contextualSearch.processSearchResponse(response);
var searchresultsObj = response.results;
for(var i = 0; i < searchresultsObj.length; i++) {
searchresultsObj[i].count = searchresultsObj[i].meta.viewCount;
}
gs.info(JSON.stringify(searchresultsObj));
Performing the above code with background script, would give us:
Testing a sort
So could we sort on this? Actually, meta.viewCount I don't know... though looking at the solution which we've set up for Order an Item (Virtual Agent - Enhanced Order an Item topic), we could add a count.
So when testing this with background script (easier and quicker to test then kicking off the Virtual Agent topic every time):
var searchtermStr = 'order a laptop';
var contextSysId = '2821675a5b30130070e4492c11f91a89';
var contextualSearch = new sn_itsm_va.VAContextualSearchUtil();
var response = contextualSearch.search(contextSysId, searchtermStr);
var success = contextualSearch.processSearchResponse(response);
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"))
gs.info(JSON.stringify(sortedObj));
The result shown in the logs looks like:
Eureka! It works 🙂
Implementing the Most Viewed sort
With the result from the previous step, implementing the Most Viewed sort is almost too easy. Below the updated code that we use on the Script Action Utility "Contextual Search":
(function execute() {
vaVars.indexInt = 0;
var searchtermStr = vaInputs.search_term;
var contextSysId = vaVars.search_context_sys_id;
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;
})()
---
And that's it actually. Hope you like it. If any questions or remarks, let me know!
If this post helped you in any way, I would appreciate it if you hit bookmark or mark it as helpful. Interested in more articles, blogs, videos, and Share projects on Virtual Agent I published? - Virtual Agent |
Kind regards,
Mark
2020 ServiceNow Community MVP
2020 ServiceNow Developer MVP
---
- 4,020 Views