How to search on specific fields and search terms in Virtual Agent Search Knowledge Base

Serkan Yilmaz1
Giga Contributor

Hi SN Community members,

I'm looking for a method that can be used to search on specific fields in the "Search Knowledge Base" topic. 

In the "Search Knowledge Base" topic there is a Script action "Contextual Search" and this uses the input from the previous User Input box. 

    var search_term = vaInputs.search_term;
    var context = vaVars.search_context_sys_id;
    var contextualSearch = new sn_itsm_va.VAContextualSearchUtil();
    var response = contextualSearch.search(context, search_term);
    var success = contextualSearch.processSearchResponse(response);

This works fine, but I want to know if there is a way to push more specific search actions into the search action (e.g. "category=ABC" -or- "Author=XYZ"). 

 

 

Another way of doing it is also fine for me, but the search should "respect" the data security (User Criteria).

Hope to hear from some of you!

 

Cheers,

Serkan

5 REPLIES 5

Rajesh Kannan G
ServiceNow Employee
ServiceNow Employee

Hi Serkan,

You can copy the search method in VAContextualSearchUtil into another script include and pass additional parameters as shown below to filter results based on author, category knowledge base.

	search: function(context, searchTerm) {
		var strSearchTerm = "" + searchTerm;
		var payload = {
			// Search Context(cxs_context_config) sys_id/name.
			// Meta data required for including Pinned Articles in Search
			meta : {
				includePinnedArticles: true,
				applyFilter : true
			},
			context : context, 
			query : {
				freetext: strSearchTerm, //(this is coming from chat)
				searchParameters:{
					knowledgeBase:"a7e8a78bff0221009b20ffffffffff17", //IT - Knowledge Base
					language:"en"
				},
				author:[
					"62d78687c0a8010e00b3d84178adc913" //Ron Kettering - Author
				],
				kb_category:[
					"5681bf8bff0221009b20ffffffffff95" //Email - Category
				],
				order:"relevancy"
			}
		};
		
		var request = new global.cxs_SearchAPI();
		var response = request.search(payload);
		return response;	
	},

 

Another approach is to use Knowledge Service Portal API. This is bit more flexible.

var searchText = "mail";
var windowStart = 0;
var windowEnd = 30;
var additionalFields = ["number", "sys_id", "published"];
var kb = new GlideRecord('kb_knowledge');
kb.addEncodedQuery('kb_knowledge_base=a7e8a78bff0221009b20ffffffffff17^kb_category=5681bf8bff0221009b20ffffffffff95^author=62d78687c0a8010e00b3d84178adc913');
//Additional filter conditions can be added to filter records.
var request = {
     keyword : searchText,
     language : gs.getUser().getLanguage(),
     resource : 'Knowledge',
     start : windowStart,
     end : windowEnd,
     order : "relevancy,true",
     knowledge_fields : additionalFields,
     kb_query: kb.getEncodedQuery()
};
var response = new KBPortalServiceImpl().getResultData(request);

Regards,

Rajesh

I will test your suggestion! Thank you for your response.

what if i add custom field and i want to filter the records based on that custom field.

like environment is dev/QA/prod.

u_environment: dev. i added like this but its not working as expected.

Rajesh Kannan G
ServiceNow Employee
ServiceNow Employee

It should have worked If you are using Knowledge Service Portal API and adding the condition as shown below. If not please share the code snippet.

var kb = new GlideRecord('kb_knowledge');
kb.addQuery('u_environment', 'development');

Regards,

Rajesh