monsieurbiz / SyliusSearchPlugin

A search plugin using Elasticsearch for Sylius
MIT License
45 stars 38 forks source link

Impossible to override the index name #153

Closed costa-jeremy closed 1 year ago

costa-jeremy commented 1 year ago

Hello, By default the indexes are generated with the name: monsieurbiz_product_xxx which is the name of the key in the document configuration yaml file

monsieurbiz_sylius_search:
    documents:
        **monsieurbiz_product:**
            #document_class: '…' # by default MonsieurBiz\SyliusSearchPlugin\Model\Documentable\Documentable
            instant_search_enabled: true # by default false

So it's impossible to change $indexCode which is used to set a name for the document :

foreach ($documentables as $indexCode => $documentableConfiguration) {
            **$documentableServiceId = 'search.documentable.' . $indexCode;**
            $documentableClass = $documentableConfiguration['document_class'];
            $this->validateDocumentableResource($documentableClass);
            $documentableDefinition = (new Definition($documentableClass))
                ->setAutowired(true)
                ->setArguments([
                    '$indexCode' => $indexCode,
                    '$sourceClass' => $documentableConfiguration['source'],
                    '$targetClass' => $documentableConfiguration['target'],
                    '$templates' => $documentableConfiguration['templates'],
                    '$limits' => $documentableConfiguration['limits'],
                ])
            ;

This îndexCode is used in the method "getIndexName" of ClientFactory.php :

public function getIndexName(DocumentableInterface $documentable, ?string $locale): string
    {
        return $documentable->getIndexCode() . strtolower(null !== $locale ? '_' . $locale : '');
    }

I have already tried to decorate this file to add a custom prefix to the index name, but it seems impossible.

So, if we have multiple sites on the same elasticsearch server, it won't work because all the files will have the same name. Maybe it is possible to add a param on yaml file or anywhere to override this name ?

also, I think adding an index name that does not contain "monsieurbiz_product" will cause problems elsewhere, for example on AppendProductAttributeMappingSubscriber.php :

  if (false === (bool) preg_match('/monsieurbiz_product$/', $event->getIndexCode())) {
            return;
  }

Thank you in advance.

delyriand commented 1 year ago

Hi @JeremyCst,

Thanks for the issue.

I have added the possibility to set a prefix in the PR #155. Can you give me feedback?

costa-jeremy commented 1 year ago

Hello @delyriand ! Thanks for the PR :)

I tested this and for me it is ok! thank you :)