Skip to content

Commit c15edc0

Browse files
committed
Added changes for AC-13691 partial search
1 parent f309978 commit c15edc0

File tree

2 files changed

+64
-2
lines changed

2 files changed

+64
-2
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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+
*/
41+
public function afterBuild(
42+
BaseMatchQuery $subject,
43+
$selectQuery,
44+
): array {
45+
46+
$nameIsSearchable = $this->dataProvider->getSearchableAttribute('name')->getIsSearchable();
47+
if ($nameIsSearchable === 0) {
48+
if (isset($selectQuery['bool']['should'][0]['match_phrase_prefix']['name']['query'])) {
49+
$requestQueryValue = $selectQuery['bool']['should'][0]['match_phrase_prefix']['name']['query'];
50+
if (!empty($requestQueryValue) &&
51+
str_contains($requestQueryValue, ' ') || str_contains($requestQueryValue, '-')) {
52+
$requestQueryValue = str_replace(['-', ' '], '', $requestQueryValue);
53+
}
54+
$selectQuery['bool']['should'][0]['match_phrase_prefix']['name']['query'] = $requestQueryValue;
55+
}
56+
}
57+
return $selectQuery;
58+
}
59+
}

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)