Skip to content

Commit 7cc107c

Browse files
committed
Fixed issues after code review
1 parent 863b865 commit 7cc107c

File tree

13 files changed

+90
-42
lines changed

13 files changed

+90
-42
lines changed

src/bundle/Core/Resources/config/services.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
imports:
22
- { resource: commands.yml }
3-
- { resource: embeddings.yml }
3+
- { resource: embeddings.yaml }
44

55
parameters:
66
ibexa.site_access.default.name: default

src/contracts/Repository/Values/Content/EmbeddingQuery.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,7 @@
1212
use Ibexa\Contracts\Core\Repository\Values\Content\Query\Embedding;
1313
use InvalidArgumentException;
1414

15-
/**
16-
* This class is used to perform an embedding query.
17-
*/
18-
class EmbeddingQuery extends Query
15+
final class EmbeddingQuery extends Query
1916
{
2017
private ?Embedding $embedding = null;
2118

@@ -29,7 +26,7 @@ public function setEmbedding(?Embedding $embedding): void
2926
$this->embedding = $embedding;
3027
}
3128

32-
public function getFilter(): ?Criterion
29+
public function getFilter(): Criterion
3330
{
3431
return $this->filter;
3532
}
@@ -105,7 +102,7 @@ public function isValid(): bool
105102
if (count($invalid) > 0) {
106103
throw new InvalidArgumentException(
107104
sprintf(
108-
'EmbeddingQuery may not set [%s].',
105+
'EmbeddingQuery did not set [%s].',
109106
implode(', ', $invalid)
110107
)
111108
);

src/contracts/Repository/Values/Content/EmbeddingQueryBuilder.php

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,21 @@
1313

1414
final class EmbeddingQueryBuilder
1515
{
16-
private EmbeddingQuery $query;
16+
private ?Embedding $embedding = null;
17+
18+
private ?int $limit = null;
19+
20+
private ?int $offset = null;
21+
22+
private ?Criterion $filter = null;
23+
24+
/** @var array<\Ibexa\Contracts\Core\Repository\Values\Content\Query\Aggregation> */
25+
private array $aggregations = [];
26+
27+
private bool $performCount = false;
1728

1829
private function __construct()
1930
{
20-
$this->query = new EmbeddingQuery();
2131
}
2232

2333
public static function create(): self
@@ -27,28 +37,28 @@ public static function create(): self
2737

2838
public function withEmbedding(Embedding $embed): self
2939
{
30-
$this->query->setEmbedding($embed);
40+
$this->embedding = $embed;
3141

3242
return $this;
3343
}
3444

3545
public function setLimit(int $limit): self
3646
{
37-
$this->query->setLimit($limit);
47+
$this->limit = $limit;
3848

3949
return $this;
4050
}
4151

4252
public function setOffset(int $offset): self
4353
{
44-
$this->query->setOffset($offset);
54+
$this->offset = $offset;
4555

4656
return $this;
4757
}
4858

4959
public function setFilter(Criterion $filter): self
5060
{
51-
$this->query->setFilter($filter);
61+
$this->filter = $filter;
5262

5363
return $this;
5464
}
@@ -58,20 +68,44 @@ public function setFilter(Criterion $filter): self
5868
*/
5969
public function setAggregations(array $aggregations): self
6070
{
61-
$this->query->setAggregations($aggregations);
71+
$this->aggregations = $aggregations;
6272

6373
return $this;
6474
}
6575

6676
public function setPerformCount(bool $performCount): self
6777
{
68-
$this->query->setPerformCount($performCount);
78+
$this->performCount = $performCount;
6979

7080
return $this;
7181
}
7282

7383
public function build(): EmbeddingQuery
7484
{
75-
return $this->query;
85+
$query = new EmbeddingQuery();
86+
87+
if ($this->embedding !== null) {
88+
$query->setEmbedding($this->embedding);
89+
}
90+
91+
if ($this->limit !== null) {
92+
$query->setLimit($this->limit);
93+
}
94+
95+
if ($this->offset !== null) {
96+
$query->setOffset($this->offset);
97+
}
98+
99+
if ($this->filter !== null) {
100+
$query->setFilter($this->filter);
101+
}
102+
103+
if (!empty($this->aggregations)) {
104+
$query->setAggregations($this->aggregations);
105+
}
106+
107+
$query->setPerformCount($this->performCount);
108+
109+
return $query;
76110
}
77111
}

src/contracts/Repository/Values/Content/Query/Embedding.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ public function __construct(array $value)
2323
$this->value = $value;
2424
}
2525

26-
/** @return float[] */
26+
/**
27+
* @return float[]
28+
*/
2729
public function getValue(): array
2830
{
2931
return $this->value;

src/contracts/Repository/Values/Content/QueryValidatorInterface.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
* @copyright Copyright (C) Ibexa AS. All rights reserved.
55
* @license For full copyright and license information view LICENSE file distributed with this source code.
66
*/
7+
declare(strict_types=1);
8+
79
namespace Ibexa\Contracts\Core\Repository\Values\Content;
810

911
interface QueryValidatorInterface

src/contracts/Search/Embedding/EmbeddingResolverNotFoundException.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
* @copyright Copyright (C) Ibexa AS. All rights reserved.
55
* @license For full copyright and license information view LICENSE file distributed with this source code.
66
*/
7+
declare(strict_types=1);
8+
79
namespace Ibexa\Contracts\Core\Search\Embedding;
810

911
use RuntimeException;

src/lib/Search/Common/FieldValueMapper/EmbeddingMapper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
use Ibexa\Core\Search\Common\FieldValueMapper;
1414

1515
/**
16-
* @internal for internal use by Search engine field value mapper
16+
* @internal for internal use by search engine field value mapper
1717
*/
1818
final class EmbeddingMapper extends FieldValueMapper
1919
{

src/lib/Search/Embedding/EmbeddingConfiguration.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public function getDefaultEmbeddingModelIdentifier(): string
6060
}
6161

6262
/**
63-
* @return array{name: string, dimensions: int, field_suffix: string, 'embedding_provider': string}
63+
* @return array{name: string, dimensions: int, field_suffix: string, embedding_provider: string}
6464
*/
6565
public function getDefaultEmbeddingModel(): array
6666
{

tests/integration/Core/Repository/Values/Content/EmbeddingQueryBuilderTest.php

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,19 +35,19 @@ public function testBuilderSetsAllowedProperties(): void
3535

3636
$query = $builder->build();
3737

38-
$this->assertSame(
38+
self::assertSame(
3939
$embedding,
4040
$query->getEmbedding(),
4141
'Embedding should be set by builder'
4242
);
4343

44-
$this->assertEquals(10, $query->getLimit(), 'Limit should be set by builder');
45-
$this->assertEquals(5, $query->getOffset(), 'Offset should be set by builder');
46-
$this->assertTrue($query->getPerformCount(), 'PerformCount flag should be true');
44+
self::assertEquals(10, $query->getLimit(), 'Limit should be set by builder');
45+
self::assertEquals(5, $query->getOffset(), 'Offset should be set by builder');
46+
self::assertTrue($query->getPerformCount(), 'PerformCount flag should be true');
4747

4848
$aggregations = $query->getAggregations();
49-
$this->assertIsArray($aggregations, 'Aggregations must be array');
50-
$this->assertCount(2, $aggregations, 'Two aggregations added');
49+
self::assertIsArray($aggregations, 'Aggregations must be array');
50+
self::assertCount(2, $aggregations, 'Two aggregations added');
5151
}
5252

5353
public function testIsValidReturnsTrueForCleanQuery(): void
@@ -56,7 +56,7 @@ public function testIsValidReturnsTrueForCleanQuery(): void
5656
->withEmbedding($this->createMock(Embedding::class))
5757
->build();
5858

59-
$this->assertTrue($query->isValid());
59+
self::assertTrue($query->isValid());
6060
}
6161

6262
public function testSettingSortClausesThenIsValidThrows(): void
@@ -65,15 +65,26 @@ public function testSettingSortClausesThenIsValidThrows(): void
6565
->withEmbedding($this->createMock(Embedding::class))
6666
->build();
6767

68-
// bypass setter via array-append magic
6968
$query->sortClauses[] = new ContentName(BaseQuery::SORT_ASC);
7069
$query->query = $this->createMock(Criterion::class);
7170
$query->facetBuilders = [$this->createMock(FacetBuilder::class)];
7271
$query->spellcheck = new Spellcheck('foo');
7372

7473
$this->expectException(InvalidArgumentException::class);
75-
$this->expectExceptionMessage('EmbeddingQuery may not set [query, sortClauses, facetBuilders, spellcheck].');
74+
$this->expectExceptionMessage('EmbeddingQuery did not set [query, sortClauses, facetBuilders, spellcheck].');
7675

7776
$query->isValid();
7877
}
78+
79+
public function testBuildReturnsNewInstance(): void
80+
{
81+
$builder = EmbeddingQueryBuilder::create();
82+
83+
$originalQuery = $builder->build();
84+
$builder->setPerformCount(true);
85+
$secondQuery = $builder->build();
86+
87+
self::assertNotSame($originalQuery, $secondQuery);
88+
self::assertNotEquals($originalQuery->getPerformCount(), $secondQuery->getPerformCount());
89+
}
7990
}

0 commit comments

Comments
 (0)