Skip to content

Fix using distinct attribute setting #295

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
Oct 16, 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
2 changes: 2 additions & 0 deletions src/Command/MeilisearchCreateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int

if (isset($value['_service']) && $value['_service'] instanceof SettingsProvider) {
$value = $value['_service']();
} elseif ('distinctAttribute' === $variable && is_array($value)) {
$value = $value[0] ?? null;
}

// Update
Expand Down
2 changes: 2 additions & 0 deletions src/Command/MeilisearchImportCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int

if (isset($value['_service']) && $value['_service'] instanceof SettingsProvider) {
$value = $value['_service']();
} elseif ('distinctAttribute' === $variable && is_array($value)) {
$value = $value[0] ?? null;
}

// Update
Expand Down
10 changes: 10 additions & 0 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,16 @@ public function getConfigTreeBuilder(): TreeBuilder
->end()
->arrayNode('settings')
->info('Configure indices settings, see: https://www.meilisearch.com/docs/reference/api/settings')
->beforeNormalization()
->always()
->then(static function (array $value) {
if (isset($value['distinctAttribute']) && !is_array($value['distinctAttribute'])) {
$value['distinctAttribute'] = (array) $value['distinctAttribute'];
}

return $value;
})
->end()
->arrayPrototype()
->variablePrototype()->end()
->end()
Expand Down
34 changes: 34 additions & 0 deletions tests/Unit/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,40 @@ public function dataTestConfigurationTree(): array
'doctrineSubscribedEvents' => ['postPersist', 'postUpdate', 'preRemove'],
],
],
'distinct attribute' => [
[
'prefix' => 'sf_',
'indices' => [
[
'name' => 'items',
'class' => 'App\Entity\Post',
'settings' => [
'distinctAttribute' => 'product_id',
],
],
],
],
[
'url' => 'http://localhost:7700',
'prefix' => 'sf_',
'indices' => [
[
'name' => 'items',
'class' => 'App\Entity\Post',
'enable_serializer_groups' => false,
'serializer_groups' => ['searchable'],
'index_if' => null,
'settings' => [
'distinctAttribute' => ['product_id'],
],
],
],
'nbResults' => 20,
'batchSize' => 500,
'serializer' => 'serializer',
'doctrineSubscribedEvents' => ['postPersist', 'postUpdate', 'preRemove'],
],
],
];
}
}