|
1
|
|
|
<?php |
|
2
|
|
|
namespace SilverStripe\FullTextSearch\Solr\Tasks; |
|
3
|
|
|
|
|
4
|
|
|
use ReflectionClass; |
|
5
|
|
|
use SilverStripe\Core\ClassInfo; |
|
6
|
|
|
use SilverStripe\Core\Injector\Injector; |
|
7
|
|
|
use SilverStripe\Dev\Debug; |
|
8
|
|
|
use SilverStripe\FullTextSearch\Search\Variants\SearchVariant; |
|
9
|
|
|
use SilverStripe\ORM\DataList; |
|
10
|
|
|
use SilverStripe\FullTextSearch\Solr\Reindex\Handlers\SolrReindexHandler; |
|
11
|
|
|
use SilverStripe\FullTextSearch\Solr\SolrIndex; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Task used for both initiating a new reindex, as well as for processing incremental batches |
|
15
|
|
|
* within a reindex. |
|
16
|
|
|
* |
|
17
|
|
|
* When running a complete reindex you can provide any of the following |
|
18
|
|
|
* - class (to limit to a single class) |
|
19
|
|
|
* - verbose (optional) |
|
20
|
|
|
* |
|
21
|
|
|
* When running with a single batch, provide the following querystring arguments: |
|
22
|
|
|
* - start |
|
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
|
|
|
// Deprecated reindex mechanism |
|
94
|
|
|
$start = $request->getVar('start'); |
|
95
|
|
|
if ($start !== null) { |
|
96
|
|
|
// Run single batch directly |
|
97
|
|
|
$indexInstance = singleton($index); |
|
98
|
|
|
$state = json_decode($request->getVar('variantstate'), true); |
|
99
|
|
|
$this->runFrom($indexInstance, $class, $start, $state); |
|
|
|
|
|
|
100
|
|
|
return; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
// Check if we are re-indexing a single group |
|
104
|
|
|
// If not using queuedjobs, we need to invoke Solr_Reindex as a separate process |
|
105
|
|
|
// Otherwise each group is processed via a SolrReindexGroupJob |
|
106
|
|
|
$groups = $request->getVar('groups'); |
|
107
|
|
|
|
|
108
|
|
|
$handler = $this->getHandler(); |
|
109
|
|
|
if ($groups) { |
|
110
|
|
|
// Run grouped batches (id % groups = group) |
|
111
|
|
|
$group = $request->getVar('group'); |
|
112
|
|
|
$indexInstance = singleton($index); |
|
113
|
|
|
$state = json_decode($request->getVar('variantstate'), true); |
|
114
|
|
|
|
|
115
|
|
|
$handler->runGroup($this->getLogger(), $indexInstance, $state, $class, $groups, $group); |
|
116
|
|
|
return; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
// If run at the top level, delegate to appropriate handler |
|
120
|
|
|
$taskName = $this->config()->segment ?: get_class($this); |
|
121
|
|
|
$handler->triggerReindex($this->getLogger(), $this->config()->recordsPerRequest, $taskName, $class); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
} |
|
125
|
|
|
|