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 Query 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 Query stage, the container is a Fusion query pipeline. The following global pipeline variables are available:Request for a Regular Query
The
request variable contains Solr query information and represents a regular request. A regular query request does not include Search DSL parameters.Response for a Regular Query
The
response variable contains Solr response information and is used to return information from a regular request. Because a regular request does not include Search DSL parameters, the response will not return DSL results.Context
The
ctx variable is a map that stores data created by each stage of the pipeline. Use it to pass data from one stage to another and to store data that needs to be read by later custom stages. The contents can change between stages based on earlier modifications and on each stage’s configuration.- The ctx variable is used to:
- Pass data from one stage to another
- Store data that needs to be passed from one custom stage to a later custom stage
- If the previous stage changes the data
- Based on the configuration of each stage
If a stage modifies shared data, a later stage may behave unexpectedly.
string
The
collection variable is the name of the Lucidworks Search collection being indexed or queried.BufferingSolrServer
The
solrServer instance manages the pipeline’s default Lucidworks Search collection. All indexing and query requests are made through this object. See the SolrJ solrClient for details.SolrClusterComponent
The
solrServerFactory provides lookups by collection name and returns a Solr server instance for that collection.Get a Solr server for a collection
QueryRequestAndResponse
Use
QueryRequestAndResponse to create query pipeline requests and responses.JSONResponse
JSONResponse returns lists of information as a string.See Custom JavaScript Query Stage Examples for more information.
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
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.
Query pipeline stage condition examples
Stages can be triggered conditionally when a script in the Condition field evaluates to true. Some examples are shown below. Run this stage only for mobile clients: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.Call reusable JavaScript functions
This example demonstrates how to call reusable JavaScript functions from a library like the one in the example above. A JavaScript stage that calls these functions must be placed after the one that loads the library into the context. This example also adds debug logging to the context so that it can be appended to the response and viewed in Query Workbench.Bury documents in search results
This example enables aproductsToBury parameter that allows you to specify a list of product IDs to “bury” or demote in search results.
Manipulate the response object
This example demonstrates how to add a new field to the response that contains the lowercase version of an existing field. It uses theAbstractResponse class to manipulate response documents, which works for both XML and JSON responses for versatility.
Boost the products in a list
This example demonstrates how to boost specific products in search results based on a comma-separated list of product IDs. It uses thebq parameter to apply boosts, which allows for precise ranking control in search results.
The boost values are linearly distributed between a maximum and minimum value, allowing for a smooth decrease in boost as the list progresses.
This is useful for merchandising control, promotional campaigns, or A/B testing different product arrangements.
Boost products using Solr’s Query Elevation Component (QEC)
This example demonstrates how to elevate specific products in search results using the Query Elevation Component (QEC) in Solr. It converts a comma-separated list of product IDs into theelevateIds parameter format required by Solr’s QEC, allowing specific documents to be promoted to the top of search results.
This is useful for merchandising control, promotional campaigns, or editorial control over search result ranking.