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

# Indexing Polymorphic Data

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

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

And for dogs:

```php
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

```php
class Animals_ByName extends AbstractMultiMapIndexCreationTask
{
  public function __construct()
  {
      parent::__construct();

      $this->addMap("from c in docs.Cats select new { c.name }");
      $this->addMap("from d in docs.Dogs select new { d.name }");
  }
}
```

### MultiMapJavaScript

```php
class Animals_ByName extends AbstractJavaScriptIndexCreationTask
{
  public function __construct()
  {
      parent::__construct();

      $this->setMaps([
          "map('cats', function (c){ return {Name: c.Name}})",
          "map('dogs', function (d){ return {Name: d.Name}})"
      ]);
  }
}
```

And query it like this:

### Query

```php
/** @var array<Animal> $results */
$results = $session
  ->advanced()
  ->documentQuery(Animal::class, Animals_ByName::class)
  ->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`:

```php
$store = new DocumentStore();
$store->getConventions()->setFindCollectionName(
  function (?string $className): string {
      if (is_a($className, Animal::class)) {
          return "Animals";
      }
      return DocumentConventions::defaultGetCollectionName($className);
  }
);
```

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

```php
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:

```php
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.php.md)
- [Indexing spatial data](https://docs.ravendb.net/6.2/indexes/indexing-spatial-data.php.md)
- [Indexing hierarchical data](https://docs.ravendb.net/6.2/indexes/indexing-hierarchical-data.php.md)
- [Query overview](https://docs.ravendb.net/6.2/client-api/session/querying/how-to-query.php.md)
- [Basics](https://docs.ravendb.net/6.2/indexes/querying/query-index.php.md)
