- 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 anullvalue 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.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
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.
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 theJava.type() function:
Examples
Set the condition field
The JavaScript Index stage lets you define a condition to trigger the script body.
if or include ; at the end of the line.
-
Works:
doc.hasField("title_s") === truedoc.hasField("title_s") === falsedoc.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 theconditions 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 returnsnull 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.
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.