Skip to content

Commit 91c5c00

Browse files
authored
Asset metadata relation fix (#150)
1 parent 29517bf commit 91c5c00

File tree

2 files changed

+60
-14
lines changed

2 files changed

+60
-14
lines changed

src/SearchIndexAdapter/OpenSearch/Asset/FieldDefinitionAdapter/RelationAdapter.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,14 @@ public function getIndexMapping(): array
2929
{
3030
return [
3131
'properties' => [
32-
'id' => [
32+
'object' => [
3333
'type' => AttributeType::LONG->value,
3434
],
35-
'type' => [
36-
'type' => AttributeType::KEYWORD->value,
35+
'asset' => [
36+
'type' => AttributeType::LONG->value,
37+
],
38+
'document' => [
39+
'type' => AttributeType::LONG->value,
3740
],
3841
],
3942
];
@@ -43,8 +46,7 @@ public function normalize(mixed $value): ?array
4346
{
4447
if($value instanceof ElementInterface) {
4548
return [
46-
'type' => Service::getElementType($value),
47-
'id' => $value->getId(),
49+
Service::getElementType($value) => [$value->getId()],
4850
];
4951
}
5052

@@ -53,7 +55,7 @@ public function normalize(mixed $value): ?array
5355

5456
protected function getSearchFilterFieldPath(AssetMetaDataFilter|AssetMetaDataAggregation $filter): string
5557
{
56-
return parent::getSearchFilterFieldPath($filter) . '.id';
58+
return parent::getSearchFilterFieldPath($filter) . '.' . $filter->getType();
5759
}
5860

5961
protected function isValidScalar(mixed $value): bool

tests/Unit/SearchIndexAdapter/Asset/FieldDefinitionAdapter/RelationAdapterTest.php

Lines changed: 52 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,14 @@ public function testGetIndexMapping()
3535

3636
$this->assertSame([
3737
'properties' => [
38-
'id' => [
38+
'object' => [
3939
'type' => 'long',
4040
],
41-
'type' => [
42-
'type' => 'keyword',
41+
'asset' => [
42+
'type' => 'long',
43+
],
44+
'document' => [
45+
'type' => 'long',
4346
],
4447
],
4548
], $adapter->getIndexMapping());
@@ -56,8 +59,7 @@ public function testNormalize()
5659
$image->setId(1);
5760

5861
$this->assertSame([
59-
'type' => 'asset',
60-
'id' => 1,
62+
'asset' => [1],
6163
], $adapter->normalize($image));
6264
}
6365

@@ -118,7 +120,7 @@ public function testApplySearchFilter()
118120
'bool' => [
119121
'filter' => [
120122
'term' => [
121-
'standard_fields.test.default.id' => 1,
123+
'standard_fields.test.default.asset' => 1,
122124
],
123125
],
124126
],
@@ -134,7 +136,7 @@ public function testApplySearchFilter()
134136
'bool' => [
135137
'filter' => [
136138
'term' => [
137-
'standard_fields.test.en.id' => 2,
139+
'standard_fields.test.en.asset' => 2,
138140
],
139141
],
140142
],
@@ -150,7 +152,49 @@ public function testApplySearchFilter()
150152
'bool' => [
151153
'filter' => [
152154
'terms' => [
153-
'standard_fields.test.en.id' => [1, 2],
155+
'standard_fields.test.en.asset' => [1, 2],
156+
],
157+
],
158+
],
159+
],
160+
], $search->toArray());
161+
162+
$searchIndexConfigServiceInterfaceMock = $this->makeEmpty(SearchIndexConfigServiceInterface::class);
163+
$adapter = (new RelationAdapter(
164+
$searchIndexConfigServiceInterfaceMock,
165+
))->setType('object');
166+
167+
$filter = new AssetMetaDataFilter('test', 'object', 1);
168+
$search = new Search();
169+
$adapter->applySearchFilter($filter, $search);
170+
171+
$this->assertSame([
172+
'query' => [
173+
'bool' => [
174+
'filter' => [
175+
'term' => [
176+
'standard_fields.test.default.object' => 1,
177+
],
178+
],
179+
],
180+
],
181+
], $search->toArray());
182+
183+
$searchIndexConfigServiceInterfaceMock = $this->makeEmpty(SearchIndexConfigServiceInterface::class);
184+
$adapter = (new RelationAdapter(
185+
$searchIndexConfigServiceInterfaceMock,
186+
))->setType('document');
187+
188+
$filter = new AssetMetaDataFilter('test', 'document', 1);
189+
$search = new Search();
190+
$adapter->applySearchFilter($filter, $search);
191+
192+
$this->assertSame([
193+
'query' => [
194+
'bool' => [
195+
'filter' => [
196+
'term' => [
197+
'standard_fields.test.default.document' => 1,
154198
],
155199
],
156200
],

0 commit comments

Comments
 (0)