@@ -70,51 +70,6 @@ public function __construct(
70
70
$ this ->config = $ config ;
71
71
}
72
72
73
- /**
74
- * @inheritdoc
75
- */
76
- public function build (array $ selectQuery , RequestQueryInterface $ requestQuery , $ conditionType )
77
- {
78
- $ queryValue = $ this ->prepareQuery ($ requestQuery ->getValue (), $ conditionType );
79
- $ queries = $ this ->buildQueries ($ requestQuery ->getMatches (), $ queryValue );
80
- $ requestQueryBoost = $ requestQuery ->getBoost () ?: 1 ;
81
- $ minimumShouldMatch = $ this ->config ->getElasticsearchConfigData ('minimum_should_match ' );
82
-
83
- foreach ($ queries as $ query ) {
84
- $ queryBody = $ query ['body ' ];
85
- $ matchKey = array_keys ($ queryBody )[0 ];
86
- foreach ($ queryBody [$ matchKey ] as $ field => $ matchQuery ) {
87
- $ matchQuery ['boost ' ] = $ requestQueryBoost + $ matchQuery ['boost ' ];
88
- if ($ minimumShouldMatch && $ matchKey != 'match_phrase_prefix ' ) {
89
- $ matchQuery ['minimum_should_match ' ] = $ minimumShouldMatch ;
90
- }
91
- $ queryBody [$ matchKey ][$ field ] = $ matchQuery ;
92
- }
93
- $ selectQuery ['bool ' ][$ query ['condition ' ]][] = $ queryBody ;
94
- }
95
-
96
- return $ selectQuery ;
97
- }
98
-
99
- /**
100
- * Prepare query
101
- *
102
- * @param string $queryValue
103
- * @param string $conditionType
104
- * @return array
105
- */
106
- private function prepareQuery (string $ queryValue , string $ conditionType ): array
107
- {
108
- $ condition = $ conditionType === BoolExpression::QUERY_CONDITION_NOT
109
- ? self ::QUERY_CONDITION_MUST_NOT
110
- : $ conditionType ;
111
-
112
- return [
113
- 'condition ' => $ condition ,
114
- 'value ' => $ queryValue ,
115
- ];
116
- }
117
-
118
73
/**
119
74
* Creates valid ElasticSearch search conditions from Match queries
120
75
*
@@ -125,21 +80,24 @@ private function prepareQuery(string $queryValue, string $conditionType): array
125
80
* The search query boost is an optional in the search query and therefore it will be set to 1 by default
126
81
* if none passed with a match query.
127
82
*
128
- * @param array $matches
129
- * @param array $queryValue
83
+ * @param array $selectQuery
84
+ * @param RequestQueryInterface $requestQuery
85
+ * @param string $conditionType
130
86
* @return array
131
87
*/
132
- private function buildQueries (array $ matches , array $ queryValue ): array
88
+ public function build (array $ selectQuery , RequestQueryInterface $ requestQuery , $ conditionType )
133
89
{
134
- $ conditions = [];
90
+ $ queryValue = $ this ->prepareQuery ($ requestQuery ->getValue (), $ conditionType );
91
+ $ requestQueryBoost = $ requestQuery ->getBoost () ?: 1 ;
92
+ $ minimumShouldMatch = $ this ->config ->getElasticsearchConfigData ('minimum_should_match ' );
135
93
136
94
// Checking for quoted phrase \"phrase test\", trim escaped surrounding quotes if found
137
95
$ count = 0 ;
138
96
$ value = preg_replace ('#^"(.*)"$#m ' , '$1 ' , $ queryValue ['value ' ], -1 , $ count );
139
97
$ condition = ($ count ) ? 'match_phrase ' : 'match ' ;
140
98
$ transformedTypes = [];
141
99
142
- foreach ($ matches as $ match ) {
100
+ foreach ($ requestQuery -> getMatches () as $ match ) {
143
101
$ resolvedField = $ this ->fieldMapper ->getFieldName (
144
102
$ match ['field ' ],
145
103
['type ' => FieldMapperInterface::TYPE_QUERY ]
@@ -153,29 +111,62 @@ private function buildQueries(array $matches, array $queryValue): array
153
111
$ transformedTypes [$ valueTransformerHash ] = $ valueTransformer ->transform ($ value );
154
112
}
155
113
$ transformedValue = $ transformedTypes [$ valueTransformerHash ];
156
-
157
114
if (null === $ transformedValue ) {
158
115
//Value is incompatible with this field type.
159
116
continue ;
160
117
}
118
+
161
119
$ matchCondition = $ match ['matchCondition ' ] ?? $ condition ;
162
120
$ fields = [];
163
121
$ fields [$ resolvedField ] = [
164
122
'query ' => $ transformedValue ,
165
- 'boost ' => $ match ['boost ' ] ?? 1 ,
123
+ 'boost ' => $ requestQueryBoost + $ match ['boost ' ] ?? 1 ,
166
124
];
167
125
168
126
if (isset ($ match ['analyzer ' ])) {
169
127
$ fields [$ resolvedField ]['analyzer ' ] = $ match ['analyzer ' ];
170
128
}
171
- $ conditions [] = [
172
- ' condition ' => $ queryValue [ ' condition ' ],
173
- ' body ' => [
174
- $ matchCondition => $ fields ,
175
- ],
176
- ];
129
+
130
+ if ( $ minimumShouldMatch && $ this -> isConditionSupportMinimumShouldMatch ( $ matchCondition )) {
131
+ $ fields [ $ resolvedField ][ ' minimum_should_match ' ] = $ minimumShouldMatch ;
132
+ }
133
+
134
+ $ selectQuery [ ' bool ' ][ $ queryValue [ ' condition ' ]][] = [ $ matchCondition => $ fields ];
177
135
}
178
136
179
- return $ conditions ;
137
+ return $ selectQuery ;
138
+ }
139
+
140
+ /**
141
+ * Prepare query
142
+ *
143
+ * @param string $queryValue
144
+ * @param string $conditionType
145
+ * @return array
146
+ */
147
+ private function prepareQuery (string $ queryValue , string $ conditionType ): array
148
+ {
149
+ $ condition = $ conditionType === BoolExpression::QUERY_CONDITION_NOT
150
+ ? self ::QUERY_CONDITION_MUST_NOT
151
+ : $ conditionType ;
152
+
153
+ return [
154
+ 'condition ' => $ condition ,
155
+ 'value ' => $ queryValue ,
156
+ ];
157
+ }
158
+
159
+ /**
160
+ * Check does condition support the minimum_should_match field
161
+ *
162
+ * @param string $condition
163
+ * @return bool
164
+ */
165
+ private function isConditionSupportMinimumShouldMatch (string $ condition ): bool
166
+ {
167
+ return !in_array ($ condition , [
168
+ 'match_phrase_prefix ' ,
169
+ 'match_phrase ' ,
170
+ ]);
180
171
}
181
172
}
0 commit comments