---
title: "Indexing Hierarchical Data"
url: "https://docs.ravendb.net/6.2/indexes/indexing-hierarchical-data"
version: "6.2"
language: "java"
---

# Indexing Hierarchical Data

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

One of the greatest advantages of a document database is that we have very few limits on how we structure our data.  
One very common scenario is the usage of hierarchical data structures.  
The most trivial of them is the comment thread:

```java
public static class BlogPost {
  private String author;
  private String title;
  private String text;
  private List<BlogPostComment> comments;

  public String getAuthor() {
      return author;
  }

  public void setAuthor(String author) {
      this.author = author;
  }

  public String getTitle() {
      return title;
  }

  public void setTitle(String title) {
      this.title = title;
  }

  public String getText() {
      return text;
  }

  public void setText(String text) {
      this.text = text;
  }

  public List<BlogPostComment> getComments() {
      return comments;
  }

  public void setComments(List<BlogPostComment> comments) {
      this.comments = comments;
  }
}

public static class BlogPostComment {
  private String author;
  private String text;
  private List<BlogPostComment> comments;


  public String getAuthor() {
      return author;
  }

  public void setAuthor(String author) {
      this.author = author;
  }

  public String getText() {
      return text;
  }

  public void setText(String text) {
      this.text = text;
  }

  public List<BlogPostComment> getComments() {
      return comments;
  }

  public void setComments(List<BlogPostComment> comments) {
      this.comments = comments;
  }
}
```

While it is very easy to work with such a structure in all respects, it does bring up an interesting question,
namely how can we search for all blog posts that were commented by specified author?

The answer to that is that RavenDB contains built-in support for indexing hierarchies,
and you can take advantage of the `Recurse` method to define an index using the following syntax:

## AbstractIndexCreationTask

```java
public static class BlogPosts_ByCommentAuthor extends AbstractIndexCreationTask {
  public BlogPosts_ByCommentAuthor() {
      map = "docs.BlogPosts.Select(post => new { " +
          "    authors = this.Recurse(post, x => x.comments).Select(x0 => x0.author) " +
          "})";
  }
}
```

## Operation

```java
IndexDefinition indexDefinition = new IndexDefinition();
indexDefinition.setName("BlogPosts/ByCommentAuthor");
indexDefinition.setMaps(Collections.singleton(
  "from post in docs.Posts" +
      "  from comment in Recurse(post, (Func<dynamic, dynamic>)(x => x.comments)) " +
      "  select new " +
      "  { " +
      "      author = comment.author " +
      "  }"
));
store.maintenance().send(new PutIndexesOperation(indexDefinition));
```

## JavaScript

```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);
  }
}
```

This will index all the comments in the thread, regardless of their location in the hierarchy.

```java
List<BlogPost> results = session
  .query(BlogPost.class, BlogPosts_ByCommentAuthor.class)
  .whereEquals("authors", "Ayende Rahien")
  .toList();
```

## See also

- [Indexing Basics](https://docs.ravendb.net/6.2/indexes/indexing-basics.java.md)
- [Indexing Related Documents](https://docs.ravendb.net/6.2/indexes/indexing-related-documents.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)
