Skip to content

Commit f6e9212

Browse files
committed
Fix using bundle configured nbResults for search
1 parent e0371f4 commit f6e9212

File tree

5 files changed

+27
-10
lines changed

5 files changed

+27
-10
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
"symfony/doctrine-bridge": "^4.4 || ^5.0 || ^6.0",
4444
"symfony/http-client": "^4.4 || ^5.0 || ^6.0",
4545
"symfony/phpunit-bridge": "^4.4 || ^5.0 || ^6.0",
46+
"symfony/var-exporter": "^4.4 || ^5.0 || ^6.0",
4647
"symfony/yaml": "^4.4 || ^5.0 || ^6.0"
4748
},
4849
"autoload": {

src/DependencyInjection/Configuration.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ public function getConfigTreeBuilder(): TreeBuilder
2222
->scalarNode('prefix')
2323
->defaultNull()
2424
->end()
25-
->scalarNode('nbResults')
25+
->integerNode('nbResults')
2626
->defaultValue(20)
2727
->end()
28-
->scalarNode('batchSize')
28+
->integerNode('batchSize')
2929
->defaultValue(500)
3030
->end()
3131
->arrayNode('doctrineSubscribedEvents')

src/Services/MeilisearchService.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ public function search(
155155
): array {
156156
$this->assertIsSearchable($className);
157157

158-
$ids = $this->engine->search($query, $this->searchableAs($className), $searchParams);
158+
$ids = $this->engine->search($query, $this->searchableAs($className), $searchParams + ['limit' => $this->configuration['nbResults']]);
159159
$results = [];
160160

161161
// Check if the engine returns results in "hits" key

tests/Integration/SearchTest.php

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
use Meilisearch\Bundle\Tests\Entity\Post;
1111
use Meilisearch\Bundle\Tests\Entity\Tag;
1212
use Meilisearch\Endpoints\Indexes;
13-
use Meilisearch\Exceptions\ApiException;
1413
use Symfony\Bundle\FrameworkBundle\Console\Application;
1514
use Symfony\Component\Console\Tester\CommandTester;
1615

@@ -26,10 +25,6 @@ class SearchTest extends BaseKernelTestCase
2625
protected Application $application;
2726
protected Indexes $index;
2827

29-
/**
30-
* @throws ApiException
31-
* @throws \Exception
32-
*/
3328
protected function setUp(): void
3429
{
3530
parent::setUp();
@@ -115,8 +110,20 @@ public function testSearchPagination(): void
115110
$this->assertEqualsCanonicalizing(array_slice($testDataTitles, 2, 2), $resultTitles);
116111
}
117112

118-
protected function tearDown(): void
113+
public function testSearchNbResults(): void
119114
{
120-
parent::tearDown();
115+
for ($i = 0; $i < 15; ++$i) {
116+
$this->createPost();
117+
}
118+
119+
$command = $this->application->find('meilisearch:import');
120+
$commandTester = new CommandTester($command);
121+
$commandTester->execute([
122+
'--indices' => $this->index->getUid(),
123+
]);
124+
125+
$results = $this->searchService->search($this->objectManager, Post::class, 'test');
126+
127+
$this->assertCount(12, $results);
121128
}
122129
}

tests/Kernel.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Doctrine\Bundle\DoctrineBundle\ConnectionFactory;
88
use Doctrine\Bundle\DoctrineBundle\DoctrineBundle;
9+
use Doctrine\ORM\Configuration;
910
use Meilisearch\Bundle\MeilisearchBundle;
1011
use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
1112
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
@@ -40,5 +41,13 @@ protected function configureContainer(ContainerBuilder $container, LoaderInterfa
4041
],
4142
]);
4243
}
44+
// @phpstan-ignore-next-line
45+
if (method_exists(Configuration::class, 'setLazyGhostObjectEnabled') && Kernel::VERSION_ID >= 60100) {
46+
$container->prependExtensionConfig('doctrine', [
47+
'orm' => [
48+
'enable_lazy_ghost_objects' => true,
49+
],
50+
]);
51+
}
4352
}
4453
}

0 commit comments

Comments
 (0)