| Total Complexity | 53 |
| Total Lines | 357 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Index 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 Index, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | class Index |
||
| 18 | { |
||
| 19 | protected $client; |
||
| 20 | protected $index; |
||
| 21 | protected $cluster; |
||
| 22 | |||
| 23 | public function __construct(Client $client, $index = null) |
||
| 24 | { |
||
| 25 | $this->client = $client; |
||
| 26 | |||
| 27 | $this->index = $index; |
||
| 28 | |||
| 29 | $this->cluster = null; |
||
| 30 | } |
||
| 31 | |||
| 32 | public function search($input): Search |
||
| 33 | { |
||
| 34 | $search = new Search($this->client); |
||
| 35 | $search->setIndex($this->index); |
||
| 36 | return $search->search($input); |
||
| 37 | } |
||
| 38 | |||
| 39 | public function getDocumentById($id) |
||
| 40 | { |
||
| 41 | $params = [ |
||
| 42 | 'body' => [ |
||
| 43 | 'index' => $this->index, |
||
| 44 | 'query' => [ |
||
| 45 | 'equals' => ['id' => $id] |
||
| 46 | ] |
||
| 47 | ] |
||
| 48 | ]; |
||
| 49 | $result = new ResultSet($this->client->search($params, true)); |
||
| 50 | return $result->valid() ? $result->current() : null; |
||
| 51 | } |
||
| 52 | |||
| 53 | public function addDocument($data, $id = null) |
||
| 54 | { |
||
| 55 | $params = [ |
||
| 56 | 'body' => [ |
||
| 57 | 'index' => $this->index, |
||
| 58 | 'id' => $id, |
||
| 59 | 'doc' => $data |
||
| 60 | ] |
||
| 61 | ]; |
||
| 62 | if ($this->cluster !== null) { |
||
| 63 | $params['body']['cluster'] = $this->cluster; |
||
| 64 | } |
||
| 65 | return $this->client->insert($params); |
||
| 66 | } |
||
| 67 | |||
| 68 | public function addDocuments($documents) |
||
| 69 | { |
||
| 70 | $toinsert = []; |
||
| 71 | foreach ($documents as $document) { |
||
| 72 | $id = $document['id']; |
||
| 73 | unset($document['id']); |
||
| 74 | $insert = [ |
||
| 75 | 'index' => $this->index, |
||
| 76 | 'id' => $id, |
||
| 77 | 'doc' => $document |
||
| 78 | ]; |
||
| 79 | if ($this->cluster !== null) { |
||
| 80 | $insert['cluster'] = $this->cluster; |
||
| 81 | } |
||
| 82 | $toinsert[] = ['insert' => $insert]; |
||
| 83 | } |
||
| 84 | return $this->client->bulk(['body' => $toinsert]); |
||
| 85 | } |
||
| 86 | |||
| 87 | public function deleteDocument($id) |
||
| 88 | { |
||
| 89 | $params = [ |
||
| 90 | 'body' => [ |
||
| 91 | 'index' => $this->index, |
||
| 92 | 'id' => $id |
||
| 93 | ] |
||
| 94 | ]; |
||
| 95 | if ($this->cluster !== null) { |
||
| 96 | $params['body']['cluster'] = $this->cluster; |
||
| 97 | } |
||
| 98 | return $this->client->delete($params); |
||
| 99 | } |
||
| 100 | |||
| 101 | public function deleteDocuments($query) |
||
| 102 | { |
||
| 103 | if ($query instanceof Query) { |
||
| 104 | $query = $query->toArray(); |
||
| 105 | } |
||
| 106 | $params = [ |
||
| 107 | 'body' => [ |
||
| 108 | 'index' => $this->index, |
||
| 109 | 'query' => $query |
||
| 110 | ] |
||
| 111 | ]; |
||
| 112 | if ($this->cluster !== null) { |
||
| 113 | $params['body']['cluster'] = $this->cluster; |
||
| 114 | } |
||
| 115 | return $this->client->delete($params); |
||
| 116 | } |
||
| 117 | |||
| 118 | public function updateDocument($data, $id) |
||
| 119 | { |
||
| 120 | $params = [ |
||
| 121 | 'body' => [ |
||
| 122 | 'index' => $this->index, |
||
| 123 | 'id' => $id, |
||
| 124 | 'doc' => $data |
||
| 125 | ] |
||
| 126 | ]; |
||
| 127 | if ($this->cluster !== null) { |
||
| 128 | $params['body']['cluster'] = $this->cluster; |
||
| 129 | } |
||
| 130 | return $this->client->update($params); |
||
| 131 | } |
||
| 132 | |||
| 133 | public function updateDocuments($data, $query) |
||
| 134 | { |
||
| 135 | if ($query instanceof Query) { |
||
| 136 | $query = $query->toArray(); |
||
| 137 | } |
||
| 138 | $params = [ |
||
| 139 | 'body' => [ |
||
| 140 | 'index' => $this->index, |
||
| 141 | 'query' => $query, |
||
| 142 | 'doc' => $data |
||
| 143 | ] |
||
| 144 | ]; |
||
| 145 | if ($this->cluster !== null) { |
||
| 146 | $params['body']['cluster'] = $this->cluster; |
||
| 147 | } |
||
| 148 | return $this->client->update($params); |
||
| 149 | } |
||
| 150 | |||
| 151 | public function replaceDocument($data, $id) |
||
| 152 | { |
||
| 153 | $params = [ |
||
| 154 | 'body' => [ |
||
| 155 | 'index' => $this->index, |
||
| 156 | 'id' => $id, |
||
| 157 | 'doc' => $data |
||
| 158 | ] |
||
| 159 | ]; |
||
| 160 | if ($this->cluster !== null) { |
||
| 161 | $params['body']['cluster'] = $this->cluster; |
||
| 162 | } |
||
| 163 | return $this->client->replace($params); |
||
| 164 | } |
||
| 165 | |||
| 166 | public function replaceDocuments($documents) |
||
| 167 | { |
||
| 168 | $toreplace = []; |
||
| 169 | foreach ($documents as $document) { |
||
| 170 | $id = $document['id']; |
||
| 171 | unset($document['id']); |
||
| 172 | $replace = [ |
||
| 173 | 'index' => $this->index, |
||
| 174 | 'id' => $id, |
||
| 175 | 'doc' => $document |
||
| 176 | |||
| 177 | ]; |
||
| 178 | if ($this->cluster !== null) { |
||
| 179 | $replace['cluster'] = $this->cluster; |
||
| 180 | } |
||
| 181 | $toreplace[] = ['replace' => $replace]; |
||
| 182 | } |
||
| 183 | return $this->client->bulk(['body' => $toreplace]); |
||
| 184 | } |
||
| 185 | |||
| 186 | public function create($fields, $settings = [], $silent = false) |
||
| 187 | { |
||
| 188 | $params = [ |
||
| 189 | 'index' => $this->index, |
||
| 190 | 'body' => [ |
||
| 191 | 'columns' => $fields, |
||
| 192 | 'settings' => $settings |
||
| 193 | ] |
||
| 194 | ]; |
||
| 195 | if ($silent === true) { |
||
| 196 | $params['body']['silent'] = true; |
||
| 197 | } |
||
| 198 | return $this->client->indices()->create($params); |
||
| 199 | } |
||
| 200 | |||
| 201 | public function drop($silent = false) |
||
| 202 | { |
||
| 203 | $params = [ |
||
| 204 | 'index' => $this->index, |
||
| 205 | ]; |
||
| 206 | if ($silent === true) { |
||
| 207 | $params['body'] = ['silent' => true]; |
||
| 208 | } |
||
| 209 | return $this->client->indices()->drop($params); |
||
| 210 | } |
||
| 211 | |||
| 212 | public function describe() |
||
| 213 | { |
||
| 214 | $params = [ |
||
| 215 | 'index' => $this->index, |
||
| 216 | ]; |
||
| 217 | return $this->client->indices()->describe($params); |
||
| 218 | } |
||
| 219 | |||
| 220 | public function status() |
||
| 221 | { |
||
| 222 | $params = [ |
||
| 223 | 'index' => $this->index, |
||
| 224 | ]; |
||
| 225 | return $this->client->indices()->status($params); |
||
| 226 | } |
||
| 227 | |||
| 228 | public function truncate() |
||
| 234 | } |
||
| 235 | |||
| 236 | public function optimize($sync = false) |
||
| 245 | } |
||
| 246 | |||
| 247 | public function flush() |
||
| 248 | { |
||
| 249 | $params = [ |
||
| 250 | 'index' => $this->index, |
||
| 251 | ]; |
||
| 252 | $this->client->indices()->flushrtindex($params); |
||
| 253 | } |
||
| 254 | |||
| 255 | public function flushramchunk() |
||
| 256 | { |
||
| 257 | $params = [ |
||
| 258 | 'index' => $this->index, |
||
| 259 | ]; |
||
| 260 | $this->client->indices()->flushramchunk($params); |
||
| 261 | } |
||
| 262 | |||
| 263 | public function alter($operation, $name, $type = null) |
||
| 264 | { |
||
| 265 | if ($operation === 'add') { |
||
| 266 | $params = [ |
||
| 267 | 'index' => $this->index, |
||
| 268 | 'body' => [ |
||
| 269 | 'operation' => 'add', |
||
| 270 | 'column' => ['name' => $name, 'type' => $type] |
||
| 271 | ] |
||
| 272 | ]; |
||
| 273 | } elseif ($operation === 'drop') { |
||
| 274 | $params = [ |
||
| 275 | 'index' => $this->index, |
||
| 276 | 'body' => [ |
||
| 277 | 'operation' => 'drop', |
||
| 278 | 'column' => ['name' => $name] |
||
| 279 | ] |
||
| 280 | ]; |
||
| 281 | } else { |
||
| 282 | throw new RuntimeException('Alter operation not recognized'); |
||
| 283 | } |
||
| 284 | return $this->client->indices()->alter($params); |
||
| 285 | } |
||
| 286 | |||
| 287 | public function keywords($query, $options) |
||
| 288 | { |
||
| 289 | $params = [ |
||
| 290 | 'index' => $this->index, |
||
| 291 | 'body' => [ |
||
| 292 | 'query' => $query, |
||
| 293 | 'options' => $options |
||
| 294 | ] |
||
| 295 | ]; |
||
| 296 | return $this->client->keywords($params); |
||
| 297 | } |
||
| 298 | |||
| 299 | public function suggest($query, $options) |
||
| 300 | { |
||
| 301 | $params = [ |
||
| 302 | 'index' => $this->index, |
||
| 303 | 'body' => [ |
||
| 304 | 'query' => $query, |
||
| 305 | 'options' => $options |
||
| 306 | ] |
||
| 307 | ]; |
||
| 308 | return $this->client->suggest($params); |
||
| 309 | } |
||
| 310 | |||
| 311 | public function explainQuery($query) |
||
| 312 | { |
||
| 313 | $params = [ |
||
| 314 | 'index' => $this->index, |
||
| 315 | 'body' => [ |
||
| 316 | 'query' => $query, |
||
| 317 | ] |
||
| 318 | ]; |
||
| 319 | return $this->client->explainQuery($params); |
||
| 320 | } |
||
| 321 | |||
| 322 | |||
| 323 | public function percolate($docs) |
||
| 336 | } |
||
| 337 | |||
| 338 | public function percolateToDocs($docs) |
||
| 339 | { |
||
| 340 | $params = ['index' => $this->index, 'body' => []]; |
||
| 341 | if ($docs instanceof Percolate) { |
||
| 342 | $params['body']['query'] = $docs->toArray(); |
||
| 343 | } else { |
||
| 344 | if (isset($docs[0]) && is_array($docs[0])) { |
||
| 345 | $params['body']['query'] = ['percolate' => ['documents' => $docs]]; |
||
| 346 | } else { |
||
| 347 | $params['body']['query'] = ['percolate' => ['document' => $docs]]; |
||
| 348 | } |
||
| 349 | } |
||
| 350 | return new Results\PercolateDocsResultSet($this->client->pq()->search($params, true), $docs); |
||
| 351 | } |
||
| 352 | |||
| 353 | |||
| 354 | public function getClient(): Client |
||
| 357 | } |
||
| 358 | |||
| 359 | public function getName(): string |
||
| 360 | { |
||
| 361 | return $this->index; |
||
| 362 | } |
||
| 363 | |||
| 364 | public function setName($index): self |
||
| 368 | } |
||
| 369 | |||
| 370 | public function setCluster($cluster): self |
||
| 371 | { |
||
| 372 | $this->cluster = $cluster; |
||
| 373 | return $this; |
||
| 374 | } |
||
| 375 | |||
| 376 | } |
||
| 377 |