> ## Documentation Index
> Fetch the complete documentation index at: https://algolia.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Multiple models in one index

> How to implement aggregators to handle multiple models in one index using Algolia SearchBundle.

To search in multiple entities at the same time, use aggregators.
Aggregators let you index multiple entities in a single index.

## Define aggregators

To create a new aggregator, add a new class to your entities directory.
The `getEntities` method of this class should return all the entity classes you want to index:

```php PHP icon=code theme={"system"}
namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use Algolia\SearchBundle\Entity\Aggregator;

#[ORM\Entity]
class News extends Aggregator
{
    /**
     * Returns the entities class names that should be aggregated.
     *
     * @return string[]
     */
    public static function getEntities()
    {
        return [
            Post::class,
            Comment::class,
        ];
    }
}
```

If you're using a NoSQL database like MongoDB,
use the `Algolia\SearchBundle\Document\Aggregator` class instead.

Finally, add the new aggregator class name to the `config/packages/algolia_search.yaml` file:

```yaml YAML icon=code theme={"system"}
algolia_search:
  indices:
    - name: news
      class: App\Entity\News
```

## Search

An aggregator is a standard entity class.
To start searching entities on the aggregator, use the `search` method of your
[SearchService](/doc/framework-integration/symfony/getting-started/quick-start):

```php PHP icon=code theme={"system"}
$this->searchService->index($objectManager, $post);
$this->searchService->index($objectManager, $comment);

$results = $this->searchService->search($objectManager, News::class, 'query');

// $results[0] contains a \App\Entity\Post.
// $results[1] contains a \App\Entity\Comment.
```

<Tip>
  Be careful, the `$result` array may contain different types of entities instances.
</Tip>

To get the raw results from Algolia,
use the [`rawSearch`](/doc/framework-integration/symfony/searching/server-side-search#raw-search) method.
However, each result might have a different structure:

```php PHP icon=code theme={"system"}
$results = $this->searchService->rawSearch(News::class, 'query');
```

```json JSON icon=braces theme={"system"}
{
  "hits": [
    {
      "id": 1,
      "title": "Post title",
      "slug": "post-title",
      "content": "Post content by query",
      "objectID": "App\\Entity\\Post::1"
    },
    {
      "id": 1,
      "content": "Comment content by query",
      "objectID": "App\\Entity\\Comment::1"
    }
  ]
}
```

To ensure that each result has a similar structure,
implement the [`normalize`](/doc/framework-integration/symfony/indexing/configure-searchable-data#normalize) method
on each entity or override it on the aggregator class.

## Conditional indexing

Conditional indexing on aggregators works just like a normal entity, using the `index_if` key.
Use `$this->entity` to have access to the current entity being aggregated.
Here is an example using the method approach:

```yaml YAML icon=code theme={"system"}
algolia_search:
  indices:
    - name: news
      class: App\Entity\News
      index_if: isPublished
```

```php PHP icon=code theme={"system"}
#[ORM\Entity]
class News extends Aggregator
{
    // ...

    public function isPublished()
    {
        // Index only published posts.
        if ($this->entity instanceof Post) {
            return $this->entity->isPublished();
        }

        // If it's not a post, index anyway.
        return true;
    }
}
```

For more information, see [Conditional indexing](/doc/framework-integration/symfony/indexing/index-data-into-algolia#conditional-indexing).
