Complex classes like Query often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Query, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
58 | class Query implements DSL |
||
59 | { |
||
60 | /** |
||
61 | * must return type for QueryBuilder usage. |
||
62 | */ |
||
63 | public function getType(): string |
||
64 | { |
||
65 | return self::TYPE_QUERY; |
||
66 | } |
||
67 | |||
68 | /** |
||
69 | * match query. |
||
70 | * |
||
71 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-query.html |
||
72 | * |
||
73 | * @param mixed $values |
||
74 | */ |
||
75 | public function match(?string $field = null, $values = null): Match |
||
76 | { |
||
77 | return new Match($field, $values); |
||
78 | } |
||
79 | |||
80 | /** |
||
81 | * multi match query. |
||
82 | * |
||
83 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-multi-match-query.html |
||
84 | */ |
||
85 | public function multi_match(): MultiMatch |
||
89 | |||
90 | /** |
||
91 | * bool query. |
||
92 | * |
||
93 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html |
||
94 | */ |
||
95 | public function bool(): BoolQuery |
||
96 | { |
||
97 | return new BoolQuery(); |
||
98 | } |
||
99 | |||
100 | /** |
||
101 | * boosting query. |
||
102 | * |
||
103 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-boosting-query.html |
||
104 | */ |
||
105 | public function boosting(): Boosting |
||
106 | { |
||
107 | return new Boosting(); |
||
108 | } |
||
109 | |||
110 | /** |
||
111 | * common terms query. |
||
112 | * |
||
113 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-common-terms-query.html |
||
114 | * |
||
115 | * @param float $cutoffFrequency percentage in decimal form (.001 == 0.1%) |
||
116 | */ |
||
117 | public function common_terms(string $field, string $query, float $cutoffFrequency): Common |
||
118 | { |
||
119 | return new Common($field, $query, $cutoffFrequency); |
||
120 | } |
||
121 | |||
122 | /** |
||
123 | * constant score query. |
||
124 | * |
||
125 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-constant-score-query.html |
||
126 | */ |
||
127 | public function constant_score(?AbstractQuery $filter = null): ConstantScore |
||
128 | { |
||
129 | return new ConstantScore($filter); |
||
130 | } |
||
131 | |||
132 | /** |
||
133 | * dis max query. |
||
134 | * |
||
135 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-dis-max-query.html |
||
136 | */ |
||
137 | public function dis_max(): DisMax |
||
138 | { |
||
139 | return new DisMax(); |
||
140 | } |
||
141 | |||
142 | /** |
||
143 | * distance feature query. |
||
144 | * |
||
145 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-distance-feature-query.html |
||
146 | * |
||
147 | * @param string|array $origin |
||
148 | */ |
||
149 | public function distance_feature(string $field, $origin, string $pivot): DistanceFeature |
||
150 | { |
||
151 | return new DistanceFeature($field, $origin, $pivot); |
||
152 | } |
||
153 | |||
154 | /** |
||
155 | * function score query. |
||
156 | * |
||
157 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-function-score-query.html |
||
158 | */ |
||
159 | public function function_score(): FunctionScore |
||
160 | { |
||
161 | return new FunctionScore(); |
||
162 | } |
||
163 | |||
164 | /** |
||
165 | * fuzzy query. |
||
166 | * |
||
167 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-fuzzy-query.html |
||
168 | * |
||
169 | * @param string $value String to search for |
||
170 | */ |
||
171 | public function fuzzy(?string $fieldName = null, ?string $value = null): Fuzzy |
||
172 | { |
||
173 | return new Fuzzy($fieldName, $value); |
||
174 | } |
||
175 | |||
176 | /** |
||
177 | * geo bounding box query. |
||
178 | * |
||
179 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-geo-bounding-box-query.html |
||
180 | */ |
||
181 | public function geo_bounding_box(string $key, array $coordinates): GeoBoundingBox |
||
182 | { |
||
183 | return new GeoBoundingBox($key, $coordinates); |
||
184 | } |
||
185 | |||
186 | /** |
||
187 | * geo distance query. |
||
188 | * |
||
189 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-geo-distance-query.html |
||
190 | * |
||
191 | * @param array|string $location |
||
192 | */ |
||
193 | public function geo_distance(string $key, $location, string $distance): GeoDistance |
||
194 | { |
||
195 | return new GeoDistance($key, $location, $distance); |
||
196 | } |
||
197 | |||
198 | /** |
||
199 | * geo polygon query. |
||
200 | * |
||
201 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-geo-polygon-query.html |
||
202 | */ |
||
203 | public function geo_polygon(string $key, array $points): GeoPolygon |
||
204 | { |
||
205 | return new GeoPolygon($key, $points); |
||
206 | } |
||
207 | |||
208 | /** |
||
209 | * has child query. |
||
210 | * |
||
211 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-has-child-query.html |
||
212 | * |
||
213 | * @param string|BaseQuery|AbstractQuery $query |
||
214 | * @param string $type Parent document type |
||
215 | */ |
||
216 | public function has_child($query, ?string $type = null): HasChild |
||
217 | { |
||
218 | return new HasChild($query, $type); |
||
219 | } |
||
220 | |||
221 | /** |
||
222 | * has parent query. |
||
223 | * |
||
224 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-has-parent-query.html |
||
225 | * |
||
226 | * @param string|BaseQuery|AbstractQuery $query |
||
227 | * @param string $type Parent document type |
||
228 | */ |
||
229 | public function has_parent($query, string $type): HasParent |
||
230 | { |
||
231 | return new HasParent($query, $type); |
||
232 | } |
||
233 | |||
234 | /** |
||
235 | * ids query. |
||
236 | * |
||
237 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-ids-query.html |
||
238 | */ |
||
239 | public function ids(array $ids = []): Ids |
||
240 | { |
||
241 | return new Ids($ids); |
||
242 | } |
||
243 | |||
244 | /** |
||
245 | * match all query. |
||
246 | * |
||
247 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-all-query.html |
||
248 | */ |
||
249 | public function match_all(): MatchAll |
||
250 | { |
||
251 | return new MatchAll(); |
||
252 | } |
||
253 | |||
254 | /** |
||
255 | * match none query. |
||
256 | * |
||
257 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-all-query.html#query-dsl-match-none-query |
||
258 | */ |
||
259 | public function match_none(): MatchNone |
||
260 | { |
||
261 | return new MatchNone(); |
||
262 | } |
||
263 | |||
264 | /** |
||
265 | * match phrase query. |
||
266 | * |
||
267 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-query-phrase.html |
||
268 | */ |
||
269 | public function match_phrase(?string $field = null, $values = null): MatchPhrase |
||
270 | { |
||
271 | return new MatchPhrase($field, $values); |
||
272 | } |
||
273 | |||
274 | /** |
||
275 | * match phrase prefix query. |
||
276 | * |
||
277 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-query-phrase-prefix.html |
||
278 | */ |
||
279 | public function match_phrase_prefix(?string $field = null, $values = null): MatchPhrasePrefix |
||
280 | { |
||
281 | return new MatchPhrasePrefix($field, $values); |
||
282 | } |
||
283 | |||
284 | /** |
||
285 | * more like this query. |
||
286 | * |
||
287 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-mlt-query.html |
||
288 | */ |
||
289 | public function more_like_this(): MoreLikeThis |
||
290 | { |
||
291 | return new MoreLikeThis(); |
||
292 | } |
||
293 | |||
294 | /** |
||
295 | * nested query. |
||
296 | * |
||
297 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-nested-query.html |
||
298 | */ |
||
299 | public function nested(): Nested |
||
300 | { |
||
301 | return new Nested(); |
||
302 | } |
||
303 | |||
304 | /** |
||
305 | * @param int|string $id |
||
306 | */ |
||
307 | public function parent_id(string $type, $id, bool $ignoreUnmapped = false): ParentId |
||
308 | { |
||
309 | return new ParentId($type, $id, $ignoreUnmapped); |
||
310 | } |
||
311 | |||
312 | /** |
||
313 | * prefix query. |
||
314 | * |
||
315 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-prefix-query.html |
||
316 | * |
||
317 | * @param array $prefix Prefix array |
||
318 | */ |
||
319 | public function prefix(array $prefix = []): Prefix |
||
320 | { |
||
321 | return new Prefix($prefix); |
||
322 | } |
||
323 | |||
324 | /** |
||
325 | * query string query. |
||
326 | * |
||
327 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-query-string-query.html |
||
328 | * |
||
329 | * @param string $queryString OPTIONAL Query string for object |
||
330 | */ |
||
331 | public function query_string(string $queryString = ''): QueryString |
||
332 | { |
||
333 | return new QueryString($queryString); |
||
334 | } |
||
335 | |||
336 | /** |
||
337 | * simple_query_string query. |
||
338 | * |
||
339 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-simple-query-string-query.html |
||
340 | */ |
||
341 | public function simple_query_string(string $query, array $fields = []): SimpleQueryString |
||
342 | { |
||
343 | return new SimpleQueryString($query, $fields); |
||
344 | } |
||
345 | |||
346 | /** |
||
347 | * range query. |
||
348 | * |
||
349 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-range-query.html |
||
350 | */ |
||
351 | public function range(?string $fieldName = null, array $args = []): Range |
||
355 | |||
356 | /** |
||
357 | * regexp query. |
||
358 | * |
||
359 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-regexp-query.html |
||
360 | */ |
||
361 | public function regexp(string $key = '', ?string $value = null, float $boost = 1.0): Regexp |
||
362 | { |
||
363 | return new Regexp($key, $value, $boost); |
||
364 | } |
||
365 | |||
366 | /** |
||
367 | * span first query. |
||
368 | * |
||
369 | * @param AbstractQuery|array $match |
||
370 | * |
||
371 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-span-first-query.html |
||
372 | */ |
||
373 | public function span_first($match = null, ?int $end = null): SpanFirst |
||
374 | { |
||
375 | return new SpanFirst($match, $end); |
||
376 | } |
||
377 | |||
378 | /** |
||
379 | * span multi term query. |
||
380 | * |
||
381 | * @param AbstractQuery|array $match |
||
382 | * |
||
383 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-span-multi-term-query.html |
||
384 | */ |
||
385 | public function span_multi_term($match = null): SpanMulti |
||
389 | |||
390 | /** |
||
391 | * span near query. |
||
392 | * |
||
393 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-span-near-query.html |
||
394 | */ |
||
395 | public function span_near(array $clauses = [], int $slop = 1, bool $inOrder = false): SpanNear |
||
399 | |||
400 | /** |
||
401 | * span not query. |
||
402 | * |
||
403 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-span-not-query.html |
||
404 | */ |
||
405 | public function span_not(?AbstractSpanQuery $include = null, ?AbstractSpanQuery $exclude = null): SpanNot |
||
409 | |||
410 | /** |
||
411 | * span_or query. |
||
412 | * |
||
413 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-span-or-query.html |
||
414 | */ |
||
415 | public function span_or(array $clauses = []): SpanOr |
||
419 | |||
420 | /** |
||
421 | * span_term query. |
||
422 | * |
||
423 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-span-term-query.html |
||
424 | */ |
||
425 | public function span_term(array $term = []): SpanTerm |
||
429 | |||
430 | /** |
||
431 | * span_containing query. |
||
432 | * |
||
433 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-span-containing-query.html |
||
434 | */ |
||
435 | public function span_containing(?AbstractSpanQuery $little = null, ?AbstractSpanQuery $big = null): SpanContaining |
||
439 | |||
440 | /** |
||
441 | * span_within query. |
||
442 | * |
||
443 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-span-within-query.html |
||
444 | */ |
||
445 | public function span_within(?AbstractSpanQuery $little = null, ?AbstractSpanQuery $big = null): SpanWithin |
||
449 | |||
450 | /** |
||
451 | * term query. |
||
452 | * |
||
453 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-term-query.html |
||
454 | */ |
||
455 | public function term(array $term = []): Term |
||
459 | |||
460 | /** |
||
461 | * terms query. |
||
462 | * |
||
463 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-terms-query.html |
||
464 | */ |
||
465 | public function terms(string $key = '', array $terms = []): Terms |
||
469 | |||
470 | /** |
||
471 | * wildcard query. |
||
472 | * |
||
473 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-wildcard-query.html |
||
474 | * |
||
475 | * @param string $key OPTIONAL Wildcard key |
||
476 | * @param string|null $value OPTIONAL Wildcard value |
||
477 | * @param float $boost OPTIONAL Boost value (default = 1) |
||
478 | */ |
||
479 | public function wildcard(string $key = '', ?string $value = null, float $boost = 1.0): Wildcard |
||
483 | |||
484 | /** |
||
485 | * exists query. |
||
486 | * |
||
487 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-exists-query.html |
||
488 | */ |
||
489 | public function exists(string $field): Exists |
||
493 | |||
494 | /** |
||
495 | * type query. |
||
496 | * |
||
497 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-percolate-query.html |
||
498 | */ |
||
499 | public function percolate(): Percolate |
||
503 | } |
||
504 |