|
1
|
|
|
<?php declare(strict_types = 1); |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Created by PhpStorm. |
|
5
|
|
|
* User: gordon |
|
6
|
|
|
* Date: 24/3/2561 |
|
7
|
|
|
* Time: 20:36 น. |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace Suilven\FreeTextSearch\Extension; |
|
11
|
|
|
|
|
12
|
|
|
use SilverStripe\ORM\DataExtension; |
|
13
|
|
|
use SilverStripe\SiteConfig\SiteConfig; |
|
14
|
|
|
use Suilven\FreeTextSearch\Factory\IndexerFactory; |
|
15
|
|
|
use Suilven\FreeTextSearch\Helper\IndexingHelper; |
|
16
|
|
|
use Suilven\FreeTextSearch\QueuedJob\BulkIndexDirtyJob; |
|
17
|
|
|
use Symbiote\QueuedJobs\Services\QueuedJobService; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Class IndexingExtension |
|
21
|
|
|
* |
|
22
|
|
|
* @package Suilven\FreeTextSearch\Extension |
|
23
|
|
|
* @property bool $IsDirtyFreeTextSearch true if this DataObject needs reindexed |
|
24
|
|
|
*/ |
|
25
|
|
|
class IndexingExtension extends DataExtension |
|
26
|
|
|
{ |
|
27
|
|
|
/** @var array<string,string> */ |
|
28
|
|
|
private static $db= [ |
|
29
|
|
|
'IsDirtyFreeTextSearch' => 'Boolean', |
|
30
|
|
|
]; |
|
31
|
|
|
|
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* If we are bulk indexing mark the dataobject as dirty |
|
35
|
|
|
*/ |
|
36
|
|
|
public function onBeforeWrite(): void |
|
37
|
|
|
{ |
|
38
|
|
|
parent::onBeforeWrite(); |
|
39
|
|
|
|
|
40
|
|
|
$config = SiteConfig::current_site_config(); |
|
41
|
|
|
|
|
42
|
|
|
// @phpstan-ignore-next-line |
|
43
|
|
|
if ($config->FreeTextSearchIndexingModeInBulk === 0) { |
|
44
|
|
|
return; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
// @phpstan-ignore-next-line |
|
48
|
|
|
$this->getOwner()->IsDirtyFreeTextSearch = true; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* If bulk indexing, add a job to index dirty objects on the queue. Otherwise index immediately |
|
54
|
|
|
* |
|
55
|
|
|
* @throws \SilverStripe\ORM\ValidationException |
|
56
|
|
|
*/ |
|
57
|
|
|
public function onAfterWrite(): void |
|
58
|
|
|
{ |
|
59
|
|
|
parent::onAfterWrite(); |
|
60
|
|
|
|
|
61
|
|
|
$config = SiteConfig::current_site_config(); |
|
62
|
|
|
|
|
63
|
|
|
// a dataobject could belong to multiple indexes. Update them all |
|
64
|
|
|
$helper = new IndexingHelper(); |
|
65
|
|
|
|
|
66
|
|
|
// @phpstan-ignore-next-line |
|
67
|
|
|
$indexNames = $helper->getIndexes($this->getOwner()); |
|
68
|
|
|
|
|
69
|
|
|
// @phpstan-ignore-next-line |
|
70
|
|
|
if ($config->FreeTextSearchIndexingModeInBulk === 1) { |
|
71
|
|
|
// Add a bulk index job to the queue. |
|
72
|
|
|
// Given same parameters, in this case index name, only one queued job is created |
|
73
|
|
|
// even if multiple documents saved in between cron jobs |
|
74
|
|
|
$job = new BulkIndexDirtyJob(); |
|
75
|
|
|
|
|
76
|
|
|
foreach ($indexNames as $indexName) { |
|
77
|
|
|
$job->hydrate($indexName); |
|
78
|
|
|
QueuedJobService::singleton()->queueJob($job); |
|
79
|
|
|
} |
|
80
|
|
|
} else { |
|
81
|
|
|
// IsDirtyFreeTextSearch flag is not used sa we are indexing immediately |
|
82
|
|
|
$factory = new IndexerFactory(); |
|
83
|
|
|
$indexer = $factory->getIndexer(); |
|
84
|
|
|
foreach ($indexNames as $indexName) { |
|
85
|
|
|
$indexer->setIndexName($indexName); |
|
86
|
|
|
|
|
87
|
|
|
// @phpstan-ignore-next-line |
|
88
|
|
|
$indexer->index($this->getOwner()); |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|