The JavaScript Query stage allows you to write a 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 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 Fusion query pipeline.
The following global pipeline variables are available:

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

Global variable logger

The global variable named logger writes messages to the log file of the server running the pipeline.
Since Fusion’s api service does the query pipeline processing, these log messages go into the log file: fusion/4.2.x/var/log/api/api.log.
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".

JavaScript Query Stage Examples

Add a parameter to the query request

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

Add a parameter to the query response

This example contains a simple JavaScript function which adds information to the query response.
Repeated calls to this function build out the response.

Debugging and Troubleshooting

Use the logger object for print debugging (in the Fusion log file api.log).

Use the Pipeline Preview tool (only available in Fusion 1.x)

The JavaScript Engine Used by Fusion

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

Upgrading to the latest Nashorn engine

The default version of the Nashorn engine used by Fusion versions 2.4.1 and earlier
is the nashorn-0.1-jdk7.jar which contains many bugs that have since been fixed in the official JDK 1.8 version.
In order to use the latest version of the Nashorn engine, you must: