meilisearch-symfony icon indicating copy to clipboard operation
meilisearch-symfony copied to clipboard

Aggregators do not create a new index

Open baumanno opened this issue 4 years ago • 1 comments

Description Use case: implement a site-wide search on multiple models.

I had hoped to use multiple indices along with meilisearch/instant-meilisearch, but sadly, that is not supported yet. I took a peek at the Algolia docs, and they recommend using Aggregator-entities for this. Conveniently, this bundle provides similar Aggregator-classes, e.g. MeiliSearch\Bundle\Entity\Aggregator, so I went ahead and "registered" the entities I want indexed in the aggregator.

However, this does not create the desired aggregated index

Expected behavior An index with the name defined in the config is created, and it contains the data of the sub-entities registered in the class extending from MeiliSearch\Bundle\Entity\Aggregator

Current behavior The import-command reports "Done!", yet no index is actually created, neither for the aggregator, nor any aggregated entities.

Code Below, find three entities: the first two are regular entities handled by Doctrine; the third is the aggregator.

<?php

declare(strict_types=1);

namespace App\Entity;

use App\Repository\PersonRepository;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\Groups;

/**
 * @ORM\Entity(repositoryClass=PersonRepository::class)
 */
class Person
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     */
    private int $id;

    /**
     * @ORM\Column(type="string", length=255)
     * @Groups({"searchable"})
     */
    private string $firstName;

    /**
     * @ORM\Column(type="string", length=255)
     * @Groups({"searchable"})
     */
    private string $lastName;

   /* getters and setters omitted for brevity */

<?php

declare(strict_types=1);

namespace App\Entity;

use App\Repository\ProjectRepository;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\Groups;

/**
 * @ORM\Entity(repositoryClass=ProjectRepository::class)
 */
class Project
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     */
    private int $id;

    /**
     * @ORM\Column(type="text")
     * @Groups({"searchable"})
     */
    private ?string $title;

    /* getters and setters omitted for brevity */

<?php declare(strict_types=1);

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use MeiliSearch\Bundle\Entity\Aggregator as AggregatorAlias;
use Symfony\Component\Serializer\Annotation\Groups;

/**
 * @ORM\Entity
 */
class Aggregator extends AggregatorAlias
{
    public static function getEntities(): array
    {
        return [
            Person::class,
            Project::class,
        ];
    }
}

This is the meili_search.yaml I'm using:

meili_search:
  url: '%env(MEILISEARCH_URL)%'
  api_key: '%env(MEILISEARCH_API_KEY)%'
  prefix: '%env(MEILISEARCH_PREFIX)%'
  indices:
    - name: data
      class: App\Entity\Aggregator
      enable_serializer_groups: true

Environment (please complete the following information):

  • OS: Arch Linux, Docker image getmeili/meilisearch:latest
  • MeiliSearch version: v.0.20.0
  • meilisearch-python version: unknown

baumanno avatar Jun 16 '21 13:06 baumanno

Coming from the central problem that MS does not support federated search, there are two options to deal with that:

1. Config setting

Allow the same index name in the meili_search.yaml for multiple/different entities.

meili_search:
  // other...
  indices:
    -  name: data
       class: 'MeiliSearch\Bundle\Test\Entity\Post'
    -  name: data
       class: 'MeiliSearch\Bundle\Test\Entity\Comment'

In this example both models, post and comment, would be inserted into the same index data.

2. Using aggregators

While I was checking method one (and will provide a bigger rewrite for this tomorrow), I also came across the issue with the aggregators.

I am pretty sure I can provide some insight and hopefully a fix for this as well.

codedge avatar Jun 16 '21 21:06 codedge

So this could probably closed if it was fixed in #92 ?

norkunas avatar Feb 09 '23 04:02 norkunas

Yeah, you're right! Good catch!

brunoocasali avatar Feb 09 '23 18:02 brunoocasali