Skip to content

Commit

Permalink
Data object search service - order by key vs. order by index (#205)
Browse files Browse the repository at this point in the history
* Add new modifier to sort results by tree index

* Apply php-cs-fixer changes

---------

Co-authored-by: lukmzig <lukmzig@users.noreply.github.com>
  • Loading branch information
lukmzig and lukmzig authored Sep 2, 2024
1 parent e48ef72 commit 1757342
Show file tree
Hide file tree
Showing 10 changed files with 145 additions and 49 deletions.
2 changes: 2 additions & 0 deletions config/pimcore/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,8 @@ pimcore_generic_data_index:
normalizer: generic_data_index_sort_normalizer
classDefinitionIcon:
type: keyword
index:
type: integer
asset:
mimetype:
type: keyword
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ If multiple sort modifiers are added to the search, the order of the modifiers i
| [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) |
| [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. |
| [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.) |
| [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! |

### Aggregations

Expand Down
1 change: 1 addition & 0 deletions src/Enum/SearchIndex/FieldCategory/SystemField.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ enum SystemField: string
case PUBLISHED = 'published';
case TYPE = 'type';
case KEY = 'key';
case INDEX = 'index';
case PATH = 'path';
case FULL_PATH = 'fullPath';
case PATH_LEVELS = 'pathLevels';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ class DataObjectSearchResultItem implements ElementSearchResultItemInterface

private string $key;

private int $index;

private bool $published;

private string $path;
Expand Down Expand Up @@ -116,6 +118,18 @@ public function setKey(string $key): DataObjectSearchResultItem
return $this;
}

public function getIndex(): int
{
return $this->index;
}

public function setIndex(int $index): DataObjectSearchResultItem
{
$this->index = $index;

return $this;
}

public function isPublished(): bool
{
return $this->published;
Expand Down
28 changes: 28 additions & 0 deletions src/Model/Search/Modifier/Sort/Tree/OrderByIndexField.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
declare(strict_types=1);

/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Commercial License (PCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PCL
*/

namespace Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Modifier\Sort\Tree;

use Pimcore\Bundle\GenericDataIndexBundle\Enum\Search\SortDirection;
use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Modifier\SearchModifierInterface;

final readonly class OrderByIndexField implements SearchModifierInterface
{
public function getDirection(): SortDirection
{
return SortDirection::ASC;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
*/
final readonly class IndexIconUpdateService implements IndexIconUpdateServiceInterface
{
public function __construct(private Client $openSearchClient)
public function __construct(private Client $openSearchClient)
{
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@
use Pimcore\Bundle\GenericDataIndexBundle\Model\OpenSearch\Modifier\SearchModifierContextInterface;
use Pimcore\Bundle\GenericDataIndexBundle\Model\OpenSearch\Sort\FieldSort;
use Pimcore\Bundle\GenericDataIndexBundle\Model\OpenSearch\Sort\FieldSortList;
use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\DataObject\DataObjectSearch;
use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Modifier\Sort\OrderByPageNumber;
use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Modifier\Sort\Tree\OrderByFullPath;
use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Modifier\Sort\Tree\OrderByIndexField;
use Pimcore\Bundle\GenericDataIndexBundle\SearchIndexAdapter\SearchIndexServiceInterface;

/**
Expand Down Expand Up @@ -93,6 +95,24 @@ public function handleSortByPageNumber(
}
}

#[AsSearchModifierHandler]
public function handleIndexSort(
OrderByIndexField $indexSort,
SearchModifierContextInterface $context
): void {
if (!$context->getOriginalSearch() instanceof DataObjectSearch) {
return;
}

$context->getSearch()
->addSort(
new FieldSort(
SystemField::INDEX->getPath(),
$indexSort->getDirection()->value
)
);
}

private function getInvertedSortList(array $sortListItems): array
{
$invertedSortList = [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public function denormalize(
->setType(SystemField::TYPE->getData($data))
->setPublished($published)
->setKey(SystemField::KEY->getData($data))
->setIndex(SystemField::INDEX->getData($data))
->setPath(SystemField::PATH->getData($data))
->setFullPath(SystemField::FULL_PATH->getData($data))
->setUserOwner(SystemField::USER_OWNER->getData($data) ?? 0)
Expand Down
1 change: 1 addition & 0 deletions src/Service/Serializer/Normalizer/DataObjectNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ private function normalizeSystemFields(AbstractObject $dataObject, bool $skipLaz
SystemField::MODIFICATION_DATE->value => $this->formatTimestamp($dataObject->getModificationDate()),
SystemField::TYPE->value => $dataObject->getType(),
SystemField::KEY->value => $dataObject->getKey(),
SystemField::INDEX->value => $dataObject->getIndex(),
SystemField::PATH->value => $dataObject->getPath(),
SystemField::FULL_PATH->value => $dataObject->getRealFullPath(),
SystemField::USER_OWNER->value => $dataObject->getUserOwner(),
Expand Down
Loading

0 comments on commit 1757342

Please sign in to comment.