---
title: "Indexes: Indexing Related Documents"
url: "https://docs.ravendb.net/6.2/indexes/indexing-related-documents"
version: "6.2"
language: "java"
---

# Indexes: Indexing Related Documents

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

To extend indexing capabilities and simplify many scenarios, we have introduced the possibility for indexing related documents.

## Example I

Let's consider a simple `Product - Category` scenario where you want to look for a `Product` by `Category Name`.

Without this feature, you would have to create a fairly complex multiple map-reduce index. This is why the `LoadDocument` function was introduced.

### AbstractIndexCreationTask

```java
public static class Products_ByCategoryName extends AbstractIndexCreationTask {
  public Products_ByCategoryName() {
      map = "docs.Products.Select(product => new { " +
          "    CategoryName = (this.LoadDocument(product.Category, \"Categories\")).Name " +
          "})";
  }
}
```

### Operation

```java
IndexDefinition indexDefinition = new IndexDefinition();
indexDefinition.setName("Products/ByCategoryName");
indexDefinition.setMaps(Collections.singleton("from product in products " +
  "   select new " +
  "   { " +
  "       CategoryName = LoadDocument(product.Category, \"\"Categories\"\").Name " +
  "   }"));

store.maintenance().send(new PutIndexesOperation(indexDefinition));
```

### JavaScript

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

Now we will be able to search for products using the `CategoryName` as a parameter:

```java
List<Product> results = session
  .query(Product.class, Products_ByCategoryName.class)
  .whereEquals("CategoryName", "Beverages")
  .toList();
```

## Example II

Our next scenario will show us how indexing of more complex relationships is also trivial. Let's consider the following case:

```java
public static class Book {
  private String id;
  private String name;

  public String getId() {
      return id;
  }

  public void setId(String id) {
      this.id = id;
  }

  public String getName() {
      return name;
  }

  public void setName(String name) {
      this.name = name;
  }
}

public static class Author {
  private String id;
  private String name;
  private List<String> bookIds;

  public String getId() {
      return id;
  }

  public void setId(String id) {
      this.id = id;
  }

  public String getName() {
      return name;
  }

  public void setName(String name) {
      this.name = name;
  }

  public List<String> getBookIds() {
      return bookIds;
  }

  public void setBookIds(List<String> bookIds) {
      this.bookIds = bookIds;
  }
}
```

To create an index with `Author Name` and list of `Book Names`, we need do the following:

### AbstractIndexCreationTask

```java
public static class Authors_ByNameAndBooks extends AbstractIndexCreationTask {
  public Authors_ByNameAndBooks() {
      map = "docs.Authors.Select(author => new { " +
          "    name = author.name, " +
          "    books = author.bookIds.Select(x => (this.LoadDocument(x, \"Books\")).name) " +
          "})";
  }
}
```

### Operation

```java
IndexDefinition indexDefinition = new IndexDefinition();
indexDefinition.setName("Authors/ByNameAndBooks");
indexDefinition.setMaps(Collections.singleton("from author in docs.Authors " +
  "     select new " +
  "     { " +
  "         name = author.name, " +
  "         books = author.bookIds.Select(x => LoadDocument(x, \"\"Books\"\").id) " +
  "     }"));
store.maintenance().send(new PutIndexesOperation(indexDefinition));
```

### JavaScript

```java
public static class Authors_ByNameAndBookNames extends AbstractJavaScriptIndexCreationTask {
  public Authors_ByNameAndBookNames() {
      setMaps(Sets.newHashSet("map('author', function(a){\n" +
          "            return {\n" +
          "                name: a.name,\n" +
          "                books: a.booksIds.forEach(x => load(x, 'Book').name)\n" +
          "            }\n" +
          "        })"));
  }
}
```

```java
List<Author> results = session
  .query(Author.class, Authors_ByNameAndBooks.class)
  .whereEquals("name", "Andrzej Sapkowski")
  .whereEquals("books", "The Witcher")
  .toList();
```

## Remarks

> **Info**
>
> Indexes are updated automatically when related documents change.

> **Warning**
>
> Using the `LoadDocument` adds a loaded document to the tracking list. This may cause very expensive calculations to occur, especially when multiple documents are tracking the same document.

## See also

- [Indexing Basics](https://docs.ravendb.net/6.2/indexes/indexing-basics.java.md)
- [Indexing Hierarchical Data](https://docs.ravendb.net/6.2/indexes/indexing-hierarchical-data.java.md)
- [Indexing Spatial Data](https://docs.ravendb.net/6.2/indexes/indexing-spatial-data.java.md)
- [Indexing Polymorphic Data](https://docs.ravendb.net/6.2/indexes/indexing-polymorphic-data.java.md)
- [Basics](https://docs.ravendb.net/6.2/indexes/querying/query-index.java.md)
- [Query Overview](https://docs.ravendb.net/6.2/client-api/session/querying/how-to-query.md)
