Skip to content

Commit

Permalink
Fix filter field managment
Browse files Browse the repository at this point in the history
  • Loading branch information
PierreGauthier committed Nov 14, 2024
1 parent 612e796 commit b6393af
Show file tree
Hide file tree
Showing 14 changed files with 222 additions and 218 deletions.
2 changes: 1 addition & 1 deletion src/Decorator/ProductIndexFieldsProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

namespace Gally\OroPlugin\Decorator;

use Gally\OroPlugin\Engine\SearchEngine;
use Gally\OroPlugin\Search\SearchEngine;
use Oro\Bundle\ProductBundle\Search\ProductIndexAttributeProviderInterface;
use Oro\Bundle\SearchBundle\Engine\EngineParameters;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@

declare(strict_types=1);

namespace Gally\OroPlugin\EventListener;
namespace Gally\OroPlugin\Indexer\EventListener;

use Doctrine\ORM\EntityManagerInterface;
use Doctrine\Persistence\ManagerRegistry;
use Gally\OroPlugin\Engine\Indexer;
use Gally\OroPlugin\Indexer\Indexer;
use Oro\Bundle\ElasticSearchBundle\Engine\ElasticSearch;
use Oro\Bundle\LocaleBundle\Entity\Localization;
use Oro\Bundle\ProductBundle\Entity\Product;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

declare(strict_types=1);

namespace Gally\OroPlugin\EventListener;
namespace Gally\OroPlugin\Indexer\EventListener;

use Doctrine\Persistence\ManagerRegistry;
use Oro\Bundle\ElasticSearchBundle\Engine\ElasticSearch;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@

declare(strict_types=1);

namespace Gally\OroPlugin\EventListener;
namespace Gally\OroPlugin\Indexer\EventListener;

use Doctrine\Persistence\ManagerRegistry;
use Gally\OroPlugin\Engine\Indexer;
use Gally\OroPlugin\Indexer\Indexer;
use Oro\Bundle\ElasticSearchBundle\Engine\ElasticSearch;
use Oro\Bundle\LocaleBundle\Entity\Localization;
use Oro\Bundle\LocaleBundle\Helper\LocalizationHelper;
Expand Down
1 change: 0 additions & 1 deletion src/Indexer/IndexDataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
namespace Gally\OroPlugin\Indexer;

use Doctrine\ORM\EntityManagerInterface;
use Gally\OroPlugin\Engine\Indexer;
use Gally\OroPlugin\Indexer\Normalizer\AbstractNormalizer;
use Oro\Bundle\ConfigBundle\Config\ConfigManager;
use Oro\Bundle\EntityBundle\ORM\DoctrineHelper;
Expand Down
2 changes: 1 addition & 1 deletion src/Engine/Indexer.php → src/Indexer/Indexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

declare(strict_types=1);

namespace Gally\OroPlugin\Engine;
namespace Gally\OroPlugin\Indexer;

use Gally\OroPlugin\Indexer\Provider\CatalogProvider;
use Gally\OroPlugin\Indexer\Provider\SourceFieldProvider;
Expand Down
8 changes: 4 additions & 4 deletions src/Resources/config/services/indexer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ services:
tags:
- { name: console.command }

Gally\OroPlugin\EventListener\WebsiteSearchWebCatalogIndexerListener:
Gally\OroPlugin\Indexer\EventListener\WebsiteSearchWebCatalogIndexerListener:
arguments:
- '@oro_website_search.manager.website_context_manager'
- '@doctrine'
Expand All @@ -56,15 +56,15 @@ services:
tags:
- { name: kernel.event_listener, event: oro_website_search.event.index_entity.contentnode, method: onWebsiteSearchIndex, priority: -255 }

Gally\OroPlugin\EventListener\WebsiteSearchInventoryLevelIndexerListener:
Gally\OroPlugin\Indexer\EventListener\WebsiteSearchInventoryLevelIndexerListener:
arguments:
- '@doctrine'
- '@oro_website_search.engine.parameters'
- '@oro_warehouse.provider.enabled_warehouses_provider'
tags:
- { name: kernel.event_listener, event: oro_website_search.event.index_entity.product, method: onWebsiteSearchIndex, priority: -255 }

Gally\OroPlugin\EventListener\WebsiteSearchChildDataIndexerListener:
Gally\OroPlugin\Indexer\EventListener\WebsiteSearchChildDataIndexerListener:
arguments:
- '@doctrine'
- '@oro_website_search.engine.parameters'
Expand Down Expand Up @@ -128,7 +128,7 @@ services:
- '%gally_config.attribute_mapping%'
- !tagged_iterator { tag: 'gally.indexer_normalizer' }

Gally\OroPlugin\Engine\Indexer:
Gally\OroPlugin\Indexer\Indexer:
arguments:
- '@oro_entity.doctrine_helper'
- '@oro_website_search.provider.search_mapping'
Expand Down
9 changes: 4 additions & 5 deletions src/Resources/config/services/search.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
services:
Gally\OroPlugin\Registry\SearchRegistry: ~
Gally\OroPlugin\Search\SearchRegistry: ~

Gally\OroPlugin\Search\ContextProvider:
arguments:
Expand Down Expand Up @@ -30,7 +30,7 @@ services:
tags:
- { name: form.type, alias: gally_search_type_select_filter }

Gally\OroPlugin\Engine\SearchEngine:
Gally\OroPlugin\Search\SearchEngine:
arguments:
- '@event_dispatcher'
- '@Gally\OroPlugin\Resolver\QueryPlaceholderResolver'
Expand All @@ -39,7 +39,7 @@ services:
# - '@oro_website_elastic_search.request_builder.registry'
- '@Gally\Sdk\Service\SearchManager'
- '@Gally\OroPlugin\Search\GallyRequestBuilder'
- '@Gally\OroPlugin\Registry\SearchRegistry'
- '@Gally\OroPlugin\Search\SearchRegistry'
- '%gally_config.attribute_mapping%'
calls:
- ['setMapper', ['@oro_website_search.engine.mapper']]
Expand All @@ -50,7 +50,6 @@ services:
arguments:
- '@oro_website_search.engine.parameters'
- '@Gally\Sdk\Service\SearchManager'
- '@Gally\OroPlugin\Registry\SearchRegistry'
- '@oro_entity_extend.enum_type_helper'
- '@Gally\OroPlugin\Search\SearchRegistry'
tags:
- { name: oro_datagrid.extension }
93 changes: 77 additions & 16 deletions src/Search/ExpressionVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use Gally\Sdk\GraphQl\Request;
use Oro\Bundle\ProductBundle\Entity\Product;
use Oro\Bundle\SearchBundle\Query\Criteria\Criteria;
use Oro\Bundle\SearchBundle\Query\Query;
use Oro\Bundle\VisibilityBundle\Entity\VisibilityResolved\BaseVisibilityResolved;

class ExpressionVisitor extends BaseExpressionVisitor
Expand Down Expand Up @@ -58,26 +59,38 @@ public function walkCompositeExpression(CompositeExpression $expr, bool $isMainQ

$filters = [];
foreach ($expr->getExpressionList() as $expression) {
$filters[] = $this->dispatch($expression, $isMainQuery);
$filter = $this->dispatch($expression, $isMainQuery);
if ($filter) {
if ($isMainQuery) {
foreach ($filter as $field => $data) {
if (!\array_key_exists($field, $filters)) {
$filters[$field] = $data;
} else {
$filters['boolFilter'] = [
$type => [
[$field => $filters[$field]],
[$field => $data],
],
];
}
}
} else {
$filters[] = $filter;
}
}
}
$filters = array_values(array_filter($filters));
$filters = array_filter($filters);

return $isMainQuery
? array_merge(...$filters)
? $filters
: ['boolFilter' => [$type => $filters]];
}

public function walkComparison(Comparison $comparison): ?array
{
[$type, $field] = Criteria::explodeFieldTypeName($comparison->getField());
$field = $this->attributeMapping[$field] ?? $field;
[$type, $field] = $this->explodeFieldTypeName($comparison->getField());
$value = $this->dispatch($comparison->getValue());
$operator = match ($comparison->getOperator()) {
'IN', 'NOT IN' => Request::FILTER_OPERATOR_IN,
'LIKE', 'NOT LIKE' => Request::FILTER_OPERATOR_MATCH,
'EXISTS', 'NOT EXISTS' => Request::FILTER_OPERATOR_EXISTS,
default => Request::FILTER_OPERATOR_EQ,
};
$operator = $this->getGallyOperator($comparison->getOperator());
$hasNegation = str_starts_with($comparison->getOperator(), 'NOT');

if ('all_text' === $field) {
Expand All @@ -88,20 +101,28 @@ public function walkComparison(Comparison $comparison): ?array

if ('id' === $field) {
$type = 'text';
} elseif ('inv_status' === $field) {
$field = 'stock.status';
} elseif ('inv_status' === $field || 'stock__status' === $field) {
if (\count($value) > 1) {
return null; // if we want in stock and out of sotck product, we do not need this filter.
return null; // if we want in stock and out of stock product, we do not need this filter.
}
$operator = Request::FILTER_OPERATOR_EQ;
$value = \in_array(Product::INVENTORY_STATUS_IN_STOCK, $value, true);
$type = 'bool';
$field = 'stock__status';
$value = \in_array(Product::INVENTORY_STATUS_IN_STOCK, $value, true) || \in_array(true, $value, true);
} elseif (str_starts_with($field, 'assigned_to.') || str_starts_with($field, 'manually_added_to.')) {
[$field, $variantId] = explode('.', $field);
[$_, $value] = explode('_', $variantId);
} elseif (str_starts_with($field, 'category_paths.')) {
[$field, $value] = explode('.', $field);
$type = 'text';
$operator = Request::FILTER_OPERATOR_IN;
} elseif ('category__id' === $field && Request::FILTER_OPERATOR_IN === $operator) {
// Category filter do not support in operator
$value = array_map(
fn ($value) => [$field => [Request::FILTER_OPERATOR_EQ => $value]],
$value
);
$field = 'boolFilter';
$operator = '_should';
} elseif (str_starts_with($field, 'visibility_customer.')) {
[$field, $customerId] = explode('.', $field);
$type = 'text';
Expand All @@ -120,6 +141,16 @@ public function walkComparison(Comparison $comparison): ?array
}
}

if ('bool' === $type) {
if (\is_array($value) && \count($value) > 1) {
$operator = Request::FILTER_OPERATOR_EXISTS;
$value = true;
} else {
$operator = Request::FILTER_OPERATOR_EQ;
$value = \is_array($value) ? reset($value) : $value;
}
}

$rule = [$field => [$operator => $this->enforceValueType($type, $value)]];

return $hasNegation
Expand All @@ -137,6 +168,36 @@ public function getSearchQuery(): ?string
return $this->searchQuery;
}

/**
* @return array [string, string]
*/
private function explodeFieldTypeName(string $field): array
{
[$type, $field] = Criteria::explodeFieldTypeName($field);
if (str_contains($field, '.')) {
$parts = explode('.', $field, 2);
if ('bool' === $parts[0]) {
[$type, $field] = $parts;
}
}

return [$type, $this->attributeMapping[$field] ?? $field];
}

private function getGallyOperator(string $operator): string
{
return match ($operator) {
'IN', 'NOT IN' => Request::FILTER_OPERATOR_IN,
'LIKE', 'NOT LIKE' => Request::FILTER_OPERATOR_MATCH,
'EXISTS', 'NOT EXISTS' => Request::FILTER_OPERATOR_EXISTS,
'>' => Request::FILTER_OPERATOR_GT,
'>=' => Request::FILTER_OPERATOR_GTE,
'<' => Request::FILTER_OPERATOR_LT,
'<=' => Request::FILTER_OPERATOR_LTE,
default => Request::FILTER_OPERATOR_EQ,
};
}

private function enforceValueType(string $type, mixed $value): mixed
{
if (\is_array($value)) {
Expand Down
Loading

0 comments on commit b6393af

Please sign in to comment.