Completed
Push — dev2 ( f6a7ae...88484c )
by Gordon
07:43
created

ReindexTask   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 81.25%

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 7
lcom 1
cbo 5
dl 0
loc 66
ccs 26
cts 32
cp 0.8125
rs 10
c 1
b 1
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
B run() 0 43 6
1
<?php
2
3
namespace SilverStripe\Elastica;
4
5
/**
6
 * Defines and refreshes the elastic search index.
7
 */
8
class ReindexTask extends \BuildTask {
9
10
	protected $title = 'Elastic Search Reindex';
11
12
	protected $description = 'Refreshes the elastic search index';
13
14
	/**
15
	 * @var ElasticaService
16
	 */
17
	private $service;
18
19 1
	public function __construct(ElasticaService $service) {
20 1
		$this->service = $service;
21 1
	}
22
23
	/*
24
	NOTE: #FIXME
25
	Use index aliasing to avoid downtime whilst re-indexing
26
	https://www.elastic.co/guide/en/elasticsearch/guide/current/index-aliases.html
27
	*/
28
29 1
	public function run($request) {
30 1
		$message = ElasticaUtil::getPrinter();
31
32 1
		$locales = array();
33 1
		if(class_exists('Translatable') && singleton('SiteTree')->hasExtension('Translatable')) {
34
			foreach (\Translatable::get_existing_content_languages('SiteTree') as $code => $val) {
35
				array_push($locales, $code);
36
			}
37
		} else {
38 1
			array_push($locales, \i18n::default_locale());
39
		}
40
41
		// now iterate all the locales indexing each locale in turn using it's owner index settings
42 1
		foreach ($locales as $locale) {
43 1
			Searchable::$index_ctr = 0;
44 1
			$message('Indexing locale '.$locale);
45
46 1
			if (class_exists('Translatable')) {
47
				\Translatable::set_current_locale($locale);
48
			}
49
50 1
			$this->service->setLocale($locale);
51
52
53 1
			$message("Bulk indexing locale $locale\n");
54 1
			$this->service->define();
55
56
			// only measure index time
57 1
			$startTime = microtime(true);
58
59 1
			$message('Refreshing the index');
60 1
			$this->service->refresh();
61
			// display indexing speed stats
62 1
			$endTime = microtime(true);
63 1
			$elapsed = $endTime-$startTime;
64 1
			$perSecond = Searchable::$index_ctr / $elapsed;
65 1
			$info = "\nReindexing $locale completed \n ".Searchable::$index_ctr." docs in ".round($elapsed,2)." seconds ";
66 1
			$info .= "at ".round($perSecond,2)." documents per second\n\n";
67 1
			$message($info);
68 1
		}
69
70
71 1
	}
72
73
}
74