---
title: "Indexing Polymorphic Data"
url: "https://docs.ravendb.net/6.2/indexes/indexing-polymorphic-data"
version: "6.2"
language: "csharp"
---

# Indexing Polymorphic Data

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

> **Note**
>
> - By default, RavenDB indexes are defined on a specific entity type, referred to as a `Collection`,  
>   and do not consider the inheritance hierarchy.
>
> - In this Page:
>   - [Polymorphic Data](#polymorphic-data)
>     - [Multi-Map Indexes](#multi-map-indexes)
>     - [Other Options](#other-options)

## Polymorphic Data

Let's assume, for example, that we have the following inheritance hierarchy:

![Figure 1: Polymorphic indexes](https://docs.ravendb.net/assets/images/polymorphic_indexes_faq-58d4920423a9e5a92985e2844c906d35.png)

When saving a `Cat` document, it will be assigned to the "Cats" collection,  
while a `Dog` document will be placed in the "Dogs" collection.

If we intend to create a simple Map-index for Cat documents based on their names, we would write:

```csharp
from cat in docs.Cats
select new { cat.Name }
```

And for dogs:

```csharp
from dog in docs.Dogs
select new { dog.Name }
```

> **Info:** The challenge {#the-challenge}
>
> Querying each index results in documents only from the specific collection the index was defined for.  
> However, what if we need to query across ALL animal collections?

## Multi-Map Indexes

The easiest way to do this is by writing a multi-map index such as:

### MultiMap

```csharp
public class Animals_ByName : AbstractMultiMapIndexCreationTask
{
  public Animals_ByName()
  {
      AddMap<Cat>(cats => from c in cats select new { c.Name });

      AddMap<Dog>(dogs => from d in dogs select new { d.Name });
  }
}
```

### MultiMapJavaScript

```csharp
public class Animals_ByName : AbstractJavaScriptIndexCreationTask
{
  public Animals_ByName()
  {
      Maps = new HashSet<string>()
      {
          @"map('cats', function (c){ return {Name: c.Name}})",
          @"map('dogs', function (d){ return {Name: d.Name}})"
      };
  }
}
```

And query it like this:

### Query

```csharp
IList<IAnimal> results = session
  .Query<IAnimal, Animals_ByName>()
  .Where(x => x.Name == "Mitzy")
  .ToList();
```

### DocumentQuery

```csharp
List<IAnimal> results = session
  .Advanced
  .DocumentQuery<IAnimal, Animals_ByName>()
  .WhereEquals("Name", "Mitzy")
  .ToList();
```

### RQL

```sql
from index 'Animals/ByName'
where Name = 'Mitzy'
```

## Other Options

Another option would be to modify the way we generate the Collection for subclasses of `Animal`:

```csharp
DocumentStore store = new DocumentStore()
{
  Conventions =
  {
      FindCollectionName = type =>
      {
          if (typeof(Animal).IsAssignableFrom(type))
              return "Animals";
          return DocumentConventions.DefaultGetCollectionName(type);
      }
  }
};
```

Using this method, we can now index on all animals using:

```csharp
from animal in docs.Animals
select new { animal.Name }
```

But what happens when you don't want to modify the entity name of an entity itself?

You can create a polymorphic index using:

```csharp
from animal in docs.WhereEntityIs("Cats", "Dogs")
select new { animal.Name }
```

It will generate an index that matches both Cats and Dogs.

## See also

- [Indexing basics](https://docs.ravendb.net/6.2/indexes/indexing-basics.md)
- [Indexing related documents](https://docs.ravendb.net/6.2/indexes/indexing-related-documents.md)
- [Indexing spatial data](https://docs.ravendb.net/6.2/indexes/indexing-spatial-data.md)
- [Indexing hierarchical data](https://docs.ravendb.net/6.2/indexes/indexing-hierarchical-data.md)
- [Query overview](https://docs.ravendb.net/6.2/client-api/session/querying/how-to-query.md)
- [Basics](https://docs.ravendb.net/6.2/indexes/querying/query-index.md)
