sn_search.ScriptableSearchAPI Description

nikita_mironov
Kilo Guru

Hi, I wonder if anyone could share sn_search.ScriptableSearchAPI description (methods, parameters). It is used in Chats Quick Action "Search KB" in Agent Workspace. Nothing found in Community / Developer Portal / hi KB.

Thanks in advance.

8 REPLIES 8

ChrisPurple
Tera Contributor

Yes I would also like to know this so that we can configure Search KB

Rahul Jain6
ServiceNow Employee
ServiceNow Employee

Here are the details:

 

REQUEST:

 

searchContextConfigId:

sys_id of the search application configuration (sys_search_context_config table).

searchTerm:

search term

paginationToken

Pagination token is used to retrieve the next set of search results. It is included in the search response.

Type: string

disableSpellCheck

This flag is used to disable spell check when running a query. The use case is that when user sees spell-corrected word in the search result page, he may still insist on searching the original term. In this case, we don't want to spell correct it again.

Type: boolean

facetFilters

Facet filters that are applied to filter search results.

type: array of string

example: ["category_s:FACET(Software)","author_s:FACET(John Doe)"]

searchFilters

Search filters that are applied to filter search results.

type: array of string

example: ["552c48888c033300964f4932b03eb092","1c832706732023002728660c4cf6a7b9"]

requestedFields

Requested Fields are the fields shown in the search response. It is a stringified JSON object.

Type: Map<String, List>

Example:

{

  kb_knowledge: ['number', 'short_description'],

  sys_user: ['email', 'company', 'user_name'],

  sc_cat_item: ['name', 'short_description', 'price']

}

 

Functions available for the response:

 

response.getSearchResults()

response.getGeniusResults()

response.getMatchedQueryIntents()

response.getResponseTimeInMs()

response.getSearchFacets()

response.getMatchedGeniusResultIntents()

response.getAppliedFacetFilters()

response.getSelectedFilterIds()

response.getCount()

response.getNextPaginationToken()

response.getPreviousPaginationToken()

response.getTerm()

response.getSpellCorrectedTerm()

 

 

 

 

 

Hi @Rahul Jain6 ,

 

I have some questions about search-kb quick action that needs to ask you:

1. About faucet filter & search filter. Would you please explain what is the difference between two kinds of filter? About the search filter, which kind of value can be added in the list?

2. I have the requirement about customising the parameter script of /search-kb quick action, the requirement is to show all KBs in one category only when agent types the keyword. Would you please specify where I can put the query condition that filters only one category in the OOTB parameter script below? Thanks

 

 

var api = new sn_search.ScriptableSearchAPI();
var searchContextConfigId = '492f958dc30101103f4fcd7b7940dd2a';
var searchTerm = query;
var disableSpellCheck = false;
var facetFilters = [];
var searchFilters = [];
var requestedFields = {
 kb_knowledge: ['author', 'category', 'text', 'sys_view_count', 'number']
};
 
var session = GlideSession.get();
var sessionUserName = session.getUserName();
var sessionUser = session.getUser().getID();
var results = [];
try {
var openedForUser = record.getValue('opened_for');
session.impersonate(openedForUser);
 
var response = api.search(searchContextConfigId, searchTerm, paginationToken, disableSpellCheck,
 facetFilters, searchFilters, requestedFields);
 
results = response.getSearchResults();
} finally {
session.impersonate(sessionUser);
if (!session.getHttpSession().getAttribute('glide_user').equals(sessionUserName))
session.getHttpSession().setAttribute('glide_user', sessionUserName);
}
 
items = results.map(function(result) {
var columns = result.getColumns();
var fields = {};
columns.forEach(function(column) {
fields[column.getFieldName()] = column.getDisplayValue();
});
return {
value: fields['number'],
displayValue: result.getTitle(),
aisDetails: {
title: result.getTitle(),
summary: result.getText(),
detailValueOne: fields['number'],
detailLabelSeparator: "•",
detailLabelType: "inline",
nextPaginationToken: response.getNextPaginationToken(),
prevPaginationToken: response.getPreviousPaginationToken()
}
};
}).filter(function(item) {
var kbGR = new GlideRecordSecure('kb_knowledge');
return kbGR.get('number', item.value);
});
 
 
answer = {
items: items
};

J2
Tera Contributor

Sorry for bumping this old question:

What is the actual format of facets here ? what does "category_s" represent ? the name of the facet ? is "Software" the value ? 

I can tell that calling "getSearchFacets()" returns a SearchFacetScript object, but how does it work ? what are it's methods and how do i use it ?