Product Selector

Fusion 5.9
    Fusion 5.9

    JavaScriptQuery pipeline stage configuration specifications

    The JavaScript Query stage allows you to write custom processing logic using JavaScript to manipulate search requests and responses. The first time that the pipeline is run, Managed Fusion compiles the JavaScript program into Java bytecode using the JDK’s JavaScript engine.

    The JavaScript Query stage allows you to run JavaScript functions over search requests and responses by manipulating variables called "request" and "response" which are Request objects and Response objects, respectively.

    Users who can create or modify code obtain access to the broader Managed Fusion environment. This access can be used to create intentional or unintentional damage to Managed Fusion.

    The JavaScript engine used by Managed Fusion

    The default JavaScript engine used by Managed Fusion is the Nashorn engine from Oracle. See The Nashorn Java API for details.

    In Managed Fusion 5.9.6 and up, you also have the option to select OpenJDK Nashorn. While Nashorn is the default option, it is in the process of being deprecated and will eventually be removed, so it is recommended to use OpenJDK Nashorn when possible. You can select the JavaScript engine in the pipeline views or in the workbenches. Your JavaScript pipeline stages are interpreted by the selected engine.

    JavaScript engine selector

    JavaScript Query Stage global variables

    JavaScript is a lightweight scripting language. The JavaScript in a JavaScript stage is standard ECMAScript. What a JavaScript program can do depends on the container in which it runs. For a JavaScript Query stage, the container is a Managed Fusion query pipeline. The following global pipeline variables are available:

    Name Type Description

    request

    The request variable contains Solr query information and is referred to as a regular request. A regular query request does not include Search DSL (domain specific language) parameters.

    request

    The request variable is also used when the query contains parameters for a Search DSL (domain specific language) request.

    See Domain Specific Language for more information.

    response

    The response variable contains Solr response information, and is used to return information from a regular request. Because a regular request does not include Search DSL (domain specific language) parameters, the response will not return Search DSL results.

    response

    The response variable is also used to return information from a Search DSL (domain specific language) request.

    See Domain Specific Language for more information.

    ctx

    A map that stores miscellaneous data created by each stage of the pipeline.

    Use the ctx variable instead of the deprecated _context global variable.

    The ctx variable is used to:

    • Pass data from one stage to another

    • Store data that needs to be passed from one custom stage to a later custom stage

    The data can differ between stages:

    • If the previous stage changes the data

    • Based on the configuration of each stage

    If the data is modified in one stage, it may cause a later stage to function irregularly.

    collection

    String

    The name of the Managed Fusion collection being indexed or queried.

    solrServer

    The Solr server instance that manages the pipeline’s default Managed Fusion collection. All indexing and query requests are done by calls to methods on this object. See solrClient for details.

    solrServerFactory

    The SolrCluster server used for lookups by collection name which returns a Solr server instance for that collection, e.g.
    var productsSolr = solrServerFactory.getSolrServer("products");.

    QueryRequestAndResponse

    Used to create query pipeline requests and responses.

    JSONResponse

    Returns list of info as a string.

    See Custom JavaScript Query Stage examples for more information.

    Syntax variants

    JavaScript stages can be written using legacy syntax or function syntax. The key difference between these syntax variants is how the "global variables" are used and interpreted. While using legacy syntax, these variables are used as global variables. With function syntax, however, these variables are passed as function parameters.

    Legacy syntax

    request.addParam("foo", "bar");

    Function syntax

    function(request,response) {
       request.addParam("foo", "bar");
    }
    Function syntax is used for the examples in this document.

    Global variable logger

    The logs are output to the query service logs for custom query stages. Access the Log Viewer and filter on this service to view the information.

    JavaScript Query Stage global variables

    JavaScript is a lightweight scripting language. The JavaScript in a JavaScript stage is standard ECMAScript. What a JavaScript program can do depends on the container in which it runs. For a JavaScript Query stage, the container is a Managed Fusion query pipeline. The following global pipeline variables are available:

    Name Type Description

    request

    The request variable contains Solr query information and is referred to as a regular request. A regular query request does not include Search DSL (domain specific language) parameters.

    request

    The request variable is also used when the query contains parameters for a Search DSL (domain specific language) request.

    See Domain Specific Language for more information.

    response

    The response variable contains Solr response information, and is used to return information from a regular request. Because a regular request does not include Search DSL (domain specific language) parameters, the response will not return Search DSL results.

    response

    The response variable is also used to return information from a Search DSL (domain specific language) request.

    See Domain Specific Language for more information.

    ctx

    A map that stores miscellaneous data created by each stage of the pipeline.

    Use the ctx variable instead of the deprecated _context global variable.

    The ctx variable is used to:

    • Pass data from one stage to another

    • Store data that needs to be passed from one custom stage to a later custom stage

    The data can differ between stages:

    • If the previous stage changes the data

    • Based on the configuration of each stage

    If the data is modified in one stage, it may cause a later stage to function irregularly.

    collection

    String

    The name of the Managed Fusion collection being indexed or queried.

    solrServer

    The Solr server instance that manages the pipeline’s default Managed Fusion collection. All indexing and query requests are done by calls to methods on this object. See solrClient for details.

    solrServerFactory

    The SolrCluster server used for lookups by collection name which returns a Solr server instance for that collection, e.g.
    var productsSolr = solrServerFactory.getSolrServer("products");.

    QueryRequestAndResponse

    Used to create query pipeline requests and responses.

    JSONResponse

    Returns list of info as a string.

    See Custom JavaScript Query Stage examples for more information.

    Syntax variants

    JavaScript stages can be written using legacy syntax or function syntax. The key difference between these syntax variants is how the "global variables" are used and interpreted. While using legacy syntax, these variables are used as global variables. With function syntax, however, these variables are passed as function parameters.

    Legacy syntax

    request.addParam("foo", "bar");

    Function syntax

    function(request,response) {
       request.addParam("foo", "bar");
    }
    Function syntax is used for the examples in this document.

    Global variable logger

    The logs are output to the query service logs for custom query stages. Access the Log Viewer and filter on this service to view the information.

    Configuration

    When entering configuration values in the UI, use unescaped characters, such as \t for the tab character. When entering configuration values in the API, use escaped characters, such as \\t for the tab character.

    Manipulate the request using JavaScript

    skip - boolean

    Set to true to skip this stage.

    Default: false

    label - string

    A unique label for this stage.

    <= 255 characters

    condition - string

    Define a conditional script that must result in true or false. This can be used to determine if the stage should process or not.

    script - stringrequired

    Enter your JavaScript code here. Your function does not need to return anything, all request and response modification is done in-place.

    Default: function (request, response, ctx) { // add your logic here }

    shareState - boolean

    Causes all instances of this stage to share global state. Enabling this will increase performance, but can lead to unexpected behavior if any variables are declared globally. This is safe to use if you declare all variables explicitly using 'var'.

    Default: true