> ## Documentation Index
> Fetch the complete documentation index at: https://doc.lucidworks.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Custom JavaScript Stages for Query Pipelines

export const LwTemplate = ({title = "Key questions to get you started", icon = "sparkles", cta = "Powered by Agent Studio", linkHref = "https://lucidworks.com/demo/?utm_source=docs&utm_medium=referral&utm_campaign=docs_cta_ai"}) => {
  const [isLoaded, setIsLoaded] = useState(false);
  useEffect(() => {
    const timer = setTimeout(() => {
      setIsLoaded(true);
    }, 500);
    return () => clearTimeout(timer);
  }, []);
  return <div className="lw-template-container">
      <Card title={title} icon={icon}>
        {isLoaded && <span dangerouslySetInnerHTML={{
    __html: `<lw-template id="a029c1a9-28be-427e-b0e1-5d918920246a"></lw-template
            >`
  }} />}
        <Link href={linkHref} className="agent-studio-link text-left text-gray-600 gap-2 dark:text-gray-400 text-sm font-medium flex flex-row items-center hover:text-primary dark:hover:text-primary-light group-hover:text-primary group-hover:dark:text-primary-light">Powered by Lucidworks Agent Studio</Link>
      </Card>
    </div>;
};

[localhost link]: http://localhost:3000/docs/5/fusion/getting-data-out/query-basics/query-pipelines/custom-javascript-query-stages

[mintlify link]: https://doc.lucidworks.com/docs/5/fusion/getting-data-out/query-basics/query-pipelines/custom-javascript-query-stages

[old doc.lw link]: https://doc.lucidworks.com/fusion/5.9/170

The JavaScript Query stage allows you to write 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](https://javadoc.lucidworks.com/fusion-pipeline-javadocs/5.9/com/lucidworks/apollo/pipeline/query/Request.html) and [Response objects](https://javadoc.lucidworks.com/fusion-pipeline-javadocs/5.9/com/lucidworks/apollo/pipeline/query/Response.html), respectively.

<Warning>
  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.
</Warning>

<LwTemplate />

## The JavaScript Engine Used by Fusion

The default JavaScript engine used by Fusion is the Nashorn engine from Oracle.
See [The Nashorn Java API](https://docs.oracle.com/javase/8/docs/technotes/guides/scripting/nashorn/api.html) 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.

<img src="https://mintcdn.com/lucidworks/sBy1WWIeb2aVbL1d/assets/images/5.9/5.9.6/js-engine-selector.png?fit=max&auto=format&n=sBy1WWIeb2aVbL1d&q=85&s=7c09e8b53421a421d450d3621bb31306" alt="JavaScript engine selector" width="1398" height="808" data-path="assets/images/5.9/5.9.6/js-engine-selector.png" />

## JavaScript Query Stage Global Variables

[JavaScript](http://en.wikipedia.org/wiki/JavaScript) is a lightweight scripting language.
In a JavaScript stage, Fusion uses the Nashorn engine, which implements [ECMAScript](http://en.wikipedia.org/wiki/ECMAScript) version 5.1. Although Nashorn does include some ECMAScript 6 (ES6) features such as `let`, `const`, or template strings, Fusion does not enable ES6 by default, so ES6 support is not guaranteed.

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.

<Accordion title="Global pipeline variables" defaultOpen>
  <ParamField path="request">
    [ Request for a Regular Query ](https://javadoc.lucidworks.com/fusion-pipeline-javadocs/5.9/com/lucidworks/apollo/pipeline/query/Request.html)   The `request` variable contains Solr query information and is referred to as a **regular** request. A regular query request does *not* include Search DSL (domain specific language) parameters.
  </ParamField>

  <ParamField path="request">
    [ DSL Request ](https://javadoc.lucidworks.com/fusion-pipeline-javadocs/5.9/com/lucidworks/search/dsl/request/DslRequest.html)   The `request` variable is also used when the query contains parameters for a Search DSL (domain specific language) request. <Note> See [Domain Specific Language](/docs/5/fusion/getting-data-out/query-basics/domain-specific-language) for more information. </Note>
  </ParamField>

  <ParamField path="response">
    [ Response for a Regular Query ](https://javadoc.lucidworks.com/fusion-pipeline-javadocs/5.9/com/lucidworks/apollo/pipeline/query/Response.html)   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 (domain specific language) parameters, the response will not return Search DSL results.
  </ParamField>

  <ParamField path="response">
    [ DSL Response ](https://javadoc.lucidworks.com/fusion-pipeline-javadocs/5.9/com/lucidworks/search/dsl/response/DslResponse.html)   The `response` variable is also used to return information from a Search DSL (domain specific language) request. <Note> See [Domain Specific Language](/docs/5/fusion/getting-data-out/query-basics/domain-specific-language) for more information. </Note>
  </ParamField>

  <ParamField path="ctx">
    [ Context ](https://javadoc.lucidworks.com/fusion-pipeline-javadocs/5.9/com/lucidworks/apollo/pipeline/Context.html)

    A map that stores miscellaneous data created by each stage of the pipeline.

    <Tip> **Important** Use the `ctx` variable instead of the deprecated `_context` global variable. </Tip>

    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

    The data can differ between stages:

    * If the previous stage changes the data
    * Based on the configuration of each stage

    <Note> If the data is modified in one stage, it may cause a later stage to function irregularly. </Note>
  </ParamField>

  <ParamField path="collection">
    String

    The name of the Fusion collection being indexed or queried.
  </ParamField>

  <ParamField path="solrServer">
    [ BufferingSolrServer ](https://javadoc.lucidworks.com/fusion-pipeline-javadocs/5.9/com/lucidworks/apollo/component/BufferingSolrServer.html)   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](https://lucene.apache.org/solr/5_2_1/solr-solrj/org/apache/solr/client/solrj/SolrClient.html) for details.
  </ParamField>

  <ParamField path="solrServerFactory">
    [ SolrClusterComponent ](https://javadoc.lucidworks.com/fusion-pipeline-javadocs/5.9/com/lucidworks/cloud/api/solr/SolrClusterComponent.html)\
    The SolrCluster server used for lookups by collection name which returns a Solr server instance for that collection, e.g. `var productsSolr = solrServerFactory.getSolrServer("products");`.
  </ParamField>

  <ParamField path="QueryRequestAndResponse">
    [ QueryRequestAndResponse ](https://javadoc.lucidworks.com/fusion-pipeline-javadocs/5.9/com/lucidworks/apollo/pipeline/query/QueryRequestAndResponse.html)   Used to create query pipeline requests and responses.
  </ParamField>

  <ParamField path="JSONResponse">
    [ JSONResponse ](https://javadoc.lucidworks.com/fusion-pipeline-javadocs/5.9/com/lucidworks/apollo/solr/response/JSONResponse.html)   Returns list of info as a string.
  </ParamField>
</Accordion>

<Note>
  See [Custom JavaScript Query Stage Examples](/docs/5/fusion/reference/example-custom-javascript-stages) for more information.
</Note>

### Syntax Variants

JavaScript stages can be written using ***function syntax***. With function syntax, global variables are passed as function parameters.

<Note>
  Support for legacy syntax was removed in Fusion 5.8.
</Note>

#### Function Syntax

```js wrap  theme={"dark"}
function(request,response) {
   request.addParam("foo", "bar");
}
```

<Tip>
  **Important**

  *Function syntax* is used for the examples in this document.
</Tip>

### 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.

## See also

* [Fusion Pipeline Javadocs](https://javadoc.lucidworks.com/fusion-pipeline-javadocs/5.9/)
* [JavaScript Query Stage](/docs/5/fusion/reference/config-ref/pipeline-stages/query-stages/javascript-query-stage)
* [Custom JavaScript Query Stage Examples](/docs/5/fusion/reference/example-custom-javascript-stages)
* [Custom JavaScript Stages Global Variables](/docs/5/fusion/reference/config-ref/pipeline-stages/custom-javascript-stages-global-variables)
* [Managed JavaScript Query Stage](/docs/5/fusion/reference/config-ref/pipeline-stages/query-stages/managed-javascript-query-stage)

<Card title="JavaScript in Fusion" class="note-image" href="https://academy.lucidworks.com/javascript-in-fusion" cta="Take this course on the LucidAcademy." icon="graduation-cap" iconType="duotone">
  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.
</Card>
