Record and Limit Knowledge Search Results in Script based on minimum relevancy

StandardSanders
Giga Guru

I have the following Action that searches our Knowledgebases based on certain parts of an email form.

(function execute(inputs, outputs) {
    var searchTerm = inputs.searchterm;
    
    var gr = new GlideRecord('kb_knowledge');
    gr.addQuery('workflow_state', 'published');
    gr.addQuery('active', true);

    // Add full-text search query for the search term
    gr.addEncodedQuery('123TEXTQUERY321=' + searchTerm); // 123TEXTQUERY321 is a special operator for a broad match text search query

    gr.query();

    if (gr.next()) {
        // Set the first result as outputs named ka_number and ka_text
        outputs.ka_number = gr.getValue('number');
        outputs.ka_text = gr.getValue('text').replace(/<[^>]+>/g, '').replace(/ /g, ' ').replace(/[\n\r]+|[\s]{2,}/g, ' ');

    } else {
        outputs.ka_number = 'No article found';
        outputs.ka_text = '';
    }
})(inputs, outputs);

I would like to do two things to help refine this search:

1.) Output the Relevancy Score of the first result from the search performed
2.) Limit the results to a minimum score at a later time based on my findings

This script is helping suggest an initial Knowledge Article for our agents and I'd like to eliminate the results that are really off the wall when we simply have no relevant knowledge 

1 ACCEPTED SOLUTION

Yes I was. 
Here is the Script to pull out the relevancy. This was created as an Action that I have inserted into a larger flow

(function execute(inputs, outputs) {
    var searchTerm = inputs.searchterm;
    var gr = new GlideRecord('kb_knowledge');
    
    gr.addQuery('workflow_state', 'published');
    gr.addQuery('active', true);
    gr.addEncodedQuery('IR_AND_OR_QUERY=' + searchTerm); 
    gr.orderByDesc('ir_query_score');
    gr.query();

    var query_score = 0; // Initialize query_score

    if (gr.next()) {
        gs.log(gr.name + ':HEYSCOREHERE ' + gr.ir_query_score);
        query_score = gr.ir_query_score;
        outputs.relevancy = query_score;
        outputs.ka_number = gr.getValue('number');

        var ka_text = gr.getValue('text');
        if (ka_text) {
            outputs.ka_text = ka_text.replace(/<[^>]+>/g, '').replace(/[\n\r]+|[\s]{2,}/g, ' ');
        } else {
            outputs.ka_text = '';
        }

    } else {
        outputs.ka_number = 'No article found';
        outputs.ka_text = '';
    }

})(inputs, outputs);

 I use the output of the relevancy in the main Flow and if it is over 101 relevancy (just where we landed) it attaches to a custom field in a the ticket for "Suggested KA" and if it is below that it attaches our "No Solution" KA we use for tracking purposes. Should work in a similar way in the VA using AI Search (I'd be going down this route now too but our VA AI Search is having a big enough issue I had to put a support ticket in).

View solution in original post

2 REPLIES 2

Shree123
Tera Contributor

Were you able to figure this out? I need to implement something similar, in AI search for virtual agent.

Yes I was. 
Here is the Script to pull out the relevancy. This was created as an Action that I have inserted into a larger flow

(function execute(inputs, outputs) {
    var searchTerm = inputs.searchterm;
    var gr = new GlideRecord('kb_knowledge');
    
    gr.addQuery('workflow_state', 'published');
    gr.addQuery('active', true);
    gr.addEncodedQuery('IR_AND_OR_QUERY=' + searchTerm); 
    gr.orderByDesc('ir_query_score');
    gr.query();

    var query_score = 0; // Initialize query_score

    if (gr.next()) {
        gs.log(gr.name + ':HEYSCOREHERE ' + gr.ir_query_score);
        query_score = gr.ir_query_score;
        outputs.relevancy = query_score;
        outputs.ka_number = gr.getValue('number');

        var ka_text = gr.getValue('text');
        if (ka_text) {
            outputs.ka_text = ka_text.replace(/<[^>]+>/g, '').replace(/[\n\r]+|[\s]{2,}/g, ' ');
        } else {
            outputs.ka_text = '';
        }

    } else {
        outputs.ka_number = 'No article found';
        outputs.ka_text = '';
    }

})(inputs, outputs);

 I use the output of the relevancy in the main Flow and if it is over 101 relevancy (just where we landed) it attaches to a custom field in a the ticket for "Suggested KA" and if it is below that it attaches our "No Solution" KA we use for tracking purposes. Should work in a similar way in the VA using AI Search (I'd be going down this route now too but our VA AI Search is having a big enough issue I had to put a support ticket in).