{"revision":"c10276e953456835cdbb1f3a60d58ae1980be353","document":{"id":773,"path":"api-reference/server-api-reference/GeniusResultContextScopedAPI.md","title":"GeniusResultContext - Scoped","bundle":null,"doc_type":null,"release":"australia","body":"\n# GeniusResultContext- Scoped\n\nThe GeniusResultContext API provides methods for retrieving search query information from the context of a Genius Result configuration.\n\nYou can use search query details retrieved with this API to populate Genius Result answer objects created with the [GeniusResultAnswer](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/server-api-reference/GeniusResultAnswerScopedAPI.md) API.\n\nUse 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](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/platform-administration/create-genius-results-config-ais.md).\n\n**Parent Topic:**[Server API reference](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/server-api-reference/api-server.md)\n\n## GeniusResultContext - getMatchingDocuments\\(\\)\n\nRetrieves search result documents from the search query that triggers your Genius Result configuration.\n\nUse 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.\n\n**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.\n\n|Name|Type|Description|\n|----|----|-----------|\n|None| | |\n\n<table id=\"table_fhg_4gr_ywb\" class=\"returns\"><thead><tr><th>\n\nType\n\n</th><th>\n\nDescription\n\n</th></tr></thead><tbody><tr><td>\n\n\\[Array\\]\n\n</td><td>\n\nArray of objects in which each object represents a search result document that matched the search query.\n\n Data type: Array\n\n ```\n[\n  {\n    \".score\": Number,\n    \".table\": \"String\",\n    \".text\": \"String\",\n    \".title\": \"String\",\n    \".url\": \"String\",\n    \"class_name\": \"String\",\n    \"sys_id\": \"String\",\n    \"<field_name>\": <field_value>\n  }\n]\n```\n\n **Note:** Depending on the search query, some of the search result document's AI Search index fields may appear in the object in addition to the listed name-value pairs.\n\n</td></tr><tr><td>\n\n\\[Array\\]..score\n\n</td><td>\n\nRelevancy score computed for the search result document in the context of the search query.\n\n Data type: Number\n\n **Note:** The name for this name-value pair begins with a period to distinguish it from the AI Search index's **score** common field.\n\n</td></tr><tr><td>\n\n\\[Array\\]..table\n\n</td><td>\n\nName of the ServiceNow AI Platform table that the search result document was indexed from.\n\n Data type: String\n\n **Note:** The name for this name-value pair begins with a period to distinguish it from the AI Search index's **table** common field.\n\n</td></tr><tr><td>\n\n\\[Array\\]..text\n\n</td><td>\n\nIndexed text from the search result document.\n\n Data type: String\n\n **Note:** The name for this name-value pair begins with a period to distinguish it from the AI Search index's **text** common field.\n\n</td></tr><tr><td>\n\n\\[Array\\]..title\n\n</td><td>\n\nIndexed title for the search result document.\n\n Data type: String\n\n **Note:** The name for this name-value pair begins with a period to distinguish it from the AI Search index's **title** common field.\n\n</td></tr><tr><td>\n\n\\[Array\\]..url\n\n</td><td>\n\nURL for the search result document.\n\n Data type: String\n\n **Note:** The name for this name-value pair begins with a period to distinguish it from the AI Search index's **url** common field.\n\n</td></tr><tr><td>\n\n\\[Array\\].class\\_name\n\n</td><td>\n\nThe child class that the search result document belongs to. For more information on table child classes, see [Table extension and classes](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/platform-administration/table-extension-and-classes.md).\n\n Data type: String\n\n</td></tr><tr><td>\n\n\\[Array\\].sys\\_id\n\n</td><td>\n\nSys\\_id for the search result document.\n\n Data type: String\n\n</td></tr><tr><td>\n\n\\[Array\\].&lt;field\\_name&gt;\n\n</td><td>\n\nAI Search index field from the search result document.\n\n Data type: The data type of the AI Search index field.\n\n</td></tr></tbody>\n</table>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.\n\n```\nfunction process(context) {\n  var answer = new sn_ais.GeniusResultAnswer();\n  var matchingDocuments = context.getMatchingDocuments();\n  if (matchingDocuments.length > 1) {\n    for (var i = 0; i < 3; i++) {\n      var currentDocument = matchingDocuments[i];\n      if (currentDocument['.table'] == 'kb_knowledge') {\n        var gr = new GlideRecord(\"kb_knowledge\");\n        if (gr.get(currentDocument['sys_id'])) {\n          answer.addDataMap({\n            \"number\": currentDocument['number'],\n            \"url\": currentDocument['.url'],\n            \"title\": currentDocument['.title']\n          });\n        }\n      }\n      break;\n    }\n  }\n  return answer;\n}\n```\n\n## GeniusResultContext – getOriginalSearchPhrase\\(\\)\n\nRetrieves the original search terms from the search query that triggers your Genius Result configuration.\n\nYou can use retrieved search terms to populate Genius Result answers using GeniusResultAnswer API methods.\n\n|Name|Type|Description|\n|----|----|-----------|\n|None| | |\n\n<table id=\"table_e5s_ntm_qnb\" class=\"returns\"><thead><tr><th>\n\nType\n\n</th><th>\n\nDescription\n\n</th></tr></thead><tbody><tr><td>\n\nString\n\n</td><td>\n\nOriginal terms from the search query.\n\n Data type: String\n\n</td></tr></tbody>\n</table>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.\n\n```\nfunction process(context) {\n  var answer = new sn_ais.GeniusResultAnswer();\n\n  var searchPhrase = context.getOriginalSearchPhrase();\n  answer.setSearchPhrase(searchPhrase);\n  answer.snippetSearch(true);\n\n  return answer;\n}\n```\n\n## GeniusResultContext – getPredictionResult\\(\\)\n\nRetrieves NLU model prediction results for the search query that triggers your Genius Result configuration.\n\nUse 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.\n\n|Name|Type|Description|\n|----|----|-----------|\n|None| | |\n\n<table id=\"table_ajs_4tm_qnb\" class=\"returns\"><thead><tr><th>\n\nType\n\n</th><th>\n\nDescription\n\n</th></tr></thead><tbody><tr><td>\n\n\\[Array\\]\n\n</td><td>\n\nArray of objects in which each object represents an NLU model intent prediction result for the search query.\n\n Data type: Array\n\n ```\n[\n  {\n    \"intentName\": \"String\",\n    \"result\": {Object},\n    \"solutionName\": \"String\"\n  }\n]\n```\n\n</td></tr><tr><td>\n\n\\[Array\\].intentName\n\n</td><td>\n\nName of the NLU model intent detected in the search query.\n\n Data type: String\n\n</td></tr><tr><td>\n\n\\[Array\\].result\n\n</td><td>\n\nObject representing the NLU model intent prediction result for the search query.\n\n Data type: Object\n\n ```\n\"result\": {\n  \"application\": {Object},\n  \"result\": {Object},\n  \"solutionLabel\": \"String\",\n  \"solutionName\": \"String\",\n  \"solutionType\": \"String\",\n  \"type\": \"String\",\n  \"version\": Number\n}\n```\n\n</td></tr><tr><td>\n\n\\[Array\\].result.application\n\n</td><td>\n\nObject containing metadata for the search application that produced the prediction result.\n\n Data type: Object\n\n ```\n\"application\": {\n  \"language\": \"String\",\n  \"type\": \"String\"\n}\n```\n\n</td></tr><tr><td>\n\n\\[Array\\].result.application.language\n\n</td><td>\n\nLanguage context for the ServiceNow AI Platform application that produced the prediction result.\n\n Data type: String\n\n</td></tr><tr><td>\n\n\\[Array\\].result.application.type\n\n</td><td>\n\nType of the ServiceNow AI Platform application that produced the prediction result.\n\n Data type: String\n\n</td></tr><tr><td>\n\n\\[Array\\].result.result\n\n</td><td>\n\nObject including details for the NLU model intent prediction result.\n\n Data type: Object\n\n ```\n\"result\": {\n  \"entities\": [Array],\n  \"intentName\": \"String\",\n  \"intents\": [Array],\n  \"nluModelName\": \"String\",\n  \"score\": Number\n}\n```\n\n</td></tr><tr><td>\n\n\\[Array\\].result.result.entities\n\n</td><td>\n\nArray of objects in which each object represents an NLU model intent entity detected in the search query.\n\n Data type: Array\n\n ```\n\"entities\": [\n  {\n    \"matchingSegments\": [Array],\n    \"name\": \"String\",\n    \"parts\": [Array],\n    \"score\": Number,\n    \"startingPosition\": Number,\n    \"value\": \"String\"\n  }\n]\n```\n\n</td></tr><tr><td>\n\n\\[Array\\].result.result.entities.matchingSegments\n\n</td><td>\n\nArray of objects in which each object represents a matching segment of the detected NLU entity.\n\n Data type: Array\n\n ```\n\"matchingSegments\": [\n  {\n    \"directMatch\": Boolean\n    \"meta\": {Object},\n    \"value\": \"String\"\n  }\n]\n```\n\n</td></tr><tr><td>\n\n\\[Array\\].result.result.entities.matchingSegments.directMatch\n\n</td><td>\n\nFlag that indicates whether the NLU entity matching segment represents an exact match for the entity value.\n\n</td></tr><tr><td>\n\n\\[Array\\].result.result.entities.matchingSegments.meta\n\n</td><td>\n\nObject containing key-value pair fields representing metadata for the NLU entity matching segment.\n\n Data type: Object\n\n ```\n\"meta\": {\n  \"augmentedBy\": \"String\",\n  \"colMeta:first_name\": \"String\",\n  \"colMeta:last_name\": \"String\",\n  \"compositionStrategy\": \"String\",\n  \"fieldName\": \"String\",\n  \"searchPreProcessors\": \"String\",\n  \"SUB_PHRASE.penalty\": \"String\",\n  \"tableName\": \"String\"\n}\n```\n\n</td></tr><tr><td>\n\n\\[Array\\].result.result.entities.matchingSegments.value\n\n</td><td>\n\nString value of the NLU entity matching segment.\n\n Data type: String\n\n</td></tr><tr><td>\n\n\\[Array\\].result.result.entities.name\n\n</td><td>\n\nName for the detected NLU entity, in `entity:[NLU_model].[intent].[entity_type]` format.\n\n Data type: String\n\n</td></tr><tr><td>\n\n\\[Array\\].result.result.entities.parts\n\n</td><td>\n\nArray of objects in which each object represents an NLU entity part found in the prediction result.\n\n Data type: Array\n\n ```\n\"parts\": [\n  {\n    \"meta\": {Object},\n    \"value\": \"String\"\n  }\n]\n```\n\n</td></tr><tr><td>\n\n\\[Array\\].result.result.entities.parts.meta\n\n</td><td>\n\nObject containing key-value pair fields representing metadata for the NLU entity part.\n\n Data type: Object\n\n ```\n\"meta\": {\n  \"augmentedBy\": \"String\",\n  \"colMeta:name\": \"String\",\n  \"compositionStrategy\": \"String\",\n  \"fieldName\": \"String\",\n  \"searchPreProcessors\": \"String\",\n  \"SUB_PHRASE.penalty\": \"String\",\n  \"tableName\": \"String\"\n}\n```\n\n</td></tr><tr><td>\n\n\\[Array\\].result.result.entities.parts.value\n\n</td><td>\n\nString value of the NLU entity part.\n\n Data type: String\n\n</td></tr><tr><td>\n\n\\[Array\\].result.result.entities.score\n\n</td><td>\n\nNumeric score from 0 through 1 indicating confidence for the detected NLU entity.\n\n Data type: Number\n\n</td></tr><tr><td>\n\n\\[Array\\].result.result.entities.startingPosition\n\n</td><td>\n\nNumeric index for the character position in the search query at which the detected NLU entity begins.\n\n Data type: Number\n\n</td></tr><tr><td>\n\n\\[Array\\].result.result.entities.value\n\n</td><td>\n\nString value of the NLU detected in the search query.\n\n Data type: String\n\n</td></tr><tr><td>\n\n\\[Array\\].result.result.intentName\n\n</td><td>\n\nName of the NLU model intent detected in the search query.\n\n Data type: String\n\n</td></tr><tr><td>\n\n\\[Array\\].result.result.intents\n\n</td><td>\n\nArray of objects in which each object represents a nested NLU model intent detected in the search query.\n\n Nested intent objects include the same parameter names and data types as the parent NLU model intent object.\n\n Data type: Array\n\n ```\n\"intents\": [\n  {\n    \"entities\": [Array],\n    \"intentName\": \"String\",\n    \"intents\": [Array],\n    \"nluModelName\": \"String\",\n    \"score\": Number\n  }\n]\n```\n\n</td></tr><tr><td>\n\n\\[Array\\].result.result.nluModelName\n\n</td><td>\n\nName of the NLU model that produced the prediction result.\n\n Data type: String\n\n</td></tr><tr><td>\n\n\\[Array\\].result.result.score\n\n</td><td>\n\nNumeric score from 0 through 1 indicating confidence for the NLU model prediction result.\n\n Data type: Number\n\n</td></tr><tr><td>\n\n\\[Array\\].result.solutionLabel\n\n</td><td>\n\nLabel for the NLU model that produced the prediction result.\n\n Data type: String\n\n</td></tr><tr><td>\n\n\\[Array\\].result.solutionName\n\n</td><td>\n\nName for the NLU model that produced the prediction result.\n\n Data type: String\n\n</td></tr><tr><td>\n\n\\[Array\\].result.solutionType\n\n</td><td>\n\nType for the NLU model that produced the prediction result.\n\n Data type: String\n\n</td></tr><tr><td>\n\n\\[Array\\].result.type\n\n</td><td>\n\nType of the prediction result.\n\n Data type: String\n\n</td></tr><tr><td>\n\n\\[Array\\].result.version\n\n</td><td>\n\nVersion of the NLU model that produced the prediction result.\n\n Data type: Number\n\n</td></tr><tr><td>\n\n\\[Array\\].solutionName\n\n</td><td>\n\nName of the NLU model that produced the prediction result.\n\n Data type: String\n\n</td></tr></tbody>\n</table>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.\n\n```\nfunction process(context) {\n  var answer = new sn_ais.GeniusResultAnswer();\n\n  answer.setTable('sc_cat_item');\n  answer.setSearchLimit(2);\n  var predictionResult = context.getPredictionResult();\n  if (predictionResult && predictionResult.length == 1) {\n    var detail = predictionResult[0];\n    if (detail['result'] && detail['result']['entities']) {\n      var entities = detail['result']['entities'];\n      for (var i = 0; i < entities.length; i++) {\n        if (entities[i]['name'].endsWith('catalogItem')) {\n          var matchingSegments = entities[i]['matchingSegments'];\n          for (var j = 0; j < matchingSegments.length; j++)\n            answer.addSearchPhrases([matchingSegments[j]['value']]);\n        }\n      }\n    }\n  }\n\n  return answer;\n}\n```\n\n"}}