1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverStripe\FullTextSearch\Solr\Tasks; |
4
|
|
|
|
5
|
|
|
use ReflectionClass; |
6
|
|
|
use SilverStripe\Core\ClassInfo; |
7
|
|
|
use SilverStripe\Core\Injector\Injector; |
8
|
|
|
use SilverStripe\Dev\Debug; |
9
|
|
|
use SilverStripe\FullTextSearch\Search\Variants\SearchVariant; |
10
|
|
|
use SilverStripe\ORM\DataList; |
11
|
|
|
use SilverStripe\FullTextSearch\Solr\Reindex\Handlers\SolrReindexHandler; |
12
|
|
|
use SilverStripe\FullTextSearch\Solr\SolrIndex; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Task used for both initiating a new reindex, as well as for processing incremental batches |
16
|
|
|
* within a reindex. |
17
|
|
|
* |
18
|
|
|
* When running a complete reindex you can provide any of the following |
19
|
|
|
* - class (to limit to a single class) |
20
|
|
|
* - verbose (optional) |
21
|
|
|
* |
22
|
|
|
* When running with a single batch, provide the following querystring arguments: |
23
|
|
|
* - index |
24
|
|
|
* - class |
25
|
|
|
* - variantstate |
26
|
|
|
* - verbose (optional) |
27
|
|
|
*/ |
28
|
|
|
class Solr_Reindex extends Solr_BuildTask |
29
|
|
|
{ |
30
|
|
|
private static $segment = 'Solr_Reindex'; |
|
|
|
|
31
|
|
|
|
32
|
|
|
protected $enabled = true; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Number of records to load and index per request |
36
|
|
|
* |
37
|
|
|
* @var int |
38
|
|
|
* @config |
39
|
|
|
*/ |
40
|
|
|
private static $recordsPerRequest = 200; |
|
|
|
|
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Get the reindex handler |
44
|
|
|
* |
45
|
|
|
* @return SolrReindexHandler |
46
|
|
|
*/ |
47
|
|
|
protected function getHandler() |
48
|
|
|
{ |
49
|
|
|
return Injector::inst()->get(SolrReindexHandler::class); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param SS_HTTPRequest $request |
54
|
|
|
*/ |
55
|
|
|
public function run($request) |
56
|
|
|
{ |
57
|
|
|
parent::run($request); |
58
|
|
|
|
59
|
|
|
// Reset state |
60
|
|
|
$originalState = SearchVariant::current_state(); |
61
|
|
|
$this->doReindex($request); |
62
|
|
|
SearchVariant::activate_state($originalState); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param SS_HTTPRequest $request |
67
|
|
|
*/ |
68
|
|
|
protected function doReindex($request) |
69
|
|
|
{ |
70
|
|
|
$class = $request->getVar('class'); |
71
|
|
|
|
72
|
|
|
$index = $request->getVar('index'); |
73
|
|
|
|
74
|
|
|
//find the index classname by IndexName |
75
|
|
|
// this is for when index names do not match the class name (this can be done by overloading getIndexName() on |
76
|
|
|
// indexes |
77
|
|
|
if ($index && !ClassInfo::exists($index)) { |
78
|
|
|
foreach (ClassInfo::subclassesFor(SolrIndex::class) as $solrIndexClass) { |
79
|
|
|
$reflection = new ReflectionClass($solrIndexClass); |
80
|
|
|
//skip over abstract classes |
81
|
|
|
if (!$reflection->isInstantiable()) { |
82
|
|
|
continue; |
83
|
|
|
} |
84
|
|
|
//check the indexname matches the index passed to the request |
85
|
|
|
if (!strcasecmp(singleton($solrIndexClass)->getIndexName(), $index)) { |
86
|
|
|
//if we match, set the correct index name and move on |
87
|
|
|
$index = $solrIndexClass; |
88
|
|
|
break; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
// Check if we are re-indexing a single group |
94
|
|
|
// If not using queuedjobs, we need to invoke Solr_Reindex as a separate process |
95
|
|
|
// Otherwise each group is processed via a SolrReindexGroupJob |
96
|
|
|
$groups = $request->getVar('groups'); |
97
|
|
|
|
98
|
|
|
$handler = $this->getHandler(); |
99
|
|
|
if ($groups) { |
100
|
|
|
// Run grouped batches (id % groups = group) |
101
|
|
|
$group = $request->getVar('group'); |
102
|
|
|
$indexInstance = singleton($index); |
103
|
|
|
$state = json_decode($request->getVar('variantstate'), true); |
104
|
|
|
|
105
|
|
|
$handler->runGroup($this->getLogger(), $indexInstance, $state, $class, $groups, $group); |
106
|
|
|
return; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
// If run at the top level, delegate to appropriate handler |
110
|
|
|
$taskName = $this->config()->segment ?: get_class($this); |
111
|
|
|
$handler->triggerReindex($this->getLogger(), $this->config()->recordsPerRequest, $taskName, $class); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|