---
title: "Indexing Nested data"
url: "https://docs.ravendb.net/6.2/indexes/indexing-nested-data"
version: "6.2"
language: "java"
---

# Indexing Nested data

> Client language: Java. Also available for [C#](https://docs.ravendb.net/6.2/indexes/indexing-nested-data.md), [Python](https://docs.ravendb.net/6.2/indexes/indexing-nested-data.python.md), [PHP](https://docs.ravendb.net/6.2/indexes/indexing-nested-data.php.md), [Node.js](https://docs.ravendb.net/6.2/indexes/indexing-nested-data.nodejs.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."

The fanout index is the index that outputs multiple index entries per each document. Here is an example of such one:

## LINQ

```java
public static class Orders_ByProduct extends AbstractIndexCreationTask {
  public Orders_ByProduct() {
      map = "docs.Orders.SelectMany(order => order.Lines, (order, orderLine) => new { " +
          "    Product = orderLine.Product, " +
          "    ProductName = orderLine.ProductName " +
          "})";
  }
}
```

## JavaScript

```java
public static class Orders_ByProduct extends AbstractJavaScriptIndexCreationTask {
  public Orders_ByProduct() {
      setMaps(Sets.newHashSet("map('Orders', function (order){\n" +
          "    var res = [];\n" +
          "    order.Lines.forEach(l => {\n" +
          "        res.push({\n" +
          "            Product: l.Product,\n" +
          "            ProductName: l.ProductName\n" +
          "        })\n" +
          "    });\n" +
          "    return res;\n" +
          "})"));
  }
}
```

A large order, having a lot of line items, will create an index entry per each `OrderLine` item from the `Lines` collection. A single document can generate hundreds of index entries.

The fanout index concept is not specific for map-only indexes. It also applies to map-reduce indexes:

## LINQ

```java
public static class Product_Sales extends AbstractIndexCreationTask {
  public Product_Sales() {
      map = "docs.Orders.SelectMany(order => order.Lines, (order, line) => new { " +
          "    Product = line.Product, " +
          "    Count = 1, " +
          "    Total = (((decimal) line.Quantity) * line.PricePerUnit) * (1M - line.Discount) " +
          "})";

      reduce = "results.GroupBy(result => result.Product).Select(g => new {\n" +
          "    Product = g.Key,\n" +
          "    Count = Enumerable.Sum(g, x => ((int) x.Count)),\n" +
          "    Total = Enumerable.Sum(g, x0 => ((decimal) x0.Total))\n" +
          "})";
  }
}
```

## JavaScript

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

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

The above index definitions are correct. In both cases this is actually what we want. However, you need to be aware that fanout indexes are typically more expensive than regular ones.
RavenDB has to index many more entries than usual. What can result is higher utilization of CPU and memory, and overall declining performance of the index.

> **Note:** Note {#note}
>
> Starting from version 4.0, the fanout indexes won't error when the number of index entries created from a single document exceeds the configured limit. The configuration options from 3.x:
>
> - `Raven/MaxSimpleIndexOutputsPerDocument`
> - `Raven/MaxMapReduceIndexOutputsPerDocument`
>
> are no longer valid.
>
> RavenDB will give you a performance hint regarding high fanout ratio using the Studio's notification center.

## Performance Hints

Once RavenDB notices that the number of indexing outputs created from a document is high, the notification that will appear in the Studio:

![Figure 1. High indexing fanout ratio notification](https://docs.ravendb.net/assets/images/fanout-index-performance-hint-1-5cb0b8230389f290be80379208488835.png)

The details will give you the following info:

![Figure 2. Fanout index, performance hint details](https://docs.ravendb.net/assets/images/fanout-index-performance-hint-2-00a4e7bf96dfe54a8ac2de03a7853c03.png)

You can control when a performance hint should be created using the `PerformanceHints.Indexing.MaxIndexOutputsPerDocument` setting (default: 1024).

## Paging

Since the fanout index creates multiple entries for a single document and queries return documents by default (it can change if the query defines the projection) the paging of query results
is a bit more complex. Please read the dedicated article about [paging through tampered results](https://docs.ravendb.net/6.2/indexes/querying/paging.java.md#paging-through-tampered-results).

## See also

- [What are Indexes](https://docs.ravendb.net/6.2/indexes/what-are-indexes.java.md)
- [Intersect queries](https://docs.ravendb.net/6.2/indexes/querying/intersection.java.md)
