1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* class DataObjectExtension|Firesphere\ElasticSearch\Extensions\DataObjectExtension Adds checking if changes should be |
4
|
|
|
* pushed to Elastic |
5
|
|
|
* |
6
|
|
|
* @package Firesphere\Elastic\Search |
7
|
|
|
* @author Simon `Firesphere` Erkelens; Marco `Sheepy` Hermo |
8
|
|
|
* @copyright Copyright (c) 2018 - now() Firesphere & Sheepy |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Firesphere\ElasticSearch\Extensions; |
12
|
|
|
|
13
|
|
|
use Elastic\Elasticsearch\Exception\ClientResponseException; |
14
|
|
|
use Elastic\Elasticsearch\Exception\ServerResponseException; |
15
|
|
|
use Elastic\Elasticsearch\Response\Elasticsearch; |
16
|
|
|
use Exception; |
17
|
|
|
use Firesphere\ElasticSearch\Indexes\ElasticIndex; |
18
|
|
|
use Firesphere\ElasticSearch\Services\ElasticCoreService; |
19
|
|
|
use Firesphere\SearchBackend\Extensions\DataObjectSearchExtension; |
20
|
|
|
use Http\Promise\Promise; |
21
|
|
|
use Psr\Container\NotFoundExceptionInterface; |
22
|
|
|
use Psr\Log\LoggerInterface; |
23
|
|
|
use SilverStripe\CMS\Model\SiteTree; |
24
|
|
|
use SilverStripe\Core\Injector\Injector; |
25
|
|
|
use SilverStripe\ORM\ArrayList; |
26
|
|
|
use SilverStripe\ORM\DataExtension; |
27
|
|
|
use SilverStripe\ORM\DataObject; |
28
|
|
|
use SilverStripe\ORM\ValidationException; |
29
|
|
|
use SilverStripe\Versioned\Versioned; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Class \Firesphere\ElasticSearch\Extensions\DataObjectElasticExtension |
33
|
|
|
* |
34
|
|
|
* @property DataObject|DataObjectElasticExtension $owner |
35
|
|
|
*/ |
36
|
|
|
class DataObjectElasticExtension extends DataExtension |
37
|
|
|
{ |
38
|
|
|
protected $deletedFromElastic; |
39
|
|
|
/** |
40
|
|
|
* @throws NotFoundExceptionInterface |
41
|
|
|
*/ |
42
|
|
|
public function onAfterDelete() |
43
|
|
|
{ |
44
|
|
|
parent::onAfterDelete(); |
45
|
|
|
$this->deleteFromElastic(); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Can be called directly, if a DataObject needs to be removed |
50
|
|
|
* immediately. |
51
|
|
|
* @return bool|Elasticsearch|Promise |
52
|
|
|
* @throws NotFoundExceptionInterface |
53
|
|
|
*/ |
54
|
|
|
public function deleteFromElastic() |
55
|
|
|
{ |
56
|
|
|
$result = false; |
57
|
|
|
$service = new ElasticCoreService(); |
58
|
|
|
$indexes = $service->getValidIndexes(); |
59
|
|
|
foreach ($indexes as $index) { |
60
|
|
|
/** @var ElasticIndex $idx */ |
61
|
|
|
$idx = Injector::inst()->get($index); |
62
|
|
|
$config = ElasticIndex::config()->get($idx->getIndexName()); |
63
|
|
|
if (in_array($this->owner->ClassName, $config['Classes'])) { |
|
|
|
|
64
|
|
|
$deleteQuery = $this->getDeleteQuery($idx); |
65
|
|
|
$result = $this->executeQuery($service, $deleteQuery); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return $result; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param ElasticIndex $index |
74
|
|
|
* @return array |
75
|
|
|
*/ |
76
|
|
|
private function getDeleteQuery(ElasticIndex $index): array |
77
|
|
|
{ |
78
|
|
|
return [ |
79
|
|
|
'index' => $index->getIndexName(), |
80
|
|
|
'body' => [ |
81
|
|
|
'query' => [ |
82
|
|
|
'match' => [ |
83
|
|
|
'id' => sprintf('%s-%s', $this->owner->ClassName, $this->owner->ID) |
|
|
|
|
84
|
|
|
] |
85
|
|
|
] |
86
|
|
|
] |
87
|
|
|
]; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param ElasticCoreService $service |
92
|
|
|
* @param array $deleteQuery |
93
|
|
|
* @return Elasticsearch|Promise|bool |
94
|
|
|
* @throws NotFoundExceptionInterface |
95
|
|
|
*/ |
96
|
|
|
protected function executeQuery(ElasticCoreService $service, array $deleteQuery) |
97
|
|
|
{ |
98
|
|
|
try { |
99
|
|
|
return $service->getClient()->deleteByQuery($deleteQuery); |
100
|
|
|
} catch (Exception $e) { |
101
|
|
|
/** @var DataObjectSearchExtension|DataObject $owner */ |
102
|
|
|
$owner = $this->owner; |
103
|
|
|
// DirtyClass handling is a DataObject Search Core extension |
104
|
|
|
$dirty = $owner->getDirtyClass('DELETE'); |
105
|
|
|
$ids = json_decode($dirty->IDs ?? '[]'); |
106
|
|
|
$ids[] = $owner->ID; |
107
|
|
|
$dirty->IDs = json_encode($ids); |
108
|
|
|
$dirty->write(); |
109
|
|
|
/** @var LoggerInterface $logger */ |
110
|
|
|
$logger = Injector::inst()->get(LoggerInterface::class); |
111
|
|
|
$logger->error($e->getMessage(), $e->getTrace()); |
112
|
|
|
|
113
|
|
|
return false; |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Reindex after write, if it's an indexed new/updated object |
119
|
|
|
* @throws ClientResponseException |
120
|
|
|
* @throws NotFoundExceptionInterface |
121
|
|
|
* @throws ServerResponseException |
122
|
|
|
*/ |
123
|
|
|
public function onAfterWrite() |
124
|
|
|
{ |
125
|
|
|
parent::onAfterWrite(); |
126
|
|
|
/** @var DataObject|SiteTree|DataObjectElasticExtension|DataObjectSearchExtension|Versioned $owner */ |
127
|
|
|
$owner = $this->owner; |
128
|
|
|
if ( |
129
|
|
|
!$owner->hasExtension(Versioned::class) || |
|
|
|
|
130
|
|
|
($owner->hasExtension(Versioned::class) && $owner->isPublished()) |
|
|
|
|
131
|
|
|
) { |
132
|
|
|
$this->pushToElastic(); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
if ($owner->hasField('ShowInSearch') && |
|
|
|
|
136
|
|
|
$owner->isChanged('ShowInSearch') && |
|
|
|
|
137
|
|
|
!$owner->ShowInSearch) { |
|
|
|
|
138
|
|
|
$this->deletedFromElastic = $this->deleteFromElastic(); |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* This is a separate method from the delete action, as it's a different route |
144
|
|
|
* and query components. |
145
|
|
|
* It can be called to add an object to the index immediately, without |
146
|
|
|
* requiring a write. |
147
|
|
|
* @return mixed |
148
|
|
|
* @throws ClientResponseException |
149
|
|
|
* @throws NotFoundExceptionInterface |
150
|
|
|
* @throws ServerResponseException |
151
|
|
|
*/ |
152
|
|
|
public function pushToElastic() |
153
|
|
|
{ |
154
|
|
|
$result = false; |
155
|
|
|
$list = ArrayList::create(); |
156
|
|
|
$list->push($this->owner); |
157
|
|
|
/** @var ElasticCoreService $service */ |
158
|
|
|
$service = Injector::inst()->get(ElasticCoreService::class); |
159
|
|
|
foreach ($service->getValidIndexes() as $indexStr) { |
160
|
|
|
/** @var ElasticIndex $index */ |
161
|
|
|
$index = Injector::inst()->get($indexStr); |
162
|
|
|
$idxConfig = ElasticIndex::config()->get($index->getIndexName()); |
163
|
|
|
if (in_array($this->owner->ClassName, $idxConfig['Classes'])) { |
|
|
|
|
164
|
|
|
$result = $service->updateIndex($index, $list); |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
return $result; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Add ability to see what the response |
173
|
|
|
* from Elasticsearch was after a delete action. |
174
|
|
|
* |
175
|
|
|
* @return mixed |
176
|
|
|
*/ |
177
|
|
|
public function isDeletedFromElastic() |
178
|
|
|
{ |
179
|
|
|
return $this->deletedFromElastic; |
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
|