Skip to content

Commit f6e69b0

Browse files
authored
[Search] [Data Objects] Search Endpoint (#918)
* Add search endpoint for data-objects * Apply php-cs-fixer changes * [Search] [Filters] Add whitespace for improved readability in SearchIndexFilter * [Refactor] Simplify value resolution in StringResolver * [Refactor] Update namespaces and tags for search controllers * [Refactor] Update search routes for consistency and clarity --------- Co-authored-by: martineiber <11687066+martineiber@users.noreply.github.com>
1 parent 3814422 commit f6e69b0

19 files changed

Lines changed: 188 additions & 66 deletions

config/data_index_filters.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ services:
5353
Pimcore\Bundle\StudioBackendBundle\DataIndex\Filter\SortFilter:
5454
tags: [ 'pimcore.studio_backend.search_index.filter' ]
5555

56+
Pimcore\Bundle\StudioBackendBundle\DataIndex\Filter\StringFilter:
57+
tags: [ 'pimcore.studio_backend.search_index.filter' ]
58+
5659
# DataObject
5760
Pimcore\Bundle\StudioBackendBundle\DataIndex\Filter\DataObject\ClassNameFilter:
5861
tags: [ 'pimcore.studio_backend.search_index.data_object.filter' ]
@@ -86,8 +89,5 @@ services:
8689
tags: [ 'pimcore.studio_backend.search_index.asset.filter' ]
8790

8891
# Asset System Data
89-
Pimcore\Bundle\StudioBackendBundle\DataIndex\Filter\Asset\System\StringFilter:
90-
tags: [ 'pimcore.studio_backend.search_index.asset.filter' ]
91-
9292
Pimcore\Bundle\StudioBackendBundle\DataIndex\Filter\Asset\System\DatetimeFilter:
9393
tags: [ 'pimcore.studio_backend.search_index.asset.filter' ]

src/DataIndex/Filter/Asset/System/StringFilter.php renamed to src/DataIndex/Filter/StringFilter.php

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,9 @@
1414
* @license http://www.pimcore.org/license GPLv3 and PCL
1515
*/
1616

17-
namespace Pimcore\Bundle\StudioBackendBundle\DataIndex\Filter\Asset\System;
17+
namespace Pimcore\Bundle\StudioBackendBundle\DataIndex\Filter;
1818

1919
use Pimcore\Bundle\StudioBackendBundle\DataIndex\Filter\Asset\IsAssetFilterTrait;
20-
use Pimcore\Bundle\StudioBackendBundle\DataIndex\Filter\FilterInterface;
21-
use Pimcore\Bundle\StudioBackendBundle\DataIndex\Query\AssetQueryInterface;
2220
use Pimcore\Bundle\StudioBackendBundle\DataIndex\Query\QueryInterface;
2321
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\InvalidArgumentException;
2422
use Pimcore\Bundle\StudioBackendBundle\Grid\Column\ColumnType;
@@ -35,20 +33,19 @@ final class StringFilter implements FilterInterface
3533
public function apply(mixed $parameters, QueryInterface $query): QueryInterface
3634
{
3735
$parameters = $this->validateParameterType($parameters);
38-
$assetQuery = $this->validateQueryType($query);
3936

40-
if (!$parameters || !$assetQuery) {
37+
if (!$parameters) {
4138
return $query;
4239
}
4340

4441
foreach ($parameters->getColumnFilterByType(ColumnType::SYSTEM_STRING->value) as $column) {
45-
$assetQuery = $this->applyStringFilter($column, $assetQuery);
42+
$query = $this->applyStringFilter($column, $query);
4643
}
4744

48-
return $assetQuery;
45+
return $query;
4946
}
5047

51-
private function applyStringFilter(ColumnFilter $column, AssetQueryInterface $query): AssetQueryInterface
48+
private function applyStringFilter(ColumnFilter $column, QueryInterface $query): QueryInterface
5249
{
5350
if (!is_string($column->getFilterValue())) {
5451
throw new InvalidArgumentException('Filter value for this filter must be a string');

src/DataIndex/Query/AssetQueryInterface.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,6 @@ interface AssetQueryInterface extends QueryInterface
2525
{
2626
public function filterMetadata(string $name, string $type, mixed $data): self;
2727

28-
public function wildcardSearch(
29-
string $fieldName,
30-
string $searchTerm,
31-
bool $enablePqlFieldNameResolution = true
32-
): self;
33-
3428
public function filterDatetime(
3529
string $field,
3630
int|null $startDate = null,

src/DataIndex/Query/DataObjectQuery.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Modifier\Filter\Tree\TagFilter;
3030
use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Modifier\FullTextSearch\ElementKeySearch;
3131
use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Modifier\FullTextSearch\FullTextSearch;
32+
use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Modifier\FullTextSearch\WildcardSearch;
3233
use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Modifier\QueryLanguage\PqlFilter;
3334
use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Modifier\Sort\OrderByField;
3435
use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Modifier\Sort\Tree\OrderByFullPath;
@@ -193,4 +194,14 @@ public function orderByField(string $fieldName, SortDirection $direction): self
193194

194195
return $this;
195196
}
197+
198+
public function wildcardSearch(
199+
string $fieldName,
200+
string $searchTerm,
201+
bool $enablePqlFieldNameResolution = true
202+
): self {
203+
$this->search->addModifier(new WildcardSearch($fieldName, $searchTerm, $enablePqlFieldNameResolution));
204+
205+
return $this;
206+
}
196207
}

src/DataIndex/Query/DocumentQueryInterface.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,5 @@ interface DocumentQueryInterface extends QueryInterface
2626
{
2727
public function orderByField(string $fieldName, SortDirection $direction): self;
2828

29-
public function wildcardSearch(
30-
string $fieldName,
31-
string $searchTerm,
32-
bool $enablePqlFieldNameResolution = true
33-
): self;
34-
3529
public function getSearch(): DocumentSearchInterface;
3630
}

src/DataIndex/Query/QueryInterface.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,10 @@ public function filterInteger(string $field, int $value): self;
5656
public function filterFullText(string $value): self;
5757

5858
public function orderByField(string $fieldName, SortDirection $direction): self;
59+
60+
public function wildcardSearch(
61+
string $fieldName,
62+
string $searchTerm,
63+
bool $enablePqlFieldNameResolution = true
64+
): self;
5965
}

src/Grid/Column/Resolver/System/StringResolver.php

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,22 +39,27 @@ final class StringResolver implements
3939
use SimpleGetterTrait;
4040
use ColumnDataTrait;
4141

42-
/**
43-
* @throws InvalidArgumentException
44-
*/
4542
public function resolveForStudioElement(Column $column, StudioElementInterface $element): ColumnData
4643
{
47-
return $this->getColumnData(
48-
$column,
49-
$this->getValue($column, $element)
50-
);
44+
return $this->resolve($column, $element);
5145
}
5246

5347
public function resolveForCoreElement(Column $column, ElementInterface $element): ColumnData
5448
{
49+
return $this->resolve($column, $element);
50+
}
51+
52+
private function resolve(Column $column, ElementInterface|StudioElementInterface $element): ColumnData
53+
{
54+
try {
55+
$value = $this->getValue($column, $element);
56+
} catch (InvalidArgumentException) {
57+
$value = null;
58+
}
59+
5560
return $this->getColumnData(
5661
$column,
57-
$this->getValue($column, $element)
62+
$value
5863
);
5964
}
6065

src/Grid/Service/GridSearchService.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,21 @@ public function getAssetSearchGrid(SearchGridParameter $gridParameter): Collecti
4545

4646
return $this->gridService->getAssetGrid($parameter);
4747
}
48+
49+
/**
50+
* {@inheritDoc}
51+
*/
52+
public function getDataObjectSearchGrid(SearchGridParameter $searchParameter, ?string $classId): Collection
53+
{
54+
$filter = $searchParameter->getFilters();
55+
$filter->setExcludeFolders(false);
56+
57+
$gridParameter = new GridParameter(
58+
folderId: 1,
59+
columns: $searchParameter->getColumns(),
60+
filters: $filter
61+
);
62+
63+
return $this->gridService->getDataObjectGrid($gridParameter, $classId);
64+
}
4865
}

src/Grid/Service/GridSearchServiceInterface.php

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

1717
namespace Pimcore\Bundle\StudioBackendBundle\Grid\Service;
1818

19+
use Exception;
1920
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\InvalidArgumentException;
21+
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\NotFoundException;
2022
use Pimcore\Bundle\StudioBackendBundle\Grid\MappedParameter\SearchGridParameter;
2123
use Pimcore\Bundle\StudioBackendBundle\Response\Collection;
2224

@@ -29,4 +31,11 @@ interface GridSearchServiceInterface
2931
* @throws InvalidArgumentException
3032
*/
3133
public function getAssetSearchGrid(SearchGridParameter $gridParameter): Collection;
34+
35+
/**
36+
* @throws InvalidArgumentException
37+
* @throws NotFoundException
38+
* @throws Exception
39+
*/
40+
public function getDataObjectSearchGrid(SearchGridParameter $searchParameter, ?string $classId): Collection;
3241
}

src/Grid/Service/GridService.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -89,16 +89,19 @@ public function getAssetGrid(GridParameter $gridParameter): Collection
8989
* @throws NotFoundException
9090
* @throws Exception
9191
*/
92-
public function getDataObjectGrid(GridParameter $gridParameter, string $classId): Collection
92+
public function getDataObjectGrid(GridParameter $gridParameter, ?string $classId): Collection
9393
{
9494
$filter = $gridParameter->getFilters();
95-
$classDefinition = $this->classDefinitionResolver->getById($classId);
9695

97-
if (!$classDefinition instanceof ClassDefinition) {
98-
throw new NotFoundException('Class definition', $classId);
99-
}
96+
if ($classId) {
97+
$classDefinition = $this->classDefinitionResolver->getById($classId);
98+
99+
if (!$classDefinition instanceof ClassDefinition) {
100+
throw new NotFoundException('Class definition', $classId);
101+
}
100102

101-
$filter->setClassName($classDefinition->getName());
103+
$filter->setClassName($classDefinition->getName());
104+
}
102105

103106
$result = $this->gridSearch->searchDataObjects($gridParameter);
104107

0 commit comments

Comments
 (0)