> ## 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 Global Variables

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/reference/config-ref/pipeline-stages/custom-javascript-stages-global-variables

[mintlify link]: https://doc.lucidworks.com/docs/5/fusion/reference/config-ref/pipeline-stages/custom-javascript-stages-global-variables

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

<LwTemplate />

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

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                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  |
| ------------------- | ------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `doc`               | [PipelineDocument](https://javadoc.lucidworks.com/fusion-pipeline-javadocs/5.9/com/lucidworks/apollo/common/pipeline/PipelineDocument.html) | The contents of each document submitted to the pipeline.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     |
| `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. <br /> <Tip> **Important**<br /> Use the `ctx` variable instead of the deprecated `_context` global variable. </Tip><br /> The `ctx` variable is used to: <br />● Pass data from one stage to another<br />● Store data that needs to be passed from one custom stage to a later custom stage  The data can differ between stages: <br />● If the previous stage changes the data<br />● Based on the configuration of each stage <br /><Note> If the data is modified in one stage, it may cause a later stage to function irregularly. </Note> |
| `collection`        | String                                                                                                                                      | The name of the Fusion collection being indexed or queried.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  |
| `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                                                                                                                                                                                                                                                                                                                                      |
| `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. For example:  `var productsSolr = solrServerFactory.getSolrServer("products");`.                                                                                                                                                                                                                                                                                                                                                                                                                        |

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

Function syntax is used for moderately complex tasks.

```js wrap  theme={"dark"}
function (doc) {
    // do some work ...
    return doc;
}
```

<Tip>
  **Important**

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

#### Advanced Syntax

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

```js wrap  theme={"dark"}
/* 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:

```js wrap  theme={"dark"}
function (doc) {
    // do some work ...
    return doc;
}
```

The allowed set of function declarations are:

```js wrap  theme={"dark"}
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](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.

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                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     |
| ------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `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.                                                                                                                                                                                                                                                                                                                                                                                                                |
| `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. <br /><Note> See [Domain Specific Language](/docs/5/fusion/getting-data-out/query-basics/domain-specific-language) for more information. </Note>                                                                                                                                                                                                                                                                                                                                    |
| `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.                                                                                                                                                                                                                                                                                                                                       |
| `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. <br /><Note> See [Domain Specific Language](/docs/5/fusion/getting-data-out/query-basics/domain-specific-language) for more information. </Note>                                                                                                                                                                                                                                                                                                                                               |
| `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**<br /> Use the `ctx` variable instead of the deprecated `_context` global variable. </Tip> The `ctx` variable is used to: <br />● Pass data from one stage to another<br />● Store data that needs to be passed from one custom stage to a later custom stage  The data can differ between stages: <br />● If the previous stage changes the data<br />● Based on the configuration of each stage <br /><Note> If the data is modified in one stage, it may cause a later stage to function irregularly. </Note> |
| `collection`              | String                                                                                                                                                   | The name of the Fusion collection being indexed or queried.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     |
| `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.                                                                                                                                                                                                                                                                                                                        |
| `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");`.                                                                                                                                                                                                                                                                                                                                                                                                                   |
| `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.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           |
| `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.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               |

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