22
33namespace PDPhilip \Elasticsearch \DSL ;
44
5-
6- use ONGR \ElasticsearchDSL \Query \FullText \QueryStringQuery ;
7- use ONGR \ElasticsearchDSL \Query \MatchAllQuery ;
8- use ONGR \ElasticsearchDSL \Search ;
9- use ONGR \ElasticsearchDSL \Sort \FieldSort ;
105use Exception ;
116
127trait QueryBuilder
138{
14-
15- public static $ filter ;
16-
9+
10+ protected static $ filter ;
11+
1712 protected static $ bucketOperators = ['and ' , 'or ' ];
18-
13+
1914 protected static $ equivalenceOperators = ['in ' , 'nin ' ];
20-
15+
2116 protected static $ clauseOperators = ['ne ' , 'gt ' , 'gte ' , 'lt ' , 'lte ' , 'between ' , 'not_between ' , 'like ' , 'not_like ' , 'exists ' , 'regex ' ];
22-
23-
17+
18+
2419 //======================================================================
2520 // Parameter builders
2621 //======================================================================
27-
28- public static function buildSearchParams ($ index , $ searchQuery , $ searchOptions , $ wheres = [], $ options = [], $ fields = [], $ columns = [])
22+
23+ /**
24+ * @throws Exception
25+ */
26+ public function buildSearchParams ($ index , $ searchQuery , $ searchOptions , $ wheres = [], $ options = [], $ fields = [], $ columns = []): array
2927 {
3028 $ params = [];
3129 if ($ index ) {
3230 $ params ['index ' ] = $ index ;
3331 }
3432 $ params ['body ' ] = [];
35-
36-
33+
34+
3735 $ queryString ['query ' ] = $ searchQuery ;
3836 if ($ wheres ) {
39- $ wheres = self :: _buildQuery ($ wheres );
37+ $ wheres = $ this -> _buildQuery ($ wheres );
4038 $ whereQueryString = $ wheres ['query ' ]['query_string ' ]['query ' ] ?? null ;
4139 if ($ whereQueryString ) {
4240 $ queryString ['query ' ] = '( ' .$ searchQuery .') AND ' .$ whereQueryString ;
4341 }
4442 }
45-
43+
4644 if ($ fields ) {
4745 $ queryString ['fields ' ] = [];
4846 foreach ($ fields as $ field => $ boostLevel ) {
@@ -57,14 +55,14 @@ public static function buildSearchParams($index, $searchQuery, $searchOptions, $
5755 $ queryString [$ searchOption ] = $ searchOptionValue ;
5856 }
5957 }
60-
58+
6159 $ params ['body ' ]['query ' ]['query_string ' ] = $ queryString ;
62-
60+
6361 if ($ columns && $ columns != '* ' ) {
6462 $ params ['body ' ]['_source ' ] = $ columns ;
6563 }
6664 if ($ options ) {
67- $ opts = self :: _buildOptions ($ options );
65+ $ opts = $ this -> _buildOptions ($ options );
6866 if ($ opts ) {
6967 foreach ($ opts as $ key => $ value ) {
7068 if (isset ($ params [$ key ])) {
@@ -76,30 +74,33 @@ public static function buildSearchParams($index, $searchQuery, $searchOptions, $
7674 }
7775 }
7876 if (self ::$ filter ) {
79- $ params = self :: _parseFilterParameter ($ params , self ::$ filter );
77+ $ params = $ this -> _parseFilterParameter ($ params , self ::$ filter );
8078 }
81-
79+
8280 return $ params ;
8381 }
84-
85- public static function buildParams ($ index , $ wheres , $ options = [], $ columns = [], $ _id = null )
82+
83+ /**
84+ * @throws Exception
85+ */
86+ public function buildParams ($ index , $ wheres , $ options = [], $ columns = [], $ _id = null ): array
8687 {
8788 if ($ index ) {
8889 $ params = [
8990 'index ' => $ index ,
9091 ];
9192 }
92-
93+
9394 if ($ _id ) {
9495 $ params ['id ' ] = $ _id ;
9596 }
96-
97- $ params ['body ' ] = self :: _buildQuery ($ wheres );
97+
98+ $ params ['body ' ] = $ this -> _buildQuery ($ wheres );
9899 if ($ columns && $ columns != '* ' ) {
99100 $ params ['body ' ]['_source ' ] = $ columns ;
100101 }
101-
102- $ opts = self :: _buildOptions ($ options );
102+
103+ $ opts = $ this -> _buildOptions ($ options );
103104 if ($ opts ) {
104105 foreach ($ opts as $ key => $ value ) {
105106 if (isset ($ params [$ key ])) {
@@ -110,76 +111,77 @@ public static function buildParams($index, $wheres, $options = [], $columns = []
110111 }
111112 }
112113 if (self ::$ filter ) {
113- $ params = self :: _parseFilterParameter ($ params , self ::$ filter );
114+ $ params = $ this -> _parseFilterParameter ($ params , self ::$ filter );
114115 }
115-
116+
116117 return $ params ;
117118 }
118-
119+
119120 //----------------------------------------------------------------------
120121 // Parsers
121122 //----------------------------------------------------------------------
122-
123- private static function _buildQueryString ($ wheres ): string
123+
124+ private function _buildQueryString ($ wheres ): string
124125 {
125126 if ($ wheres ) {
126127 foreach ($ wheres as $ key => $ value ) {
127- return self :: _parseParams ($ key , $ value );
128+ return $ this -> _parseParams ($ key , $ value );
128129 }
129130 }
130-
131+
131132 return '' ;
132133 }
133-
134+
134135 private static function _andQueryString ($ values ): string
135136 {
136137 $ strings = [];
137138 foreach ($ values as $ key => $ val ) {
138139 $ strings [] = self ::_parseParams ($ key , $ val );
139140 }
140-
141+
141142 return '( ' .implode (' AND ' , $ strings ).') ' ;
142143 }
143-
144+
144145 private static function _orQueryString ($ values ): string
145146 {
146147 $ strings = [];
147148 foreach ($ values as $ key => $ val ) {
148149 $ strings [] = self ::_parseParams ($ key , $ val );
149150 }
150-
151+
151152 return '( ' .implode (' OR ' , $ strings ).') ' ;
152153 }
153-
154+
154155 private static function _inQueryString ($ key , $ values ): string
155156 {
156157 $ strings = [];
157158 foreach ($ values as $ val ) {
158159 $ strings [] = self ::_parseParams (null , $ val );
159160 }
160-
161+
161162 return '( ' .$ key .':( ' .implode (' OR ' , $ strings ).')) ' ;
162163 }
163-
164+
164165 private static function _ninQueryString ($ key , $ values ): string
165166 {
166167 $ strings = [];
167168 foreach ($ values as $ val ) {
168169 $ strings [] = self ::_parseParams (null , $ val );
169170 }
170-
171+
171172 return '(NOT ' .$ key .':( ' .implode (' OR ' , $ strings ).')) ' ;
172173 }
173-
174+
174175 private static function _parseParams ($ key , $ value ): string
175176 {
177+
176178 if ($ key === 'and ' || $ key === 'or ' ) {
177179 return self ::{'_ ' .$ key .'QueryString ' }($ value );
178180 }
179181 if (is_array ($ value )) {
180-
182+
181183 foreach ($ value as $ op => $ opVal ) {
182-
184+
183185 if (in_array ($ op , self ::$ bucketOperators )) {
184186 return self ::{'_ ' .$ op .'QueryString ' }($ opVal );
185187 }
@@ -193,7 +195,7 @@ private static function _parseParams($key, $value): string
193195 // Is not equal to null => exists and has a value
194196 return '(_exists_: ' .$ key .') ' ;
195197 }
196-
198+
197199 return '(NOT ' .$ key .': ' .self ::_escape ($ opVal ).') ' ;
198200 case 'lt ' :
199201 return '( ' .$ key .':{* TO ' .$ opVal .'}) ' ;
@@ -217,17 +219,17 @@ private static function _parseParams($key, $value): string
217219 if ($ opVal ) {
218220 return '(_exists_: ' .$ key .') ' ;
219221 }
220-
222+
221223 return '(NOT _exists_: ' .$ key .') ' ;
222-
224+
223225 }
224-
226+
225227 }
226-
228+
227229 return self ::_parseParams ($ op , $ opVal );
228230 }
229231 }
230-
232+
231233 if (!$ key ) {
232234 return self ::_escape ($ value );
233235 }
@@ -240,46 +242,41 @@ private static function _parseParams($key, $value): string
240242 if ($ value === null ) {
241243 return '(NOT _exists_: ' .$ key .') ' ;
242244 }
243-
245+
244246 return '( ' .$ key .':" ' .self ::_escape ($ value ).'") ' ;
245-
247+
246248 }
247-
249+
248250 public static function _escape ($ string ): string
249251 {
250252 //+ - = && || > < ! ( ) { } [ ] ^ " ~ * ? : \ /
251253 $ stripped = preg_replace ('/\W/ ' , '\\\\$0 ' , $ string );
252-
254+
253255 //Put the spaces back;
254256 $ stripped = str_replace ('\ ' , ' ' , $ stripped );
255257 //Edge cases
256258 $ stripped = str_replace ('\&\& ' , '\&& ' , $ stripped );
257259 $ stripped = str_replace ('\|\| ' , '\|| ' , $ stripped );
258-
260+
259261 return $ stripped ;
260-
262+
261263 }
262-
263- private static function _buildQuery ($ wheres ): array
264+
265+ private function _buildQuery ($ wheres ): array
264266 {
265- $ search = new Search ();
266267 if (!$ wheres ) {
267- $ search ->addQuery (new MatchAllQuery ());
268- $ search ->toArray ();
269-
270- return $ search ->toArray ();
268+ return ParameterBuilder::matchAll ();
271269 }
272- $ string = self ::_buildQueryString ($ wheres );
273- $ queryStringQuery = new QueryStringQuery ($ string );
274- $ search ->addQuery ($ queryStringQuery );
275-
276- return $ search ->toArray ();
277-
270+ $ string = $ this ->_buildQueryString ($ wheres );
271+
272+ return ParameterBuilder::queryStringQuery ($ string );
278273 }
279-
280- private static function _buildOptions ($ options ): array
274+
275+ /**
276+ * @throws Exception
277+ */
278+ private function _buildOptions ($ options ): array
281279 {
282-
283280 $ return = [];
284281 if ($ options ) {
285282 foreach ($ options as $ key => $ value ) {
@@ -291,7 +288,7 @@ private static function _buildOptions($options): array
291288 if (!isset ($ return ['body ' ]['sort ' ])) {
292289 $ return ['body ' ]['sort ' ] = [];
293290 }
294- $ sortBy = self :: _parseSortOrder ($ value );
291+ $ sortBy = $ this -> _parseSortOrder ($ value );
295292 $ return ['body ' ]['sort ' ][] = $ sortBy ;
296293 break ;
297294 case 'skip ' :
@@ -302,7 +299,7 @@ private static function _buildOptions($options): array
302299 break ;
303300 case 'filters ' :
304301 foreach ($ value as $ filterType => $ filerValues ) {
305- self :: _parseFilter ($ filterType , $ filerValues );
302+ $ this -> _parseFilter ($ filterType , $ filerValues );
306303 }
307304 break ;
308305 case 'multiple ' :
@@ -314,11 +311,11 @@ private static function _buildOptions($options): array
314311 }
315312 }
316313 }
317-
314+
318315 return $ return ;
319316 }
320-
321- public static function _parseFilter ($ filterType , $ filterPayload )
317+
318+ public function _parseFilter ($ filterType , $ filterPayload ): void
322319 {
323320 switch ($ filterType ) {
324321 case 'filterGeoBox ' :
@@ -334,27 +331,26 @@ public static function _parseFilter($filterType, $filterPayload)
334331 'lat ' => $ filterPayload ['geoPoint ' ][0 ],
335332 'lon ' => $ filterPayload ['geoPoint ' ][1 ],
336333 ],
337-
334+
338335 ];
339336 break ;
340337 }
341338 }
342-
343- private static function _parseSortOrder ($ value ): array
339+
340+ private function _parseSortOrder ($ value ): array
344341 {
345342 $ field = array_key_first ($ value );
346343 $ direction = $ value [$ field ];
347-
344+
348345 $ dir = 'desc ' ;
349346 if ($ direction == 1 ) {
350347 $ dir = 'asc ' ;
351348 }
352- $ sort = new FieldSort ($ field , $ dir );
353-
354- return $ sort ->toArray ();
349+
350+ return ParameterBuilder::fieldSort ($ field , $ dir );
355351 }
356-
357- public static function _parseFilterParameter ($ params , $ filer )
352+
353+ public function _parseFilterParameter ($ params , $ filer )
358354 {
359355 $ body = $ params ['body ' ];
360356 if (!empty ($ body ['query ' ]['match_all ' ])) {
@@ -383,7 +379,7 @@ public static function _parseFilterParameter($params, $filer)
383379 ];
384380 $ params ['body ' ] = $ filteredBody ;
385381 }
386-
382+
387383 return $ params ;
388384 }
389385}
0 commit comments