Skip to content

Commit c4a48cd

Browse files
committed
Update settings once
1 parent 99c07ad commit c4a48cd

File tree

7 files changed

+90
-49
lines changed

7 files changed

+90
-49
lines changed

src/Command/MeilisearchImportCommand.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,14 +141,14 @@ protected function execute(InputInterface $input, OutputInterface $output): int
141141
);
142142
}
143143

144-
if ($updateSettings) {
145-
$this->settingsUpdater->update($index['prefixed_name'], $responseTimeout);
146-
}
147-
148144
++$page;
149145
} while (count($entities) >= $batchSize);
150146

151147
$manager->clear();
148+
149+
if ($updateSettings) {
150+
$this->settingsUpdater->update($index['prefixed_name'], $responseTimeout);
151+
}
152152
}
153153

154154
$output->writeln('<info>Done!</info>');

src/Event/SettingsUpdatedEvent.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,21 @@ final class SettingsUpdatedEvent extends Event
1818
*/
1919
private string $index;
2020

21+
/**
22+
* @var non-empty-string
23+
*/
24+
private string $setting;
25+
2126
/**
2227
* @param class-string $class
2328
* @param non-empty-string $index
29+
* @param non-empty-string $setting
2430
*/
25-
public function __construct(string $class, string $index)
31+
public function __construct(string $class, string $index, string $setting)
2632
{
2733
$this->index = $index;
2834
$this->class = $class;
35+
$this->setting = $setting;
2936
}
3037

3138
/**
@@ -43,4 +50,12 @@ public function getIndex(): string
4350
{
4451
return $this->index;
4552
}
53+
54+
/**
55+
* @return non-empty-string
56+
*/
57+
public function getSetting(): string
58+
{
59+
return $this->setting;
60+
}
4661
}

src/EventListener/ConsoleOutputSubscriber.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public function __construct(OutputStyle $io)
1919

2020
public function afterSettingsUpdate(SettingsUpdatedEvent $event): void
2121
{
22-
$this->io->writeln('<info>Settings updated of "'.$event->getIndex().'".</info>');
22+
$this->io->writeln('<info>Setting "'.$event->getSetting().'" updated of "'.$event->getIndex().'".</info>');
2323
}
2424

2525
public static function getSubscribedEvents(): array

src/Services/SettingsUpdater.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public function update(string $indice, ?int $responseTimeout = null): void
7373
throw new TaskException($task['error']);
7474
}
7575

76-
$this->eventDispatcher->dispatch(new SettingsUpdatedEvent($index['class'], $indexName));
76+
$this->eventDispatcher->dispatch(new SettingsUpdatedEvent($index['class'], $indexName, $variable));
7777
}
7878
}
7979
}

tests/Integration/Command/MeilisearchCreateCommandTest.php

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,10 @@ public function testWithoutIndices(bool $updateSettings): void
4444
if ($updateSettings) {
4545
$this->assertSame(<<<'EOD'
4646
Creating index sf_phpunit__posts for Meilisearch\Bundle\Tests\Entity\Post
47-
Settings updated of "sf_phpunit__posts".
48-
Settings updated of "sf_phpunit__posts".
49-
Settings updated of "sf_phpunit__posts".
50-
Settings updated of "sf_phpunit__posts".
47+
Setting "stopWords" updated of "sf_phpunit__posts".
48+
Setting "filterableAttributes" updated of "sf_phpunit__posts".
49+
Setting "searchCutoffMs" updated of "sf_phpunit__posts".
50+
Setting "typoTolerance" updated of "sf_phpunit__posts".
5151
Creating index sf_phpunit__comments for Meilisearch\Bundle\Tests\Entity\Comment
5252
Creating index sf_phpunit__tags for Meilisearch\Bundle\Tests\Entity\Tag
5353
Creating index sf_phpunit__tags for Meilisearch\Bundle\Tests\Entity\Link
@@ -56,10 +56,10 @@ public function testWithoutIndices(bool $updateSettings): void
5656
Creating index sf_phpunit__self_normalizable for Meilisearch\Bundle\Tests\Entity\SelfNormalizable
5757
Creating index sf_phpunit__dummy_custom_groups for Meilisearch\Bundle\Tests\Entity\DummyCustomGroups
5858
Creating index sf_phpunit__dynamic_settings for Meilisearch\Bundle\Tests\Entity\DynamicSettings
59-
Settings updated of "sf_phpunit__dynamic_settings".
60-
Settings updated of "sf_phpunit__dynamic_settings".
61-
Settings updated of "sf_phpunit__dynamic_settings".
62-
Settings updated of "sf_phpunit__dynamic_settings".
59+
Setting "filterableAttributes" updated of "sf_phpunit__dynamic_settings".
60+
Setting "searchableAttributes" updated of "sf_phpunit__dynamic_settings".
61+
Setting "stopWords" updated of "sf_phpunit__dynamic_settings".
62+
Setting "synonyms" updated of "sf_phpunit__dynamic_settings".
6363
Creating index sf_phpunit__aggregated for Meilisearch\Bundle\Tests\Entity\Post
6464
Creating index sf_phpunit__aggregated for Meilisearch\Bundle\Tests\Entity\Tag
6565
Done!
@@ -96,10 +96,10 @@ public function testWithIndices(): void
9696

9797
$this->assertSame(<<<'EOD'
9898
Creating index sf_phpunit__posts for Meilisearch\Bundle\Tests\Entity\Post
99-
Settings updated of "sf_phpunit__posts".
100-
Settings updated of "sf_phpunit__posts".
101-
Settings updated of "sf_phpunit__posts".
102-
Settings updated of "sf_phpunit__posts".
99+
Setting "stopWords" updated of "sf_phpunit__posts".
100+
Setting "filterableAttributes" updated of "sf_phpunit__posts".
101+
Setting "searchCutoffMs" updated of "sf_phpunit__posts".
102+
Setting "typoTolerance" updated of "sf_phpunit__posts".
103103
Done!
104104

105105
EOD, $createOutput);
@@ -121,10 +121,10 @@ public function testWithDynamicSettings(): void
121121

122122
$this->assertSame(<<<'EOD'
123123
Creating index sf_phpunit__dynamic_settings for Meilisearch\Bundle\Tests\Entity\DynamicSettings
124-
Settings updated of "sf_phpunit__dynamic_settings".
125-
Settings updated of "sf_phpunit__dynamic_settings".
126-
Settings updated of "sf_phpunit__dynamic_settings".
127-
Settings updated of "sf_phpunit__dynamic_settings".
124+
Setting "filterableAttributes" updated of "sf_phpunit__dynamic_settings".
125+
Setting "searchableAttributes" updated of "sf_phpunit__dynamic_settings".
126+
Setting "stopWords" updated of "sf_phpunit__dynamic_settings".
127+
Setting "synonyms" updated of "sf_phpunit__dynamic_settings".
128128
Done!
129129

130130
EOD, $importOutput);

tests/Integration/Command/MeilisearchImportCommandTest.php

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -338,10 +338,10 @@ public function testImportWithDynamicSettings(): void
338338
$this->assertSame(<<<'EOD'
339339
Importing for index Meilisearch\Bundle\Tests\Entity\DynamicSettings
340340
Indexed a batch of 6 / 6 Meilisearch\Bundle\Tests\Entity\DynamicSettings entities into sf_phpunit__dynamic_settings index (6 indexed since start)
341-
Settings updated of "sf_phpunit__dynamic_settings".
342-
Settings updated of "sf_phpunit__dynamic_settings".
343-
Settings updated of "sf_phpunit__dynamic_settings".
344-
Settings updated of "sf_phpunit__dynamic_settings".
341+
Setting "filterableAttributes" updated of "sf_phpunit__dynamic_settings".
342+
Setting "searchableAttributes" updated of "sf_phpunit__dynamic_settings".
343+
Setting "stopWords" updated of "sf_phpunit__dynamic_settings".
344+
Setting "synonyms" updated of "sf_phpunit__dynamic_settings".
345345
Done!
346346

347347
EOD, $importOutput);
@@ -356,6 +356,35 @@ public function testImportWithDynamicSettings(): void
356356
self::assertSame(['fantastic' => ['great'], 'great' => ['fantastic']], $getSetting($settings['synonyms']));
357357
}
358358

359+
public function testImportUpdatesSettingsOnce(): void
360+
{
361+
for ($i = 0; $i <= 3; ++$i) {
362+
$this->createPost();
363+
}
364+
365+
$this->entityManager->flush();
366+
367+
$importCommand = $this->application->find('meilisearch:import');
368+
$importCommandTester = new CommandTester($importCommand);
369+
$importCommandTester->execute(['--indices' => 'posts', '--batch-size' => '2']);
370+
371+
$importOutput = $importCommandTester->getDisplay();
372+
373+
$this->assertSame(<<<'EOD'
374+
Importing for index Meilisearch\Bundle\Tests\Entity\Post
375+
Indexed a batch of 2 / 2 Meilisearch\Bundle\Tests\Entity\Post entities into sf_phpunit__posts index (2 indexed since start)
376+
Indexed a batch of 2 / 2 Meilisearch\Bundle\Tests\Entity\Post entities into sf_phpunit__aggregated index (2 indexed since start)
377+
Indexed a batch of 2 / 2 Meilisearch\Bundle\Tests\Entity\Post entities into sf_phpunit__posts index (4 indexed since start)
378+
Indexed a batch of 2 / 2 Meilisearch\Bundle\Tests\Entity\Post entities into sf_phpunit__aggregated index (4 indexed since start)
379+
Setting "stopWords" updated of "sf_phpunit__posts".
380+
Setting "filterableAttributes" updated of "sf_phpunit__posts".
381+
Setting "searchCutoffMs" updated of "sf_phpunit__posts".
382+
Setting "typoTolerance" updated of "sf_phpunit__posts".
383+
Done!
384+
385+
EOD, $importOutput);
386+
}
387+
359388
public function testAlias(): void
360389
{
361390
$command = $this->application->find('meilisearch:import');

tests/Integration/SettingsTest.php

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,28 +9,21 @@
99
use Symfony\Bundle\FrameworkBundle\Console\Application;
1010
use Symfony\Component\Console\Tester\CommandTester;
1111

12-
/**
13-
* Class SettingsTest.
14-
*/
1512
class SettingsTest extends BaseKernelTestCase
1613
{
1714
private static string $indexName = 'posts';
1815

19-
public const DEFAULT_RANKING_RULES
20-
= [
21-
'words',
22-
'typo',
23-
'proximity',
24-
'attribute',
25-
'sort',
26-
'exactness',
27-
];
16+
public const DEFAULT_RANKING_RULES = [
17+
'words',
18+
'typo',
19+
'proximity',
20+
'attribute',
21+
'sort',
22+
'exactness',
23+
];
2824

2925
protected Application $application;
3026

31-
/**
32-
* @throws \Exception
33-
*/
3427
public function setUp(): void
3528
{
3629
parent::setUp();
@@ -52,18 +45,22 @@ public function testUpdateSettings(): void
5245
$settings = $this->client->index($index)->getSettings();
5346

5447
$output = $commandTester->getDisplay();
55-
$this->assertStringContainsString('Settings updated of "sf_phpunit__posts".', $output);
56-
$this->assertNotEmpty($settings['stopWords']);
48+
49+
$this->assertStringContainsString('Setting "stopWords" updated of "sf_phpunit__posts".', $output);
5750
$this->assertEquals(['a', 'an', 'the'], $settings['stopWords']);
5851

59-
$this->assertNotEmpty($settings['filterableAttributes']);
60-
$this->assertEquals(['publishedAt', 'title'], $settings['filterableAttributes']);
52+
$this->assertStringContainsString('Setting "searchCutoffMs" updated of "sf_phpunit__posts".', $output);
53+
$this->assertEquals(1500, $settings['searchCutoffMs']);
54+
55+
$this->assertStringContainsString('Setting "filterableAttributes" updated of "sf_phpunit__posts".', $output);
56+
$this->assertSame(['publishedAt', 'title'], $settings['filterableAttributes']);
6157

58+
$this->assertStringContainsString('Setting "typoTolerance" updated of "sf_phpunit__posts".', $output);
6259
$this->assertArrayHasKey('typoTolerance', $settings);
6360
$this->assertInstanceOf(TypoTolerance::class, $settings['typoTolerance']);
6461
$this->assertTrue($settings['typoTolerance']['enabled']);
65-
$this->assertEquals(['title'], $settings['typoTolerance']['disableOnAttributes']);
66-
$this->assertEquals(['york'], $settings['typoTolerance']['disableOnWords']);
67-
$this->assertEquals(['oneTypo' => 5, 'twoTypos' => 9], $settings['typoTolerance']['minWordSizeForTypos']);
62+
$this->assertSame(['title'], $settings['typoTolerance']['disableOnAttributes']);
63+
$this->assertSame(['york'], $settings['typoTolerance']['disableOnWords']);
64+
$this->assertSame(['oneTypo' => 5, 'twoTypos' => 9], $settings['typoTolerance']['minWordSizeForTypos']);
6865
}
6966
}

0 commit comments

Comments
 (0)