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 ;
@@ -28,31 +26,20 @@ class Match implements QueryInterface
28
26
private $ fieldMapper ;
29
27
30
28
/**
31
- * @deprecated
32
- * @see \Magento\Elasticsearch\SearchAdapter\Query\ValueTransformer\TextTransformer
33
29
* @var PreprocessorInterface[]
34
30
*/
35
31
protected $ preprocessorContainer ;
36
32
37
- /**
38
- * @var ValueTransformerPool
39
- */
40
- private $ valueTransformerPool ;
41
-
42
33
/**
43
34
* @param FieldMapperInterface $fieldMapper
44
35
* @param PreprocessorInterface[] $preprocessorContainer
45
- * @param ValueTransformerPool|null $valueTransformerPool
46
36
*/
47
37
public function __construct (
48
38
FieldMapperInterface $ fieldMapper ,
49
- array $ preprocessorContainer ,
50
- ValueTransformerPool $ valueTransformerPool = null
39
+ array $ preprocessorContainer
51
40
) {
52
41
$ this ->fieldMapper = $ fieldMapper ;
53
42
$ this ->preprocessorContainer = $ preprocessorContainer ;
54
- $ this ->valueTransformerPool = $ valueTransformerPool ?? ObjectManager::getInstance ()
55
- ->get (ValueTransformerPool::class);
56
43
}
57
44
58
45
/**
@@ -85,6 +72,10 @@ public function build(array $selectQuery, RequestQueryInterface $requestQuery, $
85
72
*/
86
73
protected function prepareQuery ($ queryValue , $ conditionType )
87
74
{
75
+ $ queryValue = $ this ->escape ($ queryValue );
76
+ foreach ($ this ->preprocessorContainer as $ preprocessor ) {
77
+ $ queryValue = $ preprocessor ->process ($ queryValue );
78
+ }
88
79
$ condition = $ conditionType === BoolExpression::QUERY_CONDITION_NOT ?
89
80
self ::QUERY_CONDITION_MUST_NOT : $ conditionType ;
90
81
return [
@@ -113,34 +104,21 @@ protected function buildQueries(array $matches, array $queryValue)
113
104
114
105
// Checking for quoted phrase \"phrase test\", trim escaped surrounding quotes if found
115
106
$ count = 0 ;
116
- $ value = preg_replace ('#^"(.*)"$#m ' , '$1 ' , $ queryValue ['value ' ], -1 , $ count );
107
+ $ value = preg_replace ('#^ \\\\ "(.*) \\\\ "$#m ' , '$1 ' , $ queryValue ['value ' ], -1 , $ count );
117
108
$ condition = ($ count ) ? 'match_phrase ' : 'match ' ;
118
109
119
- $ attributesTypes = $ this ->fieldMapper ->getAllAttributesTypes ();
120
- $ transformedTypes = [];
121
110
foreach ($ matches as $ match ) {
122
111
$ resolvedField = $ this ->fieldMapper ->getFieldName (
123
112
$ match ['field ' ],
124
113
['type ' => FieldMapperInterface::TYPE_QUERY ]
125
114
);
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
115
$ conditions [] = [
138
116
'condition ' => $ queryValue ['condition ' ],
139
117
'body ' => [
140
118
$ condition => [
141
119
$ resolvedField => [
142
- 'query ' => $ transformedValue ,
143
- 'boost ' => $ match ['boost ' ] ?? 1 ,
120
+ 'query ' => $ value ,
121
+ 'boost ' => isset ( $ match ['boost ' ]) ? $ match [ ' boost ' ] : 1 ,
144
122
],
145
123
],
146
124
],
@@ -153,13 +131,16 @@ protected function buildQueries(array $matches, array $queryValue)
153
131
/**
154
132
* Escape a value for special query characters such as ':', '(', ')', '*', '?', etc.
155
133
*
156
- * @deprecated
157
- * @see \Magento\Elasticsearch\SearchAdapter\Query\ValueTransformer\TextTransformer
134
+ * Cut trailing plus or minus sign, and @ symbol, using of which causes InnoDB to report a syntax error.
135
+ * https://dev.mysql.com/doc/refman/5.7/en/fulltext-boolean.html Fulltext-boolean search docs.
136
+ *
158
137
* @param string $value
159
138
* @return string
160
139
*/
161
140
protected function escape ($ value )
162
141
{
142
+ $ value = preg_replace ('/@+|[@+-]+$/ ' , '' , $ value );
143
+
163
144
$ pattern = '/(\+|-|&&|\|\||!|\(|\)|\{|}|\[|]|\^|"|~|\*|\?|:| \\\)/ ' ;
164
145
$ replace = '\\\$1 ' ;
165
146
0 commit comments