5
5
*/
6
6
namespace Magento \Elasticsearch \SearchAdapter \Query \Builder ;
7
7
8
- use Magento \Elasticsearch \SearchAdapter \Query \ValueTransformerPool ;
9
- use Magento \Framework \App \ObjectManager ;
10
8
use Magento \Framework \Search \Request \Query \BoolExpression ;
11
9
use Magento \Framework \Search \Request \QueryInterface as RequestQueryInterface ;
12
10
use Magento \Elasticsearch \Model \Adapter \FieldMapperInterface ;
13
11
use Magento \Framework \Search \Adapter \Preprocessor \PreprocessorInterface ;
14
12
15
- /**
16
- * Builder for match query.
17
- */
18
13
class Match implements QueryInterface
19
14
{
20
15
/**
@@ -28,35 +23,24 @@ class Match implements QueryInterface
28
23
private $ fieldMapper ;
29
24
30
25
/**
31
- * @deprecated
32
- * @see \Magento\Elasticsearch\SearchAdapter\Query\ValueTransformer\TextTransformer
33
26
* @var PreprocessorInterface[]
34
27
*/
35
28
protected $ preprocessorContainer ;
36
29
37
- /**
38
- * @var ValueTransformerPool
39
- */
40
- private $ valueTransformerPool ;
41
-
42
30
/**
43
31
* @param FieldMapperInterface $fieldMapper
44
32
* @param PreprocessorInterface[] $preprocessorContainer
45
- * @param ValueTransformerPool|null $valueTransformerPool
46
33
*/
47
34
public function __construct (
48
35
FieldMapperInterface $ fieldMapper ,
49
- array $ preprocessorContainer ,
50
- ValueTransformerPool $ valueTransformerPool = null
36
+ array $ preprocessorContainer
51
37
) {
52
38
$ this ->fieldMapper = $ fieldMapper ;
53
39
$ this ->preprocessorContainer = $ preprocessorContainer ;
54
- $ this ->valueTransformerPool = $ valueTransformerPool ?? ObjectManager::getInstance ()
55
- ->get (ValueTransformerPool::class);
56
40
}
57
41
58
42
/**
59
- * @inheritdoc
43
+ * { @inheritdoc}
60
44
*/
61
45
public function build (array $ selectQuery , RequestQueryInterface $ requestQuery , $ conditionType )
62
46
{
@@ -77,14 +61,16 @@ public function build(array $selectQuery, RequestQueryInterface $requestQuery, $
77
61
}
78
62
79
63
/**
80
- * Prepare query.
81
- *
82
64
* @param string $queryValue
83
65
* @param string $conditionType
84
66
* @return array
85
67
*/
86
68
protected function prepareQuery ($ queryValue , $ conditionType )
87
69
{
70
+ $ queryValue = $ this ->escape ($ queryValue );
71
+ foreach ($ this ->preprocessorContainer as $ preprocessor ) {
72
+ $ queryValue = $ preprocessor ->process ($ queryValue );
73
+ }
88
74
$ condition = $ conditionType === BoolExpression::QUERY_CONDITION_NOT ?
89
75
self ::QUERY_CONDITION_MUST_NOT : $ conditionType ;
90
76
return [
@@ -113,34 +99,21 @@ protected function buildQueries(array $matches, array $queryValue)
113
99
114
100
// Checking for quoted phrase \"phrase test\", trim escaped surrounding quotes if found
115
101
$ count = 0 ;
116
- $ value = preg_replace ('#^"(.*)"$#m ' , '$1 ' , $ queryValue ['value ' ], -1 , $ count );
102
+ $ value = preg_replace ('#^ \\\\ "(.*) \\\\ "$#m ' , '$1 ' , $ queryValue ['value ' ], -1 , $ count );
117
103
$ condition = ($ count ) ? 'match_phrase ' : 'match ' ;
118
104
119
- $ attributesTypes = $ this ->fieldMapper ->getAllAttributesTypes ();
120
- $ transformedTypes = [];
121
105
foreach ($ matches as $ match ) {
122
106
$ resolvedField = $ this ->fieldMapper ->getFieldName (
123
107
$ match ['field ' ],
124
108
['type ' => FieldMapperInterface::TYPE_QUERY ]
125
109
);
126
- $ valueTransformer = $ this ->valueTransformerPool ->get ($ attributesTypes [$ resolvedField ]['type ' ] ?? 'text ' );
127
- $ valueTransformerHash = \spl_object_hash ($ valueTransformer );
128
- if (!isset ($ transformedTypes [$ valueTransformerHash ])) {
129
- $ transformedTypes [$ valueTransformerHash ] = $ valueTransformer ->transform ($ value );
130
- }
131
- $ transformedValue = $ transformedTypes [$ valueTransformerHash ];
132
- if (null === $ transformedValue ) {
133
- //Value is incompatible with this field type.
134
- continue ;
135
- }
136
-
137
110
$ conditions [] = [
138
111
'condition ' => $ queryValue ['condition ' ],
139
112
'body ' => [
140
113
$ condition => [
141
114
$ resolvedField => [
142
- 'query ' => $ transformedValue ,
143
- 'boost ' => $ match ['boost ' ] ?? 1 ,
115
+ 'query ' => $ value ,
116
+ 'boost ' => isset ( $ match ['boost ' ]) ? $ match [ ' boost ' ] : 1 ,
144
117
],
145
118
],
146
119
],
@@ -151,15 +124,18 @@ protected function buildQueries(array $matches, array $queryValue)
151
124
}
152
125
153
126
/**
127
+ * Cut trailing plus or minus sign, and @ symbol, using of which causes InnoDB to report a syntax error.
128
+ * @link https://dev.mysql.com/doc/refman/5.7/en/fulltext-boolean.html Fulltext-boolean search docs.
129
+ *
154
130
* Escape a value for special query characters such as ':', '(', ')', '*', '?', etc.
155
131
*
156
- * @deprecated
157
- * @see \Magento\Elasticsearch\SearchAdapter\Query\ValueTransformer\TextTransformer
158
132
* @param string $value
159
133
* @return string
160
134
*/
161
135
protected function escape ($ value )
162
136
{
137
+ $ value = preg_replace ('/@+|[@+-]+$/ ' , '' , $ value );
138
+
163
139
$ pattern = '/(\+|-|&&|\|\||!|\(|\)|\{|}|\[|]|\^|"|~|\*|\?|:| \\\)/ ' ;
164
140
$ replace = '\\\$1 ' ;
165
141
0 commit comments