| Total Complexity | 62 |
| Total Lines | 284 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Search 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.
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 Search, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 20 | class Search |
||
| 21 | { |
||
| 22 | /** |
||
| 23 | * @var Client |
||
| 24 | */ |
||
| 25 | protected $_client; |
||
| 26 | |||
| 27 | protected $_query; |
||
| 28 | protected $_body; |
||
| 29 | /** |
||
| 30 | * @var array |
||
| 31 | */ |
||
| 32 | protected $_params = []; |
||
| 33 | |||
| 34 | public function __construct(Client $client) |
||
| 35 | { |
||
| 36 | $this->_client = $client; |
||
| 37 | $this->_query = new BoolQuery(); |
||
| 38 | |||
| 39 | } |
||
| 40 | |||
| 41 | public function setIndex($index): self |
||
| 42 | { |
||
| 43 | $this->_params['index'] = $index; |
||
| 44 | return $this; |
||
| 45 | } |
||
| 46 | |||
| 47 | public function setSource($source): self |
||
| 48 | { |
||
| 49 | $this->_params['_source'] = $source; |
||
| 50 | return $this; |
||
| 51 | } |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @param $string |
||
| 55 | * @return $this |
||
| 56 | */ |
||
| 57 | public function search($string): self |
||
| 58 | { |
||
| 59 | |||
| 60 | if (is_object($string)) { |
||
| 61 | $this->_query = $string; |
||
| 62 | return $this; |
||
| 63 | } |
||
| 64 | $this->_query->must(new QueryString($string)); |
||
| 65 | return $this; |
||
| 66 | } |
||
| 67 | public function match($keywords,$fields=null):self |
||
| 68 | { |
||
| 69 | $f = "*"; |
||
| 70 | if ($fields !== null && is_string($fields)) { |
||
| 71 | $f = $fields; |
||
| 72 | } |
||
| 73 | $this->_query->must(new Match($keywords, $f)); |
||
| 74 | return $this; |
||
| 75 | } |
||
| 76 | public function phrase($string,$fields=null):self |
||
| 77 | { |
||
| 78 | $f = "*"; |
||
| 79 | if ($fields !== null && is_string($fields)) { |
||
| 80 | $f = $fields; |
||
| 81 | } |
||
| 82 | $this->_query->must(new Match($string, $f)); |
||
| 83 | return $this; |
||
| 84 | } |
||
| 85 | public function limit($limit): self |
||
| 86 | { |
||
| 87 | $this->_params['limit'] = $limit; |
||
| 88 | return $this; |
||
| 89 | } |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @param $name |
||
| 93 | * @param $exp |
||
| 94 | * @return $this |
||
| 95 | */ |
||
| 96 | public function expression($name, $exp): self |
||
| 97 | { |
||
| 98 | if (!isset($this->_params['script_fields'])) { |
||
| 99 | $this->_params['script_fields'] = new ScriptFields(); |
||
| 100 | } |
||
| 101 | $this->_params['script_fields']->add($name, $exp); |
||
| 102 | return $this; |
||
| 103 | } |
||
| 104 | |||
| 105 | public function highlight($fields = []): self |
||
| 106 | { |
||
| 107 | |||
| 108 | if (count($fields) > 0) { |
||
| 109 | $hfields = new \Manticoresearch\Query(); |
||
| 110 | foreach ($fields as $f) { |
||
| 111 | $hfields->add($f, null); |
||
| 112 | } |
||
| 113 | $this->_params['highlight'] = $hfields->toArray(); |
||
| 114 | } else { |
||
| 115 | $this->_params['highlight'] = new \stdClass(); |
||
| 116 | } |
||
| 117 | |||
| 118 | return $this; |
||
| 119 | } |
||
| 120 | |||
| 121 | public function distance($args): self |
||
| 122 | { |
||
| 123 | $this->_query->must(new Distance($args)); |
||
| 124 | return $this; |
||
| 125 | } |
||
| 126 | |||
| 127 | public function filter($attr, $op = '', $values = []): self |
||
| 128 | { |
||
| 129 | if (is_object($attr)) { |
||
| 130 | $this->_query->must($attr); |
||
| 131 | return $this; |
||
| 132 | } |
||
| 133 | if (!is_array($values)) { |
||
| 134 | $values = [$values]; |
||
| 135 | } |
||
| 136 | |||
| 137 | switch ($op) { |
||
| 138 | case 'range': |
||
| 139 | $this->_query->must(new Range($attr, [ |
||
| 140 | 'gte' => $values[0], |
||
| 141 | 'lte' => $values[1] |
||
| 142 | ])); |
||
| 143 | break; |
||
| 144 | case 'lt': |
||
| 145 | case 'lte': |
||
| 146 | case 'gt': |
||
| 147 | case 'gte': |
||
| 148 | $this->_query->must(new Range($attr, [ |
||
| 149 | $op => $values[0], |
||
| 150 | ])); |
||
| 151 | break; |
||
| 152 | case 'equals': |
||
| 153 | $this->_query->must(new Equals($attr, $values[0])); |
||
| 154 | break; |
||
| 155 | } |
||
| 156 | return $this; |
||
| 157 | } |
||
| 158 | |||
| 159 | public function orFilter($attr, $op = '', $values = []): self |
||
| 160 | { |
||
| 161 | if (is_object($attr)) { |
||
| 162 | $this->_query->should($attr); |
||
| 163 | return $this; |
||
| 164 | } |
||
| 165 | if (!is_array($values)) { |
||
| 166 | $values = [$values]; |
||
| 167 | } |
||
| 168 | switch ($op) { |
||
| 169 | case 'range': |
||
| 170 | $this->_query->should(new Range($attr, [ |
||
| 171 | 'gte' => $values[0], |
||
| 172 | 'lte' => $values[1] |
||
| 173 | ])); |
||
| 174 | break; |
||
| 175 | case 'lt': |
||
| 176 | case 'lte' : |
||
| 177 | case 'gte': |
||
| 178 | case 'gt': |
||
| 179 | $this->_query->should(new Range($attr, [ |
||
| 180 | $op => $values[0], |
||
| 181 | ])); |
||
| 182 | break; |
||
| 183 | case 'equals': |
||
| 184 | $this->_query->should(new Equals($attr, $values[0])); |
||
| 185 | break; |
||
| 186 | } |
||
| 187 | return $this; |
||
| 188 | } |
||
| 189 | |||
| 190 | public function notFilter($attr, $op = '', $values = []): self |
||
| 191 | { |
||
| 192 | if (is_object($attr)) { |
||
| 193 | $this->_query->mustNot($attr); |
||
| 194 | return $this; |
||
| 195 | } |
||
| 196 | if (!is_array($values)) { |
||
| 197 | $values = [$values]; |
||
| 198 | } |
||
| 199 | var_dump($values); |
||
|
|
|||
| 200 | switch ($op) { |
||
| 201 | case 'range': |
||
| 202 | $this->_query->mustNot(new Range($attr, [ |
||
| 203 | 'gte' => $values[0], |
||
| 204 | 'lte' => $values[1] |
||
| 205 | ])); |
||
| 206 | break; |
||
| 207 | case 'lt': |
||
| 208 | case 'lte' : |
||
| 209 | case 'gte': |
||
| 210 | case 'gt': |
||
| 211 | $this->_query->mustNot(new Range($attr, [ |
||
| 212 | $op => $values[0], |
||
| 213 | ])); |
||
| 214 | break; |
||
| 215 | case 'equals': |
||
| 216 | $this->_query->mustNot(new Equals($attr, $values[0])); |
||
| 217 | break; |
||
| 218 | } |
||
| 219 | return $this; |
||
| 220 | } |
||
| 221 | |||
| 222 | public function offset($offset): self |
||
| 223 | { |
||
| 224 | $this->_params['offset'] = $offset; |
||
| 225 | return $this; |
||
| 226 | } |
||
| 227 | |||
| 228 | public function maxMatches($maxmatches): self |
||
| 229 | { |
||
| 230 | $this->_params['max_matches'] = $maxmatches; |
||
| 231 | return $this; |
||
| 232 | } |
||
| 233 | |||
| 234 | public function sort($field, $direction = 'asc', $mode = null): self |
||
| 235 | { |
||
| 236 | // reset sorting |
||
| 237 | if ($field === false) { |
||
| 238 | $this->_params['sort'] = []; |
||
| 239 | } |
||
| 240 | //if 1st arg is array means we have a sorting expression |
||
| 241 | if (is_array($field)) { |
||
| 242 | //is 2nd arg is true we full set the sort with the expr, otherwise just add it |
||
| 243 | if (isset($direction) && $direction === true) { |
||
| 244 | $this->_params['sort'] = $field; |
||
| 245 | } else { |
||
| 246 | $this->_params['sort'] [] = $field; |
||
| 247 | } |
||
| 248 | return $this; |
||
| 249 | } |
||
| 250 | if (!isset($this->_params['sort'])) { |
||
| 251 | $this->_params['sort'] = []; |
||
| 252 | } |
||
| 253 | if ($mode === null) { |
||
| 254 | $this->_params['sort'] [] = [$field => $direction]; |
||
| 255 | } else { |
||
| 256 | $this->_params['sort'] [] = [$field => ['order' => $direction, 'mode' => $mode]]; |
||
| 257 | } |
||
| 258 | |||
| 259 | return $this; |
||
| 260 | } |
||
| 261 | |||
| 262 | public function profile(): self |
||
| 263 | { |
||
| 264 | $this->_params['profile'] = true; |
||
| 265 | return $this; |
||
| 266 | } |
||
| 267 | |||
| 268 | public function get() |
||
| 269 | { |
||
| 270 | $this->_body = $this->compile(); |
||
| 271 | return $this->_client->search(['body' => $this->_body]); |
||
| 272 | } |
||
| 273 | |||
| 274 | public function compile() |
||
| 289 | } |
||
| 290 | |||
| 291 | public function getBody() |
||
| 292 | { |
||
| 294 | } |
||
| 295 | |||
| 296 | public function reset() |
||
| 297 | { |
||
| 298 | $this->_params = []; |
||
| 299 | $this->_query = new BoolQuery(); |
||
| 300 | } |
||
| 301 | public function getClient() |
||
| 304 | } |
||
| 305 | } |