Search knowledge based using REST API efficiently

wakespirit
Kilo Guru

Dear all ,

I have some special case in Searching the knowledge base which are not working as expected using REST API.

The goal in exemple below is to return all KB where short description contains for instance "access VPN" or "VPN.

What I mean is that event if access keyword is missing I should be able to get "access vpn" result

For exemple :

https://myinstance.service-now.com/api/now/table/kb_knowledge?sysparm_query=short_descriptionLIKEaccess vpn 
– Fetches desired results (OK) as the whole string is provided

https://myinstance.service-now.com/api/now/table/kb_knowledge?sysparm_query=short_descriptionLIKEvpn 
– Not all results are return as access is missing (MISSING ARTICLES)

 

The only way we make it works is to specify the whole querry as below:

https://myinstance.service-now.com/api/now/table/kb_knowledge?sysparm_query=short_descriptionLIKEacc... 
– Fetches desired results

 1st Query fetches the desired results only if all words Access and VPN are in sequence. If you change the sequence, no results are returned.

2nd Query does not fetch all the results. As word access is missing, it won’t return the results with Access and VPN in short_description

 3rd Query fetches all desired results; However as you can see I’ll have to append all the words with prefix short_descriptionLIKE. This will be a too long query if I ma more criterai.

For example, consider someone searching for kb with ‘I want to know how to get access to my application on Mobile using F5’.

Then by this long sentence I should be able to retrive any Kb wich has suitable matching short description. Which means that short descritpion can be only "access application on mobile".

Any idea how to handle such sear result ?

 

regards

1 ACCEPTED SOLUTION

moers
Giga Contributor

Hi wakespirit

the stop words list must have the same case as the query.

var stopWords = gs.getProperty('stopword.list').toString().toLowerCase().split(/[,\s]+/);

check if the stopWords an array? Format must be "term a, term b, etc";

gs.info(Array.isArray(stopWords))

same with queryArray

var arrayUtil = new ArrayUtil();

var queryArray = arrayUtil.diff(querySplit, stopWords); (querySplit - stopWords)

gs.info(Array.isArray(queryArray) + ' : '  + JSON.stringify(queryArray))

if query is an array, query.join('^') results in short_descriptionLIKEsegment_1^short_descriptionLIKEsegment_2 (desc contains [0] AND desc contains [1] AND....)

you can print the query with gs.info(kb.getEncodedQuery())

response.setBody(kb); might not work as kb is a GlideRecord object, create a json object e.g. 

response.setBody({ id : kb.getValue('sys_id'), desc : kb.getValue('short_description)});

 

Cheers

Bori 

View solution in original post

13 REPLIES 13

you can write a script include and have this script there.

Call the script include from the client script and pass the string as a parameter to this script include.

Refer to following links

https://docs.servicenow.com/bundle/london-application-development/page/script/server-scripting/concept/c_ScriptIncludes.html

https://community.servicenow.com/community?id=community_question&sys_id=94a60365db1cdbc01dcaf3231f96192d

Kindly mark the comments as helpful and correct.

I think you did not understand my need.

The input string is comimg from an external application and that external application need to call the Servicenow REST api to search for knowledge based and then return the articles.

SO i guess the script you mentionned I need to build it outside service now like in for instance .NET test application

If it is coming from an external application, then the script that i give has to be written in the .NET application. In your .NET application, after you capture the string, you need to call the above script which will split the string and gives you the API url whic you can then call ServiceNow.

Kindly mark the comments as correct and helpful.

Or the API url is dynamically build in .NET and pass directly to servicenow I guess ?

NO need to make 2 loop 

Yes. the script which i give you will return you the dynamically API url based on the user inputted string. You use that and make a call.

Kindly mark the comment(s) as helpful and correct.