Custom JavaScript Stages Global Variables
JavaScript Index 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 Index stage, the container is a Fusion index pipeline. The following global pipeline variables are available:
Name | Type | Description | ||||
---|---|---|---|---|---|---|
|
The contents of each document submitted to the pipeline. |
|||||
|
A map that stores miscellaneous data created by each stage of the pipeline.
The
The data can differ between stages:
|
|||||
|
String |
The name of the Fusion collection being indexed or queried. |
||||
|
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 |
|||||
|
The SolrCluster server used for lookups by collection name which returns a Solr server instance for that collection. For example: |
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. While using legacy syntax, these variables are used as global variables. With function syntax, however, these variables are passed as function parameters.
Function Syntax
Function syntax is used for moderately complex tasks.
function (doc) {
// do some work ...
return doc;
}
Function syntax is used for the examples in this document. |
Advanced Syntax
Advanced syntax is used for complex tasks and when multiple functions are needed.
/* globals Java, logger*/
(function () {
"use strict";
return function main(doc , ctx, collection, solrServer, solrServerFactory) {
// do some work ...
return doc;
};
})();
JavaScript Use
The JavaScript in a JavaScript Index stage must return either a single document or an array of documents. This can be accomplished by either:
-
a series of statements where the final statement evaluates to a document or array of documents
-
a function that returns a document or an array of documents
All pipeline variables referenced in the body of the JavaScript function are passed in as arguments to the function. For example, in order to access the PipelineDocument in global variable 'doc', the JavaScript function is written as follows:
function (doc) {
// do some work ...
return doc;
}
The allowed set of function declarations are:
function doWork(doc) { ... return doc; }
function doWork(doc, ctx) { ... return doc; }
function doWork(doc, ctx, collection) { ... return doc; }
function doWork(doc, ctx, collection, solrServer) { ... return doc; }
function doWork(doc, ctx, collection, solrServer, SolrClusterComponent) { ... return doc; }
The order of these arguments is according to the (estimated) frequency of use. The assumption is that most processing only requires access to the document object itself, and the next-most frequent type of processing requires only the document and read-only access of some context parameters. If you need to reference the solrServerFactory global variable, you must use the 5-arg function declaration.
In order to use other functions in your JavaScript program, you can define and use them, as long as the final statement in the program returns a document or documents.
Global variable logger
The logs are output to the indexing service logs for custom index 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 Fusion query pipeline. The following global pipeline variables are available:
Name | Type | Description | ||||
---|---|---|---|---|---|---|
|
The |
|||||
|
The
|
|||||
|
The |
|||||
|
The
|
|||||
|
A map that stores miscellaneous data created by each stage of the pipeline.
The
The data can differ between stages:
|
|||||
|
String |
The name of the Fusion collection being indexed or queried. |
||||
|
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. |
|||||
|
The SolrCluster server used for lookups by collection name which returns a Solr server instance for that collection, e.g. |
|||||
|
Used to create query pipeline requests and responses. |
|||||
|
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. While using legacy syntax, these variables are used as global variables. With function syntax, however, these variables are passed 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 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.