Skip to main content
The JavaScript Index stage allows you to write a custom processing logic using JavaScript to manipulate Pipeline Documents and the index pipeline context, which will be compiled by the JDK into Java bytecode that is executed by the Fusion pipeline. The first time that the pipeline is run, Fusion compiles the JavaScript program into Java bytecode using the JDK’s JavaScript engine.
Users who can create or modify code obtain access to the broader Fusion environment. This access can be used to create intentional or unintentional damage to Fusion.
For a JavaScript Index stage, the JavaScript code must return either:
  • A single document or array of documents or
  • The null value or an empty array.
    In the latter case, no further processing is possible, which means that the document will not be indexed or updated. For example, Solr commits have a null value and are dropped. For information about how to skip stages when Solr commits are sent, see the Skip JavaScript stages during Solr commits.

JavaScript Index Stage Global Variables

JavaScript is a lightweight scripting language. In a JavaScript stage, Fusion uses the Nashorn engine, which implements ECMAScript version 5.1. 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:

Syntax Variants

JavaScript stages can be written using function syntax. With function syntax, global variables are passed as function parameters.
Support for legacy syntax was removed in Fusion 5.8.

Function Syntax

Function syntax is used for moderately complex tasks.
ImportantFunction syntax is used for the examples in this document.

Advanced Syntax

Advanced syntax is used for complex tasks and when multiple functions are needed.

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:
The allowed set of function declarations are:
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.

The JavaScript Engine Used by Fusion

The default JavaScript engine used by Fusion is the Nashorn engine from Oracle. See The Nashorn Java API for details. In 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

Creating and accessing Java types

The following information is taken from Oracle’s JavaScript programming guide section 3, Using Java From Scripts. To create script objects that access and reference Java types from Javascript use the Java.type() function:

Examples

Set the condition field

The JavaScript Index stage lets you define a condition to trigger the script body. Javascript Form The condition field is evaluated as either true or false. Do not precede with if or include ; at the end of the line.
  • Works:
    • doc.hasField("title_s") === true
    • doc.hasField("title_s") === false
    • doc.hasField("title_s")
  • Does not work:
    • if doc.hasField("title_s") === false;

Skip JavaScript stages during Solr commits

When JavaScript stages contain data manipulation such as values in the conditions field, errors may be generated and Solr commits are dropped because the value is null. To skip those stages when a Solr commit is sent through the pipeline, add the following condition to those stages: doc.getCommands().size()==0.

Add a field to a document

Join two fields

The following example conjoins separate latitude and longitude fields into a single geo-coordinate field, whose field name follows Solr schema conventions and ends in “_p”. It also removes the original latitude and longitude fields from the document.

Return an array of documents

Parse a JSON-escaped string into a JSON object

While it is simpler to use a JSON Parsing index stage, the following code example shows you how to parse a JSON-escaped string representation into a JSON object. This code parses a JSON object into an array of attributes, and then find the attribute “tags” which has as its value a list of strings. Each item in the list is added to a multi-valued document field named “tag_ss”.

Do a lookup on another Fusion collection

Reject a document

If the function returns null or an empty array, it will not be indexed or updated into Fusion.

Drop a document by ID

Format Date to Solr Date

Replace whitespace and newlines

Split the values in a field

Prevent global variables in JavaScript

Variable declared using the var keyword

If a variable is declared using the var keyword, the JavaScript interpreter processes the value sequentially. In this example, the values for var i = 0 are logged in order as 0, 1, 2, 3, 4, etc.

Variable not declared using the var keyword

If a variable is not declared using the var keyword, the JavaScript interpreter moves the declaration of variable and functions to the top of the declared (global) scope. Because Fusion pipeline stages execute in a multi-threaded environment, these global (shared) variables make the stages not thread-safe. For more detailed information, see Hoisting.
Issues and errors that may occur include: For multi-threaded environments, the value of i may not proceed sequentially from 0 to 4 as the loop is processed. Instead, values may be logged based on the execution state of the other pipeline requests. For example, 0, 1, 3, 1, 2, etc., which logs the values as "cat", "the cat", "the cat in the hat is back", "the cat", "the cat in the hat". However, if only one thread is incrementing the i variable, the values proceed sequentially (0, 1, 2, 3, 4, etc.) If the queries array varies in length from document to document, the loop may generate an ArrayIndexOutOfBounds exception for a Java array or an undefined error for a JavaScript array. Threads may not log all four queries.

Setting the "use strict" directive

Setting the "use strict" directive tells the JavaScript engine to require non-global declarations of all functions and variables. The following example demonstrates how to create a copy of a PipelineDocument and return both the original and the copy to the pipeline for processing.

JavaScript examples

Set up a reusable library

This is an example of a reusable JavaScript library that provides various utility functions for string processing, date handling, and type conversion. To reuse Javascript functions, create a stage like this one that defines them, then place it before any of the stages that use it.

Learn more

JavaScript in Fusion

The course for JavaScript in Fusion focuses on how to leverage JavaScript in Fusion to build powerful and responsive scripts at index and query time.

See also

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.