Skip to content

Commit b1e3430

Browse files
committed
Merge remote-tracking branch 'origin/AC-13691' into spartans_pr_28042025
2 parents 1cca741 + 35fc357 commit b1e3430

File tree

2 files changed

+65
-2
lines changed

2 files changed

+65
-2
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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+
}

app/code/Magento/OpenSearch/etc/di.xml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?xml version="1.0"?>
22
<!--
33
/**
4-
* Copyright © Magento, Inc. All rights reserved.
5-
* See COPYING.txt for license details.
4+
* Copyright 2022 Adobe
5+
* All Rights Reserved.
66
*/
77
-->
88
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
@@ -287,4 +287,7 @@
287287
</argument>
288288
</arguments>
289289
</type>
290+
<type name="Magento\Elasticsearch\SearchAdapter\Query\Builder\QueryInterface">
291+
<plugin name="sanitize_search_string" type="Magento\OpenSearch\Plugin\SearchAdapter\Query\Builder\MatchQueryPlugin" />
292+
</type>
290293
</config>

0 commit comments

Comments
 (0)