Skip to content

Allow to use services for settings #253

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions src/Command/MeilisearchCreateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Meilisearch\Bundle\Exception\TaskException;
use Meilisearch\Bundle\Model\Aggregator;
use Meilisearch\Bundle\SearchService;
use Meilisearch\Bundle\SettingsProvider;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any requirement to have the Interface suffix?

SettingsProviderInterface

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SearchService is also an interface, so avoided

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we could have the Interface suffix on both. But it could be done later I'll create a new issue to make this repo PSR complaint https://www.php-fig.org/psr/

use Meilisearch\Client;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
Expand Down Expand Up @@ -42,7 +43,7 @@ protected function configure(): void
->addOption('indices', 'i', InputOption::VALUE_OPTIONAL, 'Comma-separated list of index names');
}

private function entitiesToIndex($indexes)
private function entitiesToIndex($indexes): array
{
foreach ($indexes as $key => $index) {
$entityClassName = $index['class'];
Expand Down Expand Up @@ -85,16 +86,26 @@ protected function execute(InputInterface $input, OutputInterface $output): int
foreach ($index['settings'] as $variable => $value) {
$method = sprintf('update%s', ucfirst($variable));

if (false === method_exists($indexInstance, $method)) {
if (!method_exists($indexInstance, $method)) {
throw new InvalidSettingName(sprintf('Invalid setting name: "%s"', $variable));
}

if (isset($value['_service']) && $value['_service'] instanceof SettingsProvider) {
$value = $value['_service']();
}

// Update
$task = $indexInstance->{$method}($value);

// Get task information using uid
$indexInstance->waitForTask($task['taskUid']);
$task = $indexInstance->getTask($task['taskUid']);

if ('failed' === $task['status']) {
throw new TaskException($task['error']);
}

$output->writeln('<info>Settings updated of "'.$index['name'].'".</info>');
}
}
}
Expand Down
15 changes: 11 additions & 4 deletions src/Command/MeilisearchImportCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
use Meilisearch\Bundle\Exception\TaskException;
use Meilisearch\Bundle\Model\Aggregator;
use Meilisearch\Bundle\SearchService;
use Meilisearch\Bundle\SettingsProvider;
use Meilisearch\Client;
use Meilisearch\Exceptions\TimeOutException;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
Expand Down Expand Up @@ -155,10 +157,15 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$indexInstance = $this->searchClient->index($index['name']);
foreach ($index['settings'] as $variable => $value) {
$method = sprintf('update%s', ucfirst($variable));
if (false === method_exists($indexInstance, $method)) {

if (!method_exists($indexInstance, $method)) {
throw new InvalidSettingName(sprintf('Invalid setting name: "%s"', $variable));
}

if (isset($value['_service']) && $value['_service'] instanceof SettingsProvider) {
$value = $value['_service']();
}

// Update
$task = $indexInstance->{$method}($value);

Expand All @@ -168,9 +175,9 @@ protected function execute(InputInterface $input, OutputInterface $output): int

if ('failed' === $task['status']) {
throw new TaskException($task['error']);
} else {
$output->writeln('<info>Settings updated.</info>');
}

$output->writeln('<info>Settings updated of "'.$index['name'].'".</info>');
}
}

Expand All @@ -185,7 +192,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
return 0;
}

/*
/**
* @throws TimeOutException
*/
private function formatIndexingResponse(array $batch, int $responseTimeout): array
Expand Down
22 changes: 22 additions & 0 deletions src/DependencyInjection/MeilisearchExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ public function load(array $configs, ContainerBuilder $container): void
$config['prefix'] = $container->getParameter('kernel.environment').'_';
}

foreach ($config['indices'] as $index => $indice) {
$config['indices'][$index]['settings'] = $this->findReferences($config['indices'][$index]['settings']);
}

$container->setParameter('meili_url', $config['url'] ?? null);
$container->setParameter('meili_api_key', $config['api_key'] ?? null);
$container->setParameter('meili_symfony_version', MeilisearchBundle::qualifiedVersion());
Expand All @@ -55,4 +59,22 @@ public function load(array $configs, ContainerBuilder $container): void

$container->setDefinition('search.service', $searchDefinition->setPublic(true));
}

/**
* @param array<mixed, mixed> $settings
*
* @return array<mixed, mixed>
*/
private function findReferences(array $settings): array
{
foreach ($settings as $key => $value) {
if (is_array($value)) {
$settings[$key] = $this->findReferences($value);
} elseif ('_service' === substr((string) $key, -8) || str_starts_with((string) $value, '@') || 'service' === $key) {
$settings[$key] = new Reference(ltrim($value, '@'));
}
}

return $settings;
}
}
13 changes: 13 additions & 0 deletions src/SettingsProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace Meilisearch\Bundle;

interface SettingsProvider
{
/**
* @return array<mixed>
*/
public function __invoke(): array;
}
46 changes: 46 additions & 0 deletions tests/Entity/DynamicSettings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

declare(strict_types=1);

namespace Meilisearch\Bundle\Tests\Entity;

use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\Entity
*/
#[ORM\Entity]
class DynamicSettings
{
/**
* @ORM\Id
*
* @ORM\Column(type="integer")
*/
#[ORM\Id]
#[ORM\Column(type: Types::INTEGER)]
private int $id;

/**
* @ORM\Column(type="string")
*/
#[ORM\Column(type: Types::STRING)]
private string $name;

public function __construct(int $id, string $name)
{
$this->id = $id;
$this->name = $name;
}

public function getId(): int
{
return $this->id;
}

public function getName(): string
{
return $this->name;
}
}
76 changes: 73 additions & 3 deletions tests/Integration/CommandsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Meilisearch\Bundle\Tests\BaseKernelTestCase;
use Meilisearch\Bundle\Tests\Entity\DummyCustomGroups;
use Meilisearch\Bundle\Tests\Entity\DynamicSettings;
use Meilisearch\Bundle\Tests\Entity\SelfNormalizable;
use Meilisearch\Client;
use Meilisearch\Endpoints\Indexes;
Expand Down Expand Up @@ -77,9 +78,9 @@ public function testSearchImportAndClearAndDeleteWithoutIndices(): void
Importing for index Meilisearch\Bundle\Tests\Entity\Post
Indexed a batch of 6 / 6 Meilisearch\Bundle\Tests\Entity\Post entities into sf_phpunit__posts index (6 indexed since start)
Indexed a batch of 6 / 6 Meilisearch\Bundle\Tests\Entity\Post entities into sf_phpunit__aggregated index (6 indexed since start)
Settings updated.
Settings updated.
Settings updated.
Settings updated of "sf_phpunit__posts".
Settings updated of "sf_phpunit__posts".
Settings updated of "sf_phpunit__posts".
Importing for index Meilisearch\Bundle\Tests\Entity\Comment
Importing for index Meilisearch\Bundle\Tests\Entity\Tag
Indexed a batch of 6 / 6 Meilisearch\Bundle\Tests\Entity\Tag entities into sf_phpunit__tags index (6 indexed since start)
Expand All @@ -89,6 +90,11 @@ public function testSearchImportAndClearAndDeleteWithoutIndices(): void
Indexed a batch of 6 / 6 Meilisearch\Bundle\Tests\Entity\Page entities into sf_phpunit__pages index (6 indexed since start)
Importing for index Meilisearch\Bundle\Tests\Entity\SelfNormalizable
Importing for index Meilisearch\Bundle\Tests\Entity\DummyCustomGroups
Importing for index Meilisearch\Bundle\Tests\Entity\DynamicSettings
Settings updated of "sf_phpunit__dynamic_settings".
Settings updated of "sf_phpunit__dynamic_settings".
Settings updated of "sf_phpunit__dynamic_settings".
Settings updated of "sf_phpunit__dynamic_settings".
Importing for index Meilisearch\Bundle\Tests\Entity\Post
Indexed a batch of 6 / 6 Meilisearch\Bundle\Tests\Entity\Post entities into sf_phpunit__posts index (6 indexed since start)
Indexed a batch of 6 / 6 Meilisearch\Bundle\Tests\Entity\Post entities into sf_phpunit__aggregated index (6 indexed since start)
Expand All @@ -114,6 +120,7 @@ public function testSearchImportAndClearAndDeleteWithoutIndices(): void
Cleared sf_phpunit__pages index of Meilisearch\Bundle\Tests\Entity\Page
Cleared sf_phpunit__self_normalizable index of Meilisearch\Bundle\Tests\Entity\SelfNormalizable
Cleared sf_phpunit__dummy_custom_groups index of Meilisearch\Bundle\Tests\Entity\DummyCustomGroups
Cleared sf_phpunit__dynamic_settings index of Meilisearch\Bundle\Tests\Entity\DynamicSettings
Done!

EOD, $clearOutput);
Expand All @@ -132,6 +139,7 @@ public function testSearchImportAndClearAndDeleteWithoutIndices(): void
Deleted sf_phpunit__pages
Deleted sf_phpunit__self_normalizable
Deleted sf_phpunit__dummy_custom_groups
Deleted sf_phpunit__dynamic_settings
Done!

EOD, $clearOutput);
Expand Down Expand Up @@ -325,12 +333,20 @@ public function testSearchCreateWithoutIndices(): void

$this->assertSame(<<<'EOD'
Creating index sf_phpunit__posts for Meilisearch\Bundle\Tests\Entity\Post
Settings updated of "sf_phpunit__posts".
Settings updated of "sf_phpunit__posts".
Settings updated of "sf_phpunit__posts".
Creating index sf_phpunit__comments for Meilisearch\Bundle\Tests\Entity\Comment
Creating index sf_phpunit__tags for Meilisearch\Bundle\Tests\Entity\Tag
Creating index sf_phpunit__tags for Meilisearch\Bundle\Tests\Entity\Link
Creating index sf_phpunit__pages for Meilisearch\Bundle\Tests\Entity\Page
Creating index sf_phpunit__self_normalizable for Meilisearch\Bundle\Tests\Entity\SelfNormalizable
Creating index sf_phpunit__dummy_custom_groups for Meilisearch\Bundle\Tests\Entity\DummyCustomGroups
Creating index sf_phpunit__dynamic_settings for Meilisearch\Bundle\Tests\Entity\DynamicSettings
Settings updated of "sf_phpunit__dynamic_settings".
Settings updated of "sf_phpunit__dynamic_settings".
Settings updated of "sf_phpunit__dynamic_settings".
Settings updated of "sf_phpunit__dynamic_settings".
Creating index sf_phpunit__aggregated for Meilisearch\Bundle\Tests\Entity\Post
Creating index sf_phpunit__aggregated for Meilisearch\Bundle\Tests\Entity\Tag
Done!
Expand All @@ -350,6 +366,9 @@ public function testSearchCreateWithIndices(): void

$this->assertSame(<<<'EOD'
Creating index sf_phpunit__posts for Meilisearch\Bundle\Tests\Entity\Post
Settings updated of "sf_phpunit__posts".
Settings updated of "sf_phpunit__posts".
Settings updated of "sf_phpunit__posts".
Done!

EOD, $createOutput);
Expand Down Expand Up @@ -437,4 +456,55 @@ public function testImportsDummyWithCustomGroups(): void
],
], $this->client->index('sf_phpunit__dummy_custom_groups')->getDocuments()->getResults());
}

/**
* @testWith ["meili:create"]
* ["meili:import"]
*/
public function testImportWithDynamicSettings(string $command): void
{
for ($i = 0; $i <= 5; ++$i) {
$this->entityManager->persist(new DynamicSettings($i, "Dynamic $i"));
}

$this->entityManager->flush();

$importCommand = $this->application->find($command);
$importCommandTester = new CommandTester($importCommand);
$importCommandTester->execute(['--indices' => 'dynamic_settings']);

$importOutput = $importCommandTester->getDisplay();

if ('meili:import' === $command) {
$this->assertSame(<<<'EOD'
Importing for index Meilisearch\Bundle\Tests\Entity\DynamicSettings
Indexed a batch of 6 / 6 Meilisearch\Bundle\Tests\Entity\DynamicSettings entities into sf_phpunit__dynamic_settings index (6 indexed since start)
Settings updated of "sf_phpunit__dynamic_settings".
Settings updated of "sf_phpunit__dynamic_settings".
Settings updated of "sf_phpunit__dynamic_settings".
Settings updated of "sf_phpunit__dynamic_settings".
Done!

EOD, $importOutput);
} else {
$this->assertSame(<<<'EOD'
Creating index sf_phpunit__dynamic_settings for Meilisearch\Bundle\Tests\Entity\DynamicSettings
Settings updated of "sf_phpunit__dynamic_settings".
Settings updated of "sf_phpunit__dynamic_settings".
Settings updated of "sf_phpunit__dynamic_settings".
Settings updated of "sf_phpunit__dynamic_settings".
Done!

EOD, $importOutput);
}

$settings = $this->get('search.client')->index('sf_phpunit__dynamic_settings')->getSettings();

$getSetting = static fn ($value) => $value instanceof \IteratorAggregate ? iterator_to_array($value) : $value;

self::assertSame(['publishedAt', 'title'], $getSetting($settings['filterableAttributes']));
self::assertSame(['title'], $getSetting($settings['searchableAttributes']));
self::assertSame(['a', 'n', 'the'], $getSetting($settings['stopWords']));
self::assertSame(['fantastic' => ['great'], 'great' => ['fantastic']], $getSetting($settings['synonyms']));
}
}
15 changes: 15 additions & 0 deletions tests/Integration/Fixtures/FilterableAttributes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Meilisearch\Bundle\Tests\Integration\Fixtures;

use Meilisearch\Bundle\SettingsProvider;

class FilterableAttributes implements SettingsProvider
{
public function __invoke(): array
{
return ['title', 'publishedAt'];
}
}
15 changes: 15 additions & 0 deletions tests/Integration/Fixtures/SearchableAttributes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Meilisearch\Bundle\Tests\Integration\Fixtures;

use Meilisearch\Bundle\SettingsProvider;

class SearchableAttributes implements SettingsProvider
{
public function __invoke(): array
{
return ['title'];
}
}
15 changes: 15 additions & 0 deletions tests/Integration/Fixtures/StopWords.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Meilisearch\Bundle\Tests\Integration\Fixtures;

use Meilisearch\Bundle\SettingsProvider;

class StopWords implements SettingsProvider
{
public function __invoke(): array
{
return ['the', 'a', 'n'];
}
}
18 changes: 18 additions & 0 deletions tests/Integration/Fixtures/Synonyms.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace Meilisearch\Bundle\Tests\Integration\Fixtures;

use Meilisearch\Bundle\SettingsProvider;

class Synonyms implements SettingsProvider
{
public function __invoke(): array
{
return [
'great' => ['fantastic'],
'fantastic' => ['great'],
];
}
}
2 changes: 1 addition & 1 deletion tests/Integration/SettingsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function testUpdateSettings(): void
$settings = $this->client->index($index)->getSettings();

$output = $commandTester->getDisplay();
$this->assertStringContainsString('Settings updated.', $output);
$this->assertStringContainsString('Settings updated of "sf_phpunit__posts".', $output);
$this->assertNotEmpty($settings['stopWords']);
$this->assertEquals(['a', 'an', 'the'], $settings['stopWords']);

Expand Down
Loading