Skip to content

Commit

Permalink
[Pipeline] #1245170 - Prevent empty pipeline
Browse files Browse the repository at this point in the history
  • Loading branch information
PierreGauthier committed Sep 20, 2024
1 parent 092cc71 commit 0f20b8a
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 16 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
],
"require": {
"php": ">=8.2",
"api-platform/core": "^3.2",
"api-platform/core": "~3.3.0",
"cweagans/composer-patches": "^1.7",
"opensearch-project/opensearch-php": "^2.1",
"lexik/jwt-authentication-bundle": "^2.14",
Expand Down
1 change: 0 additions & 1 deletion src/Doctrine/Filter/SearchFilterWithDefault.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;

// todo upgrade @Pigau ajouter des tests concerant ce filtre ?
class SearchFilterWithDefault extends GallySearchFilter
{
use SearchFilterTrait;
Expand Down
6 changes: 4 additions & 2 deletions src/Index/Service/IndexSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -433,9 +433,11 @@ public function getDynamicIndexSettings(Metadata $metadata, LocalizedCatalog|int
$complexeSourceField = $this->sourceFieldRepository->getComplexeFields($metadata);
$settings += ['mapping.nested_fields.limit' => \count($complexeSourceField)];

// Add default pipeline
// Add default pipeline if any processor are defined.
$pipeline = $this->pipelineRepository->createByMetadata($metadata);
$settings['default_pipeline'] = $pipeline->getName();
if ($pipeline) {
$settings['default_pipeline'] = $pipeline->getName();
}

return $settings;
}
Expand Down
9 changes: 4 additions & 5 deletions src/Index/Tests/Unit/IndexSettingsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class IndexSettingsTest extends AbstractTestCase
*
* @dataProvider dynamicAttributeDataProvider
*/
public function testDynamicIndexSettings(string $fieldName, int $expectedNestedFieldsLimit, string $expectedDefaultPipeline)
public function testDynamicIndexSettings(string $fieldName, int $expectedNestedFieldsLimit)
{
static::loadFixture(
[
Expand All @@ -43,15 +43,14 @@ public function testDynamicIndexSettings(string $fieldName, int $expectedNestedF
$settings = $indexSettings->getDynamicIndexSettings($metadata, $localizedCatalogs);

$this->assertEquals($expectedNestedFieldsLimit, $settings['mapping.nested_fields.limit']);
$this->assertEquals($expectedDefaultPipeline, $settings['default_pipeline']);
}

protected function dynamicAttributeDataProvider(): array
{
return [
['source_field_1.yaml', 0, 'test-gally-llm-pipeline-product'],
['source_field_2.yaml', 2, 'test-gally-llm-pipeline-product'],
['source_field_3.yaml', 4, 'test-gally-llm-pipeline-product'],
['source_field_1.yaml', 0],
['source_field_2.yaml', 2],
['source_field_3.yaml', 4],
];
}
}
6 changes: 4 additions & 2 deletions src/Search/Repository/Ingest/PipelineRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function get(string $name): ?IngestPipeline
/**
* @throws \Exception
*/
public function createByMetadata(Metadata $metadata): IngestPipeline
public function createByMetadata(Metadata $metadata): ?IngestPipeline
{
$pipelineName = $this->pipelinePrefix . $metadata->getEntity();
$processors = [];
Expand All @@ -72,6 +72,8 @@ public function createByMetadata(Metadata $metadata): IngestPipeline
$processors = array_merge($processors, $processorsProvider->getProcessors($metadata));
}

return $this->create($pipelineName, $pipelineName, $processors);
return empty($processors)
? null
: $this->create($pipelineName, $pipelineName, $processors);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,5 @@ public function get(string $name): ?IngestPipeline;
*
* @param Metadata $metadata metadata
*/
public function createByMetadata(Metadata $metadata): IngestPipeline;
public function createByMetadata(Metadata $metadata): ?IngestPipeline;
}
4 changes: 4 additions & 0 deletions src/Search/Resources/config/test/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@ services:
class: Gally\Search\Tests\Service\SearchSettingsProvider
arguments:
- '%gally.search_settings%'

Gally\Search\Tests\Mock\DummyProcessorProvider:
tags:
- { name: gally.search.ingest_pipeline_processors_provider }
34 changes: 34 additions & 0 deletions src/Search/Tests/Mock/DummyProcessorProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php
/**
* DISCLAIMER.
*
* Do not edit or add to this file if you wish to upgrade Gally to newer versions in the future.
*
* @author Gally Team <[email protected]>
* @copyright 2022-present Smile
* @license Open Software License v. 3.0 (OSL-3.0)
*/

declare(strict_types=1);

namespace Gally\Search\Tests\Mock;

use Gally\Metadata\Entity\Metadata;
use Gally\Search\Service\IngestPipelineProcessorProvider;

class DummyProcessorProvider implements IngestPipelineProcessorProvider
{
public function getProcessors(Metadata $metadata): array
{
return 'category' === $metadata->getEntity()
? [
[
'set' => [
'field' => 'dummy',
'value' => ['test'],
],
],
]
: [];
}
}
17 changes: 13 additions & 4 deletions src/Search/Tests/Unit/Repository/IngestPipelineRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,9 +199,15 @@ public function testCreateByMetadata(): void
$expectedPipeline = new IngestPipeline(
'test-gally-llm-pipeline-category',
'test-gally-llm-pipeline-category',
[]
[
'set' => [
'field' => 'dummy',
'value' => ['test'],
],
]
);
$actualPipeline = $ingestPipelineRepository->createByMetadata($metadataRepository->findByEntity('category'));

$this->assertEquals($expectedPipeline->getName(), $actualPipeline->getName());
$this->assertEquals($expectedPipeline->getDescription(), $actualPipeline->getDescription());
$this->assertGreaterThanOrEqual(\count($expectedPipeline->getProcessors()), \count($actualPipeline->getProcessors()));
Expand All @@ -212,8 +218,11 @@ public function testCreateByMetadata(): void
[]
);
$actualPipeline = $ingestPipelineRepository->createByMetadata($metadataRepository->findByEntity('product_document'));
$this->assertEquals($expectedPipeline->getName(), $actualPipeline->getName());
$this->assertEquals($expectedPipeline->getDescription(), $actualPipeline->getDescription());
$this->assertGreaterThanOrEqual(\count($expectedPipeline->getProcessors()), \count($actualPipeline->getProcessors()));
// If pipeline doesn't have any processor it will be not created.
if ($actualPipeline) {
$this->assertEquals($expectedPipeline->getName(), $actualPipeline->getName());
$this->assertEquals($expectedPipeline->getDescription(), $actualPipeline->getDescription());
$this->assertGreaterThanOrEqual(\count($expectedPipeline->getProcessors()), \count($actualPipeline->getProcessors()));
}
}
}

0 comments on commit 0f20b8a

Please sign in to comment.