Skip to content

Commit

Permalink
Improve search request building
Browse files Browse the repository at this point in the history
  • Loading branch information
PierreGauthier committed Nov 12, 2024
1 parent fc45d60 commit ed074d3
Show file tree
Hide file tree
Showing 18 changed files with 162 additions and 327 deletions.
96 changes: 0 additions & 96 deletions src/Engine/ExpressionVisitor.php

This file was deleted.

2 changes: 1 addition & 1 deletion src/Engine/SearchEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
namespace Gally\OroPlugin\Engine;

use Gally\OroPlugin\Registry\SearchRegistry;
use Gally\OroPlugin\RequestBuilder\GallyRequestBuilder;
use Gally\OroPlugin\Search\GallyRequestBuilder;
use Gally\Sdk\Service\SearchManager;
use Oro\Bundle\SearchBundle\Provider\AbstractSearchMappingProvider;
use Oro\Bundle\SearchBundle\Query\Query;
Expand Down
4 changes: 1 addition & 3 deletions src/Indexer/IndexDataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,7 @@ private function prepareIndexData(
}
}

if (str_starts_with($fieldName, 'category_path')) {
// Todo
} elseif (str_starts_with($fieldName, 'ordered_at_by')) {
if (str_starts_with($fieldName, 'ordered_at_by')) {
// Todo
} elseif (!str_starts_with($fieldName, self::ALL_TEXT_PREFIX)) {
if (null === $value || '' === $value || [] === $value) {
Expand Down
2 changes: 1 addition & 1 deletion src/Indexer/Normalizer/BooleanDataNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function preProcess(
$fieldConfig = null;
}
$type = $fieldConfig ? $fieldConfig->getId()->getFieldType() : $fieldData['type'];
if ('boolean' === $type || str_starts_with($fieldName, 'is_')) {
if ('boolean' === $type) {
$this->booleanAttributes[] = $fieldName;
}
}
Expand Down
12 changes: 5 additions & 7 deletions src/Indexer/Normalizer/CategoryDataNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

namespace Gally\OroPlugin\Indexer\Normalizer;

use Oro\Bundle\CatalogBundle\Placeholder\CategoryPathPlaceholder;
use Oro\Bundle\EntityBundle\ORM\DoctrineHelper;
use Oro\Bundle\LocaleBundle\Entity\Localization;
use Oro\Bundle\LocaleBundle\Helper\LocalizationHelper;
Expand Down Expand Up @@ -98,15 +99,11 @@ public function normalize(
}
}

$assignSortOrders = $fieldsValues['assigned_to_sort_order.ASSIGN_TYPE_ASSIGN_ID'] ?? [];
foreach ($assignSortOrders as $assignSortOrder) {
$value = $assignSortOrder['value'];
foreach ($fieldsValues['category_paths.CATEGORY_PATH'] ?? [] as $value) {
$value = $value['value'];
if ($value instanceof PlaceholderValue) {
$placeholders = $value->getPlaceholders();
if ('variant' === $placeholders[AssignTypePlaceholder::NAME]) {
$variantId = $placeholders[AssignIdPlaceholder::NAME];
$categories[$variantId]['position'] = $value->getValue();
}
$preparedEntityData['category_paths'][] = $placeholders[CategoryPathPlaceholder::NAME];
}
}

Expand All @@ -124,5 +121,6 @@ public function normalize(
unset($fieldsValues['assigned_to.ASSIGN_TYPE_ASSIGN_ID']);
unset($fieldsValues['manually_added_to.ASSIGN_TYPE_ASSIGN_ID']);
unset($fieldsValues['assigned_to_sort_order.ASSIGN_TYPE_ASSIGN_ID']);
unset($fieldsValues['category_paths.CATEGORY_PATH']);
}
}
20 changes: 13 additions & 7 deletions src/Indexer/Normalizer/VisibilityDataNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

use Oro\Bundle\CustomerBundle\Placeholder\CustomerIdPlaceholder;
use Oro\Bundle\ProductBundle\Entity\Product;
use Oro\Bundle\VisibilityBundle\Entity\VisibilityResolved\BaseVisibilityResolved;
use Oro\Bundle\WebsiteBundle\Entity\Website;
use Oro\Bundle\WebsiteSearchBundle\Placeholder\PlaceholderValue;

Expand All @@ -33,7 +34,8 @@ public function normalize(
array &$preparedEntityData,
): void {
if (Product::class === $entityClass) {
$visibilitiesByCustomer = [];
$visibleForCustomers = [];
$hiddenForCustomers = [];
$visibilities = $fieldsValues['visibility_customer.CUSTOMER_ID'] ?? [];
foreach ($this->toArray($visibilities) as $value) {
$value = $value['value'];
Expand All @@ -44,14 +46,18 @@ public function normalize(
$value = $value->getValue();
}

$visibilitiesByCustomer[] = [
'customer_id' => $placeholders[CustomerIdPlaceholder::NAME],
'value' => $value,
];
if (BaseVisibilityResolved::VISIBILITY_VISIBLE === $value) {
$visibleForCustomers[] = $placeholders[CustomerIdPlaceholder::NAME];
} elseif (BaseVisibilityResolved::VISIBILITY_HIDDEN === $value) {
$hiddenForCustomers[] = $placeholders[CustomerIdPlaceholder::NAME];
}
}

if (!empty($visibilitiesByCustomer)) {
$preparedEntityData['visibility_customer'] = $visibilitiesByCustomer;
if (!empty($visibleForCustomers)) {
$preparedEntityData['visible_for_customer'] = $visibleForCustomers;
}
if (!empty($hiddenForCustomers)) {
$preparedEntityData['hidden_for_customer'] = $hiddenForCustomers;
}
unset($fieldsValues['visibility_customer.CUSTOMER_ID']);
}
Expand Down
33 changes: 23 additions & 10 deletions src/Indexer/Provider/CatalogProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Doctrine\ORM\EntityManagerInterface;
use Gally\Sdk\Entity\Catalog;
use Gally\Sdk\Entity\LocalizedCatalog;
use Oro\Bundle\LocaleBundle\Entity\Localization;
use Oro\Bundle\PricingBundle\Provider\WebsiteCurrencyProvider;
use Oro\Bundle\WebsiteBundle\Entity\Repository\WebsiteRepository;
use Oro\Bundle\WebsiteBundle\Entity\Website;
Expand All @@ -30,6 +31,8 @@ class CatalogProvider implements ProviderInterface
protected WebsiteRepository $websiteRepository;
protected AbstractWebsiteLocalizationProvider $websiteLocalizationProvider;

private array $catalogCache = [];

public function __construct(
EntityManagerInterface $entityManager,
AbstractWebsiteLocalizationProvider $websiteLocalizationProvider,
Expand All @@ -47,16 +50,8 @@ public function provide(): iterable
$websites = $this->websiteRepository->findAll();

foreach ($websites as $website) {
$localizations = $this->websiteLocalizationProvider->getLocalizations($website);
$catalog = new Catalog($this->getCatalogCodeFromWebsiteId($website->getId()), $website->getName());
foreach ($localizations as $localization) {
yield new LocalizedCatalog(
$catalog,
'website_' . $website->getId() . '_' . $localization->getFormattingCode(),
$localization->getName(),
$localization->getFormattingCode(),
$this->currencyProvider->getWebsiteDefaultCurrency($website->getId())
);
foreach ($this->websiteLocalizationProvider->getLocalizations($website) as $localization) {
yield $this->buildLocalizedCatalog($website, $localization);
}
}
}
Expand All @@ -65,4 +60,22 @@ public function getCatalogCodeFromWebsiteId(int $websiteId): string
{
return 'website_' . $websiteId;
}

public function buildLocalizedCatalog(Website $website, Localization $localization): LocalizedCatalog
{
if (!\array_key_exists($website->getId(), $this->catalogCache)) {
$this->catalogCache[$website->getId()] = new Catalog(
$this->getCatalogCodeFromWebsiteId($website->getId()),
$website->getName(),
);
}

return new LocalizedCatalog(
$this->catalogCache[$website->getId()],
'website_' . $website->getId() . '_' . $localization->getFormattingCode(),
$localization->getName(),
$localization->getFormattingCode(),
$this->currencyProvider->getWebsiteDefaultCurrency($website->getId())
);
}
}
16 changes: 15 additions & 1 deletion src/Indexer/Provider/SourceFieldProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,20 @@ class SourceFieldProvider implements ProviderInterface
'brand_LOCALIZATION_ID', // Brand field is managed as a select
];

private array $oroSystemAttribute = [ // todo conf
'status',
'assigned_to',
'manually_added_to',
'category_path',
'category_paths',
'is_variant',
'is_visible_by_default',
'visibility_anonymous',
'visibility_new',
'visible_for_customer',
'hidden_for_customer',
];

public function __construct(
private SearchMappingProvider $mappingProvider,
private EntityAliasResolver $entityAliasResolver,
Expand Down Expand Up @@ -108,6 +122,7 @@ public function provide(): iterable
$fieldType,
$defaultLabel,
$this->getLabels($labelKey, $defaultLabel),
\in_array($fieldName, $this->oroSystemAttribute, true)
);
}
}
Expand Down Expand Up @@ -141,7 +156,6 @@ private function getGallyType(string $fieldName, string $fieldType): string
'brand' === $fieldName => SourceField::TYPE_SELECT,
str_ends_with($fieldName, '_enum') => SourceField::TYPE_SELECT,
str_starts_with($fieldName, 'image_') => SourceField::TYPE_IMAGE,
str_starts_with($fieldName, 'is_') => SourceField::TYPE_BOOLEAN,
default => $this->typeMapping[$fieldType] ?? SourceField::TYPE_TEXT,
};
}
Expand Down
Loading

0 comments on commit ed074d3

Please sign in to comment.