Name
global.AISearchHelper
Description
A helper for AI Search
Script
var AISearchHelper = Class.create();
AISearchHelper.stripHighlightTags = function(original) {
var text = original;
text = text.replaceAll("<highlight>", "");
text = text.replaceAll("</highlight>", "");
return text;
};
AISearchHelper.getDocumentTitleFromModel = function(model) {
var val = '';
if (!gs.nil(model['columns.name']))
val = model['columns.name'];
else if (!gs.nil(model['name']))
val = model['name'];
else if (!gs.nil(model['ai_search_teaser_title']))
val = model['ai_search_teaser_title'];
return global.AISearchHelper.stripHighlightTags(val);
},
/**
* Returns short_description or description based on what are available
*/
AISearchHelper.getDescriptionFromModel = function(model) {
var desc = '';
// Genius search result
if (!gs.nil(model['columns.short_description'])) {
desc = model['columns.short_description'];
}
else if (!gs.nil(model['columns.description'])) {
desc = model['columns.description'];
}
else if (!gs.nil(model['columns.ai_search_teaser_title'])) {
desc = model['columns.ai_search_teaser_title'];
}
else if (!gs.nil(model['columns.ai_search_teaser_text'])) {
desc = model['columns.ai_search_teaser_text'];
}
else if (!gs.nil(model['columns.ai_search_parent_title'])) {
desc = model['columns.ai_search_parent_title'];
}
// Search results
else if (!gs.nil(model['kb_article.short_description'])) {
// this is the case where table = 'sn_cat_item' and the catalog contains kb_knowledge.
desc = model['kb_article.short_description'];
}
else if (!gs.nil(model['short_description'])) {
desc = model['short_description']; // most likely, this is the case where the table is kb_knowledge
}
else if (!gs.nil(model['description'])) {
desc = model['description']; // if short_description is not found so far, use the description
}
else if (!gs.nil(model['ai_search_teaser_title'])) {
desc = model['ai_search_teaser_title'];
}
else if (!gs.nil(model['ai_search_teaser_text'])) {
desc = model['ai_search_teaser_text'];
}
// remove high-light tag
return global.AISearchHelper.stripHighlightTags(desc);
};
/**
* Returns a KB reference if found.
*/
AISearchHelper.getKBArticleIdFromModel = function(model) {
var rtnSysId = '';
if (!gs.nil(model['parent_table']) && model['parent_table'] == 'kb_knowledge' ) {
rtnSysId = model['parent_sys_id'];
}
else if (!gs.nil(model['columns.table']) && (model['columns.table'] == 'kb_knowledge')) {
// genius search with kb
rtnSysId = model['columns.sys_id'];
}
else if (!gs.nil(model['kb_article.sys_id'])) {
// this is the case where table = 'sn_cat_item' and the catalog contains kb_knowledge.
rtnSysId = model['kb_article.sys_id'];
}
else if (!gs.nil(model['table'] == 'kb_knowledge')) {
// this is the case where table is kb_knowledge
rtnSysId = model['sys_id'];
if(gs.nil(rtnSysId) && !gs.nil(model['sysId']))
rtnSysId = model['sysId'];
}
return rtnSysId;
};
/**
* Returns the table from the model in the line-item.
*/
AISearchHelper.getResourceTableFromModel = function(model) {
var tableName = '';
// search in this order
if (!gs.nil(model['columns.kb_article_sys_id']))
tableName = 'kb_knowledge'; // kb table.
if (!gs.nil(model['columns.table']))
tableName = model['columns.table']; // for genius search result.
else if(!gs.nil(model['table']))
tableName = model['table'];
return tableName;
};
/**
* Returns table by checking all possible locations
*/
AISearchHelper.getParentTableFromPropValues = function(propValues) {
var tableName = '';
if (gs.nil(propValues))
return tableName;
var model = propValues.model ? propValues.model : {};
var actionPayload = propValues.clickAction ? propValues.clickAction.actionPayload ? propValues.clickAction.actionPayload : {} : {};
if (!gs.nil(actionPayload['table']))
tableName = actionPayload['table'];
else if (!gs.nil(model['parent_table']))
tableName = model['parent_table'];
else
tableName = AISearchHelper.getResourceTableFromModel(model);
return tableName;
};
AISearchHelper.getDataSourceTitle = function(srcTable) {
if(gs.nil(srcTable))
return '';
var gr = new GlideRecord(global.AISearchConstants.DATA_SOURCE_TABLE);
gr.addQuery('source', srcTable);
gr.query();
return gr.next() ? gr.getValue('name') : '';
};
AISearchHelper.getSourceURLFromModel = function(model) {
return model.url || '';
};
AISearchHelper.getAISDocId = function(srcTable, resultSysId) {
return srcTable +'_'+resultSysId;
};
/**
* Returns the sysId from the model in the line-item.
*/
AISearchHelper.getResourceIdFromModel = function(model) {
var sysId = '';
// search in this order
if (!gs.nil(model['columns.kb_article_sys_id'])) // kb article specific
sysId = model['columns.kb_article_sys_id'];
if (!gs.nil(model['columns.sys_id']))
sysId = model['columns.sys_id']; // for genius search result.
else if(!gs.nil(model.sys_id))
sysId = model.sys_id; // regular search result
else if(!gs.nil(model.sysId))
sysId = model.sysId;
return sysId;
};
/**
* Returns sysId by checking all possible locations
*/
AISearchHelper.getParentIdFromPropValues = function(propValues) {
var sysId = '';
if (gs.nil(propValues))
return sysId;
var model = propValues.model ? propValues.model : {};
var actionPayload = propValues.clickAction ? propValues.clickAction.actionPayload ? propValues.clickAction.actionPayload : {} : {};
if (!gs.nil(actionPayload['sysId']))
sysId = actionPayload['sysId'];
else if (!gs.nil(model['parent_sys_id']))
sysId = model['parent_sys_id'];
else
sysId = AISearchHelper.getResourceIdFromModel(model);
return sysId;
};
AISearchHelper.getResultLimits = function(searchAppConfig) {
var result
= {'genius_results_limit' : global.AISearchConstants.DEFAULT_GENIUS_SEARCH_RESULT_LIMIT,
'search_results_limit' : global.AISearchConstants.DEFAULT_SEARCH_RESULT_LIMIT};
if (!gs.nil(searchAppConfig)) {
result.genius_results_limit = searchAppConfig.genius_results_limit.getValue();
result.search_results_limit = searchAppConfig.search_results_limit.getValue();
}
return result;
};
AISearchHelper.getGlideSignalsEventTypeByResultType = function(resultType) {
var eventType;
if (resultType == global.AISearchConstants.RESULT_TYPE_GENIUS)
eventType = global.AISearchConstants.RESULT_EVENT_TYPE_GENIUS;
else if(resultType == global.AISearchConstants.RESULT_TYPE_SEARCH)
eventType = global.AISearchConstants.RESULT_EVENT_TYPE_SEARCH;
return eventType;
};
AISearchHelper.sendMetricsToGlideSignalsAPI = function(eventType, payload, logger) {
var startTs = new GlideDateTime().getNumericValue();
var userId = payload.userId;
var sessionId = payload.sessionId;
try {
var signal = new global.GlideSignalsService();
signal.trackEventAsync(eventType, "INFO", payload);
if (!gs.nil(logger)) {
var endTs = new GlideDateTime().getNumericValue() - startTs;
logger.info("Sent {0} tracking events to GlideSignals API. It took {1} milliseconds. user_id:{2}, session_id:{3}",
eventType, endTs+'', userId, sessionId);
}
return payload;
} catch(e) {
// log the issue and let the flow continues..
// the payload won't be logged here to avoid any possible privacy issue.
if (!gs.nil(logger)) {
logger.error("An error occurred while sending {0} tracking events to GlideSignals API. user_id:{1}, session_id:{2} Details:{3}",
eventType, userId, sessionId, e);
}
return {};
}
};
function _getSeachResultMetadata(searchResult) {
return searchResult.result[0].executionResult.searchMetadata.searchResultMetadata;
}
function getGeniusSearchResults(searchResult) {
return searchResult.result[0].executionResult.geniusResultsTemplates.items;
}
function _getSearchResults(searchResult) {
return searchResult.result[0].executionResult.searchResultsTemplates.items;
}
Sys ID
04ba44ac539e01105400ddeeff7b12d5