Product Selector

Fusion 5.12
    Fusion 5.12

    JavaScript Query Stage

    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, 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.

    JavaScript Stages Variables and Parameters

    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 Fusion query pipeline. The following global pipeline variables are available:

    Name Type Description

    request

    The Solr query information.

    response

    The Solr response information.

    ctx

    A reference to the container which holds a map over the pipeline properties. Used to update or modify this information for downstream pipeline stages.

    collection

    String

    The name of the Fusion collection being indexed or queried.

    solrServer

    The Solr server instance that manages the pipeline’s default 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 a that collection. For example:
    var productsSolr = solrServerFactory.getSolrServer("products");.

    QueryRequestAndResponse

    Returns list of info as a string.

    Syntax Variants

    JavaScript stages are written using function syntax, which passes variables as function parameters.

    Function Syntax

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

    Global variable logger

    The global variable named logger writes messages to the log file of the server running the pipeline. This variable is truly global and does not need to be declared as part of the function parameter list.

    There are 5 methods available, which each take either a single argument (the string message to log) or two arguments (the string message and an exception to log). The five methods are, "debug", "info", "warn", and "error".

    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

    One context variable 'request' is visible to the script.

    shareState - boolean

    Causes all instances of this stage to share 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: false