---
title: "JavaScript Indexes"
url: "https://docs.ravendb.net/6.2/indexes/javascript-indexes"
version: "6.2"
language: "java"
---

# JavaScript Indexes

> Client language: Java. Also available for [C#](https://docs.ravendb.net/6.2/indexes/javascript-indexes.md).
>
> RavenDB publishes an official agent skill for coding assistants at https://docs.ravendb.net/skills/ravendb/SKILL.md (also installable with `npx skills add ravendb/skills`). The skill describes itself as: "Read this before writing any RavenDB query, client call or /databases/ request. Covers server setup, document modelling, the Node.js, .NET and Python clients, RQL, indexes, vector search and AI agents. Code written from recall tends to run and return the wrong rows, because RQL resembles SQL and the clients take field names as plain strings."

This feature was created for users who want to create an index and prefer JavaScript over C#.  
JavaScript indexes can be defined by a user with lower permissions than the C# indexes (admin not required).  
All other capabilities and features are the same as C# indexes.

## Creating JavaScript index

If we want to create JavaScript index we need to create an instance of our class that inherits
from AbstractJavaScriptIndexCreationTask.  
AbstractJavaScriptIndexCreationTask inherits from AbstractIndexCreationTask
(Read more about AbstractIndexCreationTask [here](https://docs.ravendb.net/6.2/indexes/creating-and-deploying.java.md#Using-AbstractIndexCreationTask).)

```java
public static class Employees_ByFirstAndLastName extends AbstractJavaScriptIndexCreationTask {
  // ...
}
```

## Map index

`Map` indexes, sometimes referred to as simple indexes, contain one (or more) mapping functions that indicate which fields from the documents should be indexed.
They indicate which documents can be searched by which fields.

```java
map(<collection-name>, function (document){
   return {
       // indexed properties go here e.g:
       // name: document.name
   };
})
```

### Example I - Simple map index

```java
public static class Employees_ByFirstAndLastName extends AbstractJavaScriptIndexCreationTask {
  public Employees_ByFirstAndLastName() {
      setMaps(Sets.newHashSet("map('Employees', function (employee){\n" +
          "                return {\n" +
          "                    FirstName : employee.FirstName,\n" +
          "                    LastName : employee.LastName\n" +
          "                };\n" +
          "        })"));
  }
}
```

### Example II - Map index with additional sources

```java
public static class BlogPosts_ByCommentAuthor extends AbstractJavaScriptIndexCreationTask {
  public BlogPosts_ByCommentAuthor() {
      setMaps(Sets.newHashSet("map('BlogPosts', function(b){\n" +
          "            var names = [];\n" +
          "            b.comments.forEach(x => getNames(x, names));\n" +
          "                return {\n" +
          "                   authors : names\n" +
          "                };" +
          "            })"));

      java.util.Map<String, String> additionalSources = new HashMap<>();
      additionalSources.put("The Script", "function getNames(x, names){\n" +
          "        names.push(x.author);\n" +
          "        x.comments.forEach(x => getNames(x, names));\n" +
          "    }");

      setAdditionalSources(additionalSources);
  }
}
```

Read more about map indexes [here](https://docs.ravendb.net/6.2/indexes/map-indexes.java.md).

## Multi map index

Multi-Map indexes allow you to index data from multiple collections

### Example

```java
public static class Animals_ByName extends AbstractJavaScriptIndexCreationTask {
  public Animals_ByName() {
      setMaps(Sets.newHashSet(
          "map('cats', function (c){ return {name: c.name}})",
          "map('dogs', function (d){ return {name: d.name}})"
      ));
  }
}
```

Read more about multi map indexes [here](https://docs.ravendb.net/6.2/indexes/map-reduce-indexes.java.md).

## Map-Reduce index

Map-Reduce indexes allow you to perform complex aggregations of data.
The first stage, called the map, runs over documents and extracts portions of data according to the defined mapping function(s).
Upon completion of the first phase, reduction is applied to the map results and the final outcome is produced.

```java
groupBy(x => {map properties})
   .aggregate(y => {
       return {
           // indexed properties go here e.g:
           // name: y.name
       };
   })
```

### Example I

```java
public static class Products_ByCategory extends AbstractJavaScriptIndexCreationTask {
  public Products_ByCategory() {
      setMaps(Sets.newHashSet("map('products', function(p){\n" +
          "            return {\n" +
          "                Category: load(p.Category, 'Categories').Name,\n" +
          "                Count: 1\n" +
          "            }\n" +
          "        })"));

      setReduce("groupBy(x => x.Category)\n" +
          "    .aggregate(g => {\n" +
          "        return {\n" +
          "            Category: g.key,\n" +
          "            Count: g.values.reduce((count, val) => val.Count + count, 0)\n" +
          "        };\n" +
          "    })");
  }
}
```

### Example II

```java
public static class Product_Sales_ByMonth extends AbstractJavaScriptIndexCreationTask {
  public Product_Sales_ByMonth() {
      setMaps(Sets.newHashSet("map('orders', function(order){\n" +
          "            var res = [];\n" +
          "            order.Lines.forEach(l => {\n" +
          "            res.push({\n" +
          "                Product: l.Product,\n" +
          "                Month: new Date( (new Date(order.OrderedAt)).getFullYear(),(new Date(order.OrderedAt)).getMonth(),1),\n" +
          "                Count: 1,\n" +
          "                Total: (l.Quantity * l.PricePerUnit) * (1- l.Discount)\n" +
          "            })\n" +
          "        });\n" +
          "        return res;\n" +
          "    })"));

      setReduce("groupBy(x => ({Product: x.Product, Month: x.Month}))\n" +
          "    .aggregate(g => {\n" +
          "        return {\n" +
          "            Product: g.key.Product,\n" +
          "            Month: g.key.Month,\n" +
          "            Count: g.values.reduce((sum, x) => x.Count + sum, 0),\n" +
          "            Total: g.values.reduce((sum, x) => x.Total + sum, 0)\n" +
          "        }\n" +
          "    })");

      setOutputReduceToCollection("MonthlyProductSales");
  }
}
```

Read more about map reduce indexes [here](https://docs.ravendb.net/6.2/indexes/multi-map-indexes.java.md).

> **Info:** Information {#information}
>
> Supported JavaScript version : ECMAScript 5.1

## See also

- [Indexing Related Documents](https://docs.ravendb.net/6.2/indexes/indexing-related-documents.java.md)
- [Map Indexes](https://docs.ravendb.net/6.2/indexes/map-indexes.java.md)
- [Map-Reduce Indexes](https://docs.ravendb.net/6.2/indexes/map-reduce-indexes.java.md)
- [Creating and Deploying Indexes](https://docs.ravendb.net/6.2/indexes/creating-and-deploying.java.md)
- [Query Overview](https://docs.ravendb.net/6.2/client-api/session/querying/how-to-query.md)
- [Querying an Index](https://docs.ravendb.net/6.2/indexes/querying/query-index.java.md)
- [Basics](https://docs.ravendb.net/6.2/indexes/querying/query-index.java.md)
