Complex classes like ElasticaService 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 ElasticaService, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | class ElasticaService { |
||
| 14 | |||
| 15 | /** |
||
| 16 | * @var \Elastica\Document[] |
||
| 17 | */ |
||
| 18 | protected $buffer = array(); |
||
| 19 | |||
| 20 | |||
| 21 | /** |
||
| 22 | * @var bool controls whether indexing operations are buffered or not |
||
| 23 | */ |
||
| 24 | protected $buffered = false; |
||
| 25 | |||
| 26 | |||
| 27 | /** |
||
| 28 | * @var \Elastica\Client Elastica Client object |
||
| 29 | */ |
||
| 30 | private $client; |
||
| 31 | |||
| 32 | |||
| 33 | /** |
||
| 34 | * @var string index name |
||
| 35 | */ |
||
| 36 | private $indexName; |
||
| 37 | |||
| 38 | |||
| 39 | /** |
||
| 40 | * The code of the locale being indexed or searched |
||
| 41 | * @var string e.g. th_TH, en_US |
||
| 42 | */ |
||
| 43 | private $locale; |
||
| 44 | |||
| 45 | |||
| 46 | /** |
||
| 47 | * Mapping of DataObject ClassName and whether it is in the SiteTree or not |
||
| 48 | * @var array $site_tree_classes; |
||
| 49 | */ |
||
| 50 | private static $site_tree_classes = array(); |
||
| 51 | |||
| 52 | |||
| 53 | /** |
||
| 54 | * Counter used to for testing, records indexing requests |
||
| 55 | * @var integer |
||
| 56 | */ |
||
| 57 | public static $indexing_request_ctr = 0; |
||
| 58 | |||
| 59 | |||
| 60 | /** |
||
| 61 | * Array of highlighted fields, e.g. Title, Title.standard. If this is empty then the |
||
| 62 | * ShowHighlight field of SearchableField is used to determine which fields to highlight |
||
| 63 | * @var array |
||
| 64 | */ |
||
| 65 | private $highlightedFields = array(); |
||
| 66 | |||
| 67 | |||
| 68 | /** |
||
| 69 | * The number of documents to index currently for this locale |
||
| 70 | * @var integer The number of documents left to index |
||
| 71 | */ |
||
| 72 | private $nDocumentsToIndexForLocale = 0; |
||
| 73 | |||
| 74 | |||
| 75 | /* |
||
| 76 | Set the highlight fields for subsequent searches |
||
| 77 | */ |
||
| 78 | public function setHighlightedFields($newHighlightedFields) { |
||
| 79 | $this->highlightedFields = $newHighlightedFields; |
||
| 80 | } |
||
| 81 | |||
| 82 | |||
| 83 | /* |
||
| 84 | Enable this to allow test classes not to be ignored when indexing |
||
| 85 | */ |
||
| 86 | public $test_mode = false; |
||
| 87 | |||
| 88 | |||
| 89 | /** |
||
| 90 | * @param \Elastica\Client $client |
||
| 91 | * @param string $newIndexName Name of the new index |
||
| 92 | */ |
||
| 93 | public function __construct(Client $client, $newIndexName) { |
||
| 98 | |||
| 99 | |||
| 100 | public function setTestMode($newTestMode) { |
||
| 101 | $this->test_mode = $newTestMode; |
||
| 102 | } |
||
| 103 | |||
| 104 | |||
| 105 | /** |
||
| 106 | * @return \Elastica\Client |
||
| 107 | */ |
||
| 108 | public function getClient() { |
||
| 111 | |||
| 112 | |||
| 113 | /** |
||
| 114 | * @return \Elastica\Index |
||
| 115 | */ |
||
| 116 | public function getIndex() { |
||
| 120 | |||
| 121 | |||
| 122 | public function setLocale($newLocale) { |
||
| 125 | |||
| 126 | public function getIndexName() { |
||
| 127 | return $this->indexName; |
||
| 128 | } |
||
| 129 | |||
| 130 | private function getLocaleIndexName() { |
||
| 136 | |||
| 137 | |||
| 138 | /** |
||
| 139 | * Performs a search query and returns a result list. |
||
| 140 | * |
||
| 141 | * @param \Elastica\Query|string|array $query |
||
| 142 | * @param string|array $types List of comma separated SilverStripe classes to search, or blank for all |
||
| 143 | * @return \Elastica\ResultList |
||
| 144 | */ |
||
| 145 | public function search($query, $types = '') { |
||
| 183 | |||
| 184 | |||
| 185 | /** |
||
| 186 | * @param Query $query |
||
| 187 | */ |
||
| 188 | private function addExtractedQueryTermsForMoreLikeThis($query, &$highlights) { |
||
| 202 | |||
| 203 | |||
| 204 | /** |
||
| 205 | * @param Search $search |
||
| 206 | * @param Query $query |
||
| 207 | */ |
||
| 208 | private function addTypesToSearch(&$search, $types, $query) { |
||
| 219 | |||
| 220 | |||
| 221 | private function getHighlightingConfig() { |
||
| 260 | |||
| 261 | |||
| 262 | private function checkForTermsMoreLikeThis($elasticaQuery, $search) { |
||
| 299 | |||
| 300 | |||
| 301 | /** |
||
| 302 | * Ensure that the index is present |
||
| 303 | */ |
||
| 304 | protected function ensureIndex() { |
||
| 310 | |||
| 311 | |||
| 312 | /** |
||
| 313 | * Ensure that there is a mapping present |
||
| 314 | * |
||
| 315 | * @param \Elastica\Type Type object |
||
| 316 | * @param SilverStripe\Elastica\Searchable DataObject that implements Searchable |
||
| 317 | * @return \Elastica\Mapping Mapping object |
||
| 318 | */ |
||
| 319 | protected function ensureMapping(\Elastica\Type $type, \DataObject $record) { |
||
| 329 | |||
| 330 | |||
| 331 | /** |
||
| 332 | * Either creates or updates a record in the index. |
||
| 333 | * |
||
| 334 | * @param Searchable $record |
||
| 335 | */ |
||
| 336 | public function index($record) { |
||
| 357 | |||
| 358 | |||
| 359 | /** |
||
| 360 | * Begins a bulk indexing operation where documents are buffered rather than |
||
| 361 | * indexed immediately. |
||
| 362 | */ |
||
| 363 | public function startBulkIndex() { |
||
| 366 | |||
| 367 | |||
| 368 | public function listIndexes($trace) { |
||
| 376 | |||
| 377 | |||
| 378 | /** |
||
| 379 | * Ends the current bulk index operation and indexes the buffered documents. |
||
| 380 | */ |
||
| 381 | public function endBulkIndex() { |
||
| 410 | |||
| 411 | |||
| 412 | /** |
||
| 413 | * Deletes a record from the index. |
||
| 414 | * |
||
| 415 | * @param Searchable $record |
||
| 416 | */ |
||
| 417 | public function remove($record) { |
||
| 423 | |||
| 424 | |||
| 425 | /** |
||
| 426 | * Creates the index and the type mappings. |
||
| 427 | */ |
||
| 428 | public function define() { |
||
| 444 | |||
| 445 | |||
| 446 | /** |
||
| 447 | * Refresh an array of records in the index |
||
| 448 | * |
||
| 449 | * @param array $records |
||
| 450 | */ |
||
| 451 | protected function refreshRecords($records) { |
||
| 458 | |||
| 459 | |||
| 460 | /** |
||
| 461 | * Get a List of all records by class. Get the "Live data" If the class has the "Versioned" extension |
||
| 462 | * |
||
| 463 | * @param string $class Class Name |
||
| 464 | * @param int $pageSize Optional page size, only a max of this number of records returned |
||
| 465 | * @param int $page Page number to return |
||
| 466 | * @return \DataList $records |
||
| 467 | */ |
||
| 468 | protected function recordsByClassConsiderVersioned($class, $pageSize = 0, $page = 0) { |
||
| 487 | |||
| 488 | |||
| 489 | /** |
||
| 490 | * Refresh the records of a given class within the search index |
||
| 491 | * |
||
| 492 | * @param string $class Class Name |
||
| 493 | */ |
||
| 494 | protected function refreshClass($class) { |
||
| 508 | |||
| 509 | |||
| 510 | /** |
||
| 511 | * Re-indexes each record in the index. |
||
| 512 | */ |
||
| 513 | public function refresh() { |
||
| 554 | |||
| 555 | |||
| 556 | /** |
||
| 557 | * Reset the current index |
||
| 558 | */ |
||
| 559 | public function reset() { |
||
| 564 | |||
| 565 | |||
| 566 | private function createIndex() { |
||
| 571 | |||
| 572 | |||
| 573 | /** |
||
| 574 | * Get the index settings for the current locale |
||
| 575 | * @return IndexSettings index settings for the current locale |
||
| 576 | */ |
||
| 577 | public function getIndexSettingsForCurrentLocale() { |
||
| 589 | |||
| 590 | |||
| 591 | /** |
||
| 592 | * Gets the classes which are indexed (i.e. have the extension applied). |
||
| 593 | * |
||
| 594 | * @return array |
||
| 595 | */ |
||
| 596 | public function getIndexedClasses() { |
||
| 625 | |||
| 626 | |||
| 627 | /** |
||
| 628 | * Get the number of indexing requests made. Used for testing bulk indexing |
||
| 629 | * @return integer indexing request counter |
||
| 630 | */ |
||
| 631 | public function getIndexingRequestCtr() { |
||
| 634 | |||
| 635 | |||
| 636 | /** |
||
| 637 | * Get the term vectors in the index for the provided Searchable is_object |
||
| 638 | * @param Searchable $searchable An object that implements Searchable |
||
| 639 | * @return array array of field name to terms indexed |
||
| 640 | */ |
||
| 641 | public function getTermVectors($searchable) { |
||
| 682 | } |
||
| 683 |