The GeniusResultContext API provides methods for retrieving search query information from the context of a Genius Result configuration.

You can use search query details retrieved with this API to populate Genius Result answer objects created with the GeniusResultAnswer API.

Use this API in Genius Result server-side scripts with the sn_ais namespace identifier. For more information on scripting logic for Genius Results, see Create a new Genius Result configuration.

GeniusResultContext - getMatchingDocuments()

Retrieves search result documents from the search query that triggers your Genius Result configuration.

Use this method in a Genius Result configuration's response processor script to retrieve field values from search result documents returned by the search query that triggers your Genius Result. You can populate Genius Result answers with fields from the search result documents using GeniusResultAnswer API methods.
Note: This method returns no results when invoked in an AI Search request processor script, since the script is executed before AI Search computes results for the triggering search query. Don't use this method in your request processor scripts.
Table 1. Parameters
Name Type Description
None

Example

This AI Search response processor script retrieves up to three search result documents matched by the search query and checks whether any of them is from the Knowledge [kb_knowledge] table. If it finds a search result document from the Knowledge table, the function adds that document's fields to the GeniusResultAnswer object so the UI can display them to the user.

function process(context) {
  var answer = new sn_ais.GeniusResultAnswer();
  var matchingDocuments = context.getMatchingDocuments();
  if (matchingDocuments.length > 1) {
    for (var i = 0; i < 3; i++) {
      var currentDocument = matchingDocuments[i];
      if (currentDocument['.table'] == 'kb_knowledge') {
        var gr = new GlideRecord("kb_knowledge");
        if (gr.get(currentDocument['sys_id'])) {
          answer.addDataMap({
            "number": currentDocument['number'],
            "url": currentDocument['.url'],
            "title": currentDocument['.title']
          });
        }
      }
      break;
    }
  }
  return answer;
}

GeniusResultContext – getOriginalSearchPhrase()

Retrieves the original search terms from the search query that triggers your Genius Result configuration.

You can use retrieved search terms to populate Genius Result answers using GeniusResultAnswer API methods.

Table 3. Parameters
Name Type Description
None
Table 4. Returns
Type Description
String

Original terms from the search query.

Data type: String

Example

This AI Search request processor script gets the search query's original terms and uses them as the query terms for a new snippet search.

function process(context) {
  var answer = new sn_ais.GeniusResultAnswer();

  var searchPhrase = context.getOriginalSearchPhrase();
  answer.setSearchPhrase(searchPhrase);
  answer.snippetSearch(true);

  return answer;
}

GeniusResultContext – getPredictionResult()

Retrieves NLU model prediction results for the search query that triggers your Genius Result configuration.

Use this method in a Genius Result configuration's request or response processor script to retrieve the intent detected for the triggering search query. You can populate Genius Result answers with details from the detected intent using GeniusResultAnswer API methods.

Table 5. Parameters
Name Type Description
None

Example

This AI Search request processor script checks whether the search query includes an NLU model prediction result. When it finds a prediction result, it adds the matching segments from all detected catalogItem entities as search terms.

function process(context) {
  var answer = new sn_ais.GeniusResultAnswer();

  answer.setTable('sc_cat_item');
  answer.setSearchLimit(2);
  var predictionResult = context.getPredictionResult();
  if (predictionResult && predictionResult.length == 1) {
    var detail = predictionResult[0];
    if (detail['result'] && detail['result']['entities']) {
      var entities = detail['result']['entities'];
      for (var i = 0; i < entities.length; i++) {
        if (entities[i]['name'].endsWith('catalogItem')) {
          var matchingSegments = entities[i]['matchingSegments'];
          for (var j = 0; j < matchingSegments.length; j++)
            answer.addSearchPhrases([matchingSegments[j]['value']]);
        }
      }
    }
  }

  return answer;
}