Skip to content

Commit 1757342

Browse files
authored
Data object search service - order by key vs. order by index (#205)
* Add new modifier to sort results by tree index * Apply php-cs-fixer changes --------- Co-authored-by: lukmzig <[email protected]>
1 parent e48ef72 commit 1757342

File tree

10 files changed

+145
-49
lines changed

10 files changed

+145
-49
lines changed

config/pimcore/config.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,8 @@ pimcore_generic_data_index:
209209
normalizer: generic_data_index_sort_normalizer
210210
classDefinitionIcon:
211211
type: keyword
212+
index:
213+
type: integer
212214
asset:
213215
mimetype:
214216
type: keyword

doc/04_Searching_For_Data_In_Index/05_Search_Modifiers/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ If multiple sort modifiers are added to the search, the order of the modifiers i
5959
| [OrderByFullPath](https://github.com/pimcore/generic-data-index-bundle/blob/1.x/src/Model/Search/Modifier/Sort/Tree/OrderByFullPath.php) | Tree related sorting | Order by full path (including element key) |
6060
| [OrderByField](https://github.com/pimcore/generic-data-index-bundle/blob/1.x/src/Model/Search/Modifier/Sort/OrderByField.php) | Field based sorting | Order by given field name.<br/>If `$enablePqlFieldNameResolution` is set to true (default) [Pimcore Query Language](https://github.com/pimcore/generic-data-index-bundle/blob/1.x/src/Model/Search/Modifier/Sort/OrderByField.php) field name resolution logic is enabled. Therefore it's possible to use short field names then instead of specifying the full path in OpenSearch. |
6161
| [OrderByPageNumber](https://github.com/pimcore/generic-data-index-bundle/blob/1.x/src/Model/Search/Modifier/Sort/Tree/OrderByPageNumber.php) | Search related sorting | Use inverted search for large amounts of data (this modifier is added to the search when there are at least 1000 results by default, and page number is above the half of total pages. Furthermore, existing sorting has to be already applied.) |
62+
| [OrderByIndexField](https://github.com/pimcore/generic-data-index-bundle/blob/1.x/src/Model/Search/Modifier/Sort/Tree/OrderByIndexField.php) | Search related sorting | Order by object tree index for custom tree sorting. This modifier is currently applied only for data objects! |
6263

6364
### Aggregations
6465

src/Enum/SearchIndex/FieldCategory/SystemField.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ enum SystemField: string
3333
case PUBLISHED = 'published';
3434
case TYPE = 'type';
3535
case KEY = 'key';
36+
case INDEX = 'index';
3637
case PATH = 'path';
3738
case FULL_PATH = 'fullPath';
3839
case PATH_LEVELS = 'pathLevels';

src/Model/Search/DataObject/SearchResult/DataObjectSearchResultItem.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ class DataObjectSearchResultItem implements ElementSearchResultItemInterface
3131

3232
private string $key;
3333

34+
private int $index;
35+
3436
private bool $published;
3537

3638
private string $path;
@@ -116,6 +118,18 @@ public function setKey(string $key): DataObjectSearchResultItem
116118
return $this;
117119
}
118120

121+
public function getIndex(): int
122+
{
123+
return $this->index;
124+
}
125+
126+
public function setIndex(int $index): DataObjectSearchResultItem
127+
{
128+
$this->index = $index;
129+
130+
return $this;
131+
}
132+
119133
public function isPublished(): bool
120134
{
121135
return $this->published;
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
/**
5+
* Pimcore
6+
*
7+
* This source file is available under two different licenses:
8+
* - GNU General Public License version 3 (GPLv3)
9+
* - Pimcore Commercial License (PCL)
10+
* Full copyright and license information is available in
11+
* LICENSE.md which is distributed with this source code.
12+
*
13+
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
14+
* @license http://www.pimcore.org/license GPLv3 and PCL
15+
*/
16+
17+
namespace Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Modifier\Sort\Tree;
18+
19+
use Pimcore\Bundle\GenericDataIndexBundle\Enum\Search\SortDirection;
20+
use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Modifier\SearchModifierInterface;
21+
22+
final readonly class OrderByIndexField implements SearchModifierInterface
23+
{
24+
public function getDirection(): SortDirection
25+
{
26+
return SortDirection::ASC;
27+
}
28+
}

src/SearchIndexAdapter/OpenSearch/DataObject/IndexIconUpdateService.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
*/
2727
final readonly class IndexIconUpdateService implements IndexIconUpdateServiceInterface
2828
{
29-
public function __construct(private Client $openSearchClient)
29+
public function __construct(private Client $openSearchClient)
3030
{
3131
}
3232

src/SearchIndexAdapter/OpenSearch/Search/Modifier/Sort/TreeSortHandlers.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,10 @@
2222
use Pimcore\Bundle\GenericDataIndexBundle\Model\OpenSearch\Modifier\SearchModifierContextInterface;
2323
use Pimcore\Bundle\GenericDataIndexBundle\Model\OpenSearch\Sort\FieldSort;
2424
use Pimcore\Bundle\GenericDataIndexBundle\Model\OpenSearch\Sort\FieldSortList;
25+
use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\DataObject\DataObjectSearch;
2526
use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Modifier\Sort\OrderByPageNumber;
2627
use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Modifier\Sort\Tree\OrderByFullPath;
28+
use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Modifier\Sort\Tree\OrderByIndexField;
2729
use Pimcore\Bundle\GenericDataIndexBundle\SearchIndexAdapter\SearchIndexServiceInterface;
2830

2931
/**
@@ -93,6 +95,24 @@ public function handleSortByPageNumber(
9395
}
9496
}
9597

98+
#[AsSearchModifierHandler]
99+
public function handleIndexSort(
100+
OrderByIndexField $indexSort,
101+
SearchModifierContextInterface $context
102+
): void {
103+
if (!$context->getOriginalSearch() instanceof DataObjectSearch) {
104+
return;
105+
}
106+
107+
$context->getSearch()
108+
->addSort(
109+
new FieldSort(
110+
SystemField::INDEX->getPath(),
111+
$indexSort->getDirection()->value
112+
)
113+
);
114+
}
115+
96116
private function getInvertedSortList(array $sortListItems): array
97117
{
98118
$invertedSortList = [];

src/Service/Serializer/Denormalizer/Search/DataObjectSearchResultDenormalizer.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ public function denormalize(
5959
->setType(SystemField::TYPE->getData($data))
6060
->setPublished($published)
6161
->setKey(SystemField::KEY->getData($data))
62+
->setIndex(SystemField::INDEX->getData($data))
6263
->setPath(SystemField::PATH->getData($data))
6364
->setFullPath(SystemField::FULL_PATH->getData($data))
6465
->setUserOwner(SystemField::USER_OWNER->getData($data) ?? 0)

src/Service/Serializer/Normalizer/DataObjectNormalizer.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ private function normalizeSystemFields(AbstractObject $dataObject, bool $skipLaz
107107
SystemField::MODIFICATION_DATE->value => $this->formatTimestamp($dataObject->getModificationDate()),
108108
SystemField::TYPE->value => $dataObject->getType(),
109109
SystemField::KEY->value => $dataObject->getKey(),
110+
SystemField::INDEX->value => $dataObject->getIndex(),
110111
SystemField::PATH->value => $dataObject->getPath(),
111112
SystemField::FULL_PATH->value => $dataObject->getRealFullPath(),
112113
SystemField::USER_OWNER->value => $dataObject->getUserOwner(),

0 commit comments

Comments
 (0)