|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright 2025 Adobe |
| 4 | + * All Rights Reserved. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\OpenSearch\Plugin\SearchAdapter\Query\Builder; |
| 9 | + |
| 10 | +use Magento\Elasticsearch\SearchAdapter\Query\Builder\QueryInterface as BaseMatchQuery; |
| 11 | +use Magento\CatalogSearch\Model\Indexer\Fulltext\Action\DataProvider; |
| 12 | + |
| 13 | +/** |
| 14 | + * Modifications for sanitize search query and output result when name attribute disabled from search |
| 15 | + */ |
| 16 | +class MatchQueryPlugin |
| 17 | +{ |
| 18 | + /** |
| 19 | + * @var DataProvider |
| 20 | + */ |
| 21 | + private $dataProvider; |
| 22 | + |
| 23 | + /** |
| 24 | + * Constructor |
| 25 | + * |
| 26 | + * @param DataProvider $dataProvider |
| 27 | + */ |
| 28 | + public function __construct( |
| 29 | + DataProvider $dataProvider |
| 30 | + ) { |
| 31 | + $this->dataProvider = $dataProvider; |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * After build plugin to modify selectQuery |
| 36 | + * |
| 37 | + * @param BaseMatchQuery $subject |
| 38 | + * @param array $selectQuery |
| 39 | + * @return array |
| 40 | + * @SuppressWarnings(PHPMD.UnusedFormalParameter) |
| 41 | + */ |
| 42 | + public function afterBuild( |
| 43 | + BaseMatchQuery $subject, |
| 44 | + $selectQuery, |
| 45 | + ): array { |
| 46 | + |
| 47 | + $nameIsSearchable = $this->dataProvider->getSearchableAttribute('name')->getIsSearchable(); |
| 48 | + if ($nameIsSearchable == 0) { |
| 49 | + if (isset($selectQuery['bool']['should'][0]['match_phrase_prefix']['name']['query'])) { |
| 50 | + $requestQueryValue = $selectQuery['bool']['should'][0]['match_phrase_prefix']['name']['query']; |
| 51 | + if (!empty($requestQueryValue) && |
| 52 | + str_contains($requestQueryValue, ' ') || str_contains($requestQueryValue, '-')) { |
| 53 | + $requestQueryValue = str_replace(['-', ' '], '', $requestQueryValue); |
| 54 | + } |
| 55 | + $selectQuery['bool']['should'][0]['match_phrase_prefix']['name']['query'] = $requestQueryValue; |
| 56 | + } |
| 57 | + } |
| 58 | + return $selectQuery; |
| 59 | + } |
| 60 | +} |
0 commit comments