-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathNodeIndexer.php
450 lines (388 loc) · 16.6 KB
/
NodeIndexer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
<?php
namespace Flowpack\ElasticSearch\ContentRepositoryAdaptor\Indexer;
/*
* This file is part of the Flowpack.ElasticSearch.ContentRepositoryAdaptor package.
*
* (c) Contributors of the Neos Project - www.neos.io
*
* This package is Open Source Software. For the full copyright and license
* information, please view the LICENSE file which was distributed with this
* source code.
*/
use Flowpack\ElasticSearch\ContentRepositoryAdaptor\Driver\DocumentDriverInterface;
use Flowpack\ElasticSearch\ContentRepositoryAdaptor\Driver\IndexDriverInterface;
use Flowpack\ElasticSearch\ContentRepositoryAdaptor\Driver\IndexerDriverInterface;
use Flowpack\ElasticSearch\ContentRepositoryAdaptor\Driver\RequestDriverInterface;
use Flowpack\ElasticSearch\ContentRepositoryAdaptor\Driver\SystemDriverInterface;
use Flowpack\ElasticSearch\ContentRepositoryAdaptor\ElasticSearchClient;
use Flowpack\ElasticSearch\ContentRepositoryAdaptor\Exception;
use Flowpack\ElasticSearch\ContentRepositoryAdaptor\Mapping\NodeTypeMappingBuilder;
use Flowpack\ElasticSearch\Domain\Model\Document as ElasticSearchDocument;
use Flowpack\ElasticSearch\Domain\Model\Index;
use Flowpack\ElasticSearch\Transfer\Exception\ApiException;
use Neos\Flow\Annotations as Flow;
use Neos\ContentRepository\Domain\Model\NodeInterface;
use Neos\ContentRepository\Domain\Service\ContentDimensionCombinator;
use Neos\ContentRepository\Domain\Service\Context;
use Neos\ContentRepository\Domain\Service\ContextFactory;
use Neos\ContentRepository\Search\Indexer\AbstractNodeIndexer;
use Neos\ContentRepository\Search\Indexer\BulkNodeIndexerInterface;
/**
* Indexer for Content Repository Nodes. Triggered from the NodeIndexingManager.
*
* Internally, uses a bulk request.
*
* @Flow\Scope("singleton")
*/
class NodeIndexer extends AbstractNodeIndexer implements BulkNodeIndexerInterface
{
/**
* Optional postfix for the index, e.g. to have different indexes by timestamp.
*
* @var string
*/
protected $indexNamePostfix = '';
/**
* @Flow\Inject
* @var ElasticSearchClient
*/
protected $searchClient;
/**
* @Flow\Inject
* @var \Flowpack\ElasticSearch\ContentRepositoryAdaptor\LoggerInterface
*/
protected $logger;
/**
* @Flow\Inject
* @var ContentDimensionCombinator
*/
protected $contentDimensionCombinator;
/**
* @Flow\Inject
* @var ContextFactory
*/
protected $contextFactory;
/**
* @var DocumentDriverInterface
* @Flow\Inject
*/
protected $documentDriver;
/**
* @var IndexerDriverInterface
* @Flow\Inject
*/
protected $indexerDriver;
/**
* @var IndexDriverInterface
* @Flow\Inject
*/
protected $indexDriver;
/**
* @var RequestDriverInterface
* @Flow\Inject
*/
protected $requestDriver;
/**
* @var SystemDriverInterface
* @Flow\Inject
*/
protected $systemDriver;
/**
* The current ElasticSearch bulk request, in the format required by http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/docs-bulk.html
*
* @var array
*/
protected $currentBulkRequest = [];
/**
* @var boolean
*/
protected $bulkProcessing = false;
/**
* Returns the index name to be used for indexing, with optional indexNamePostfix appended.
*
* @return string
*/
public function getIndexName()
{
$indexName = $this->searchClient->getIndexName();
if (strlen($this->indexNamePostfix) > 0) {
$indexName .= '-' . $this->indexNamePostfix;
}
return $indexName;
}
/**
* Set the postfix for the index name
*
* @param string $indexNamePostfix
* @return void
*/
public function setIndexNamePostfix($indexNamePostfix)
{
$this->indexNamePostfix = $indexNamePostfix;
}
/**
* Return the currently active index to be used for indexing
*
* @return Index
*/
public function getIndex()
{
$index = $this->searchClient->findIndex($this->getIndexName());
$index->setSettingsKey($this->searchClient->getIndexName());
return $index;
}
/**
* Index this node, and add it to the current bulk request.
*
* @param NodeInterface $node
* @param string $targetWorkspaceName In case this is triggered during publishing, a workspace name will be passed in
* @return void
* @throws \Neos\ContentRepository\Search\Exception\IndexingException
*/
public function indexNode(NodeInterface $node, $targetWorkspaceName = null)
{
$indexer = function (NodeInterface $node, $targetWorkspaceName = null) {
$contextPath = $node->getContextPath();
if ($this->settings['indexAllWorkspaces'] === false) {
// we are only supposed to index the live workspace.
// We need to check the workspace at two occasions; checking the
// $targetWorkspaceName and the workspace name of the node's context as fallback
if ($targetWorkspaceName !== null && $targetWorkspaceName !== 'live') {
return;
}
if ($targetWorkspaceName === null && $node->getContext()->getWorkspaceName() !== 'live') {
return;
}
}
if ($targetWorkspaceName !== null) {
$contextPath = str_replace($node->getContext()->getWorkspace()->getName(), $targetWorkspaceName, $contextPath);
}
$documentIdentifier = $this->calculateDocumentIdentifier($node, $targetWorkspaceName);
$nodeType = $node->getNodeType();
$mappingType = $this->getIndex()->findType(NodeTypeMappingBuilder::convertNodeTypeNameToMappingName($nodeType));
if ($this->bulkProcessing === false) {
// Remove document with the same contextPathHash but different NodeType, required after NodeType change
$this->logger->log(sprintf('NodeIndexer (%s): Search and remove duplicate document for node %s (%s) if needed.', $documentIdentifier, $contextPath, $node->getIdentifier()), LOG_DEBUG, null, 'ElasticSearch (CR)');
$this->documentDriver->deleteDuplicateDocumentNotMatchingType($this->getIndex(), $documentIdentifier, $node->getNodeType());
}
$fulltextIndexOfNode = [];
$nodePropertiesToBeStoredInIndex = $this->extractPropertiesAndFulltext($node, $fulltextIndexOfNode, function ($propertyName) use ($documentIdentifier, $node) {
$this->logger->log(sprintf('NodeIndexer (%s) - Property "%s" not indexed because no configuration found, node type %s.', $documentIdentifier, $propertyName, $node->getNodeType()->getName()), LOG_DEBUG, null, 'ElasticSearch (CR)');
});
$document = new ElasticSearchDocument($mappingType,
$nodePropertiesToBeStoredInIndex,
$documentIdentifier
);
$documentData = $document->getData();
if ($targetWorkspaceName !== null) {
$documentData['__workspace'] = $targetWorkspaceName;
}
$dimensionCombinations = $node->getContext()->getDimensions();
if (is_array($dimensionCombinations)) {
$documentData['__dimensionCombinations'] = $dimensionCombinations;
$documentData['__dimensionCombinationHash'] = md5(json_encode($dimensionCombinations));
}
if ($this->isFulltextEnabled($node)) {
$this->currentBulkRequest[] = $this->indexerDriver->document($this->getIndexName(), $node, $document, $documentData, $fulltextIndexOfNode, $targetWorkspaceName);
$this->currentBulkRequest[] = $this->indexerDriver->fulltext($node, $fulltextIndexOfNode, $targetWorkspaceName);
}
$this->logger->log(sprintf('NodeIndexer (%s): Indexed node %s.', $documentIdentifier, $contextPath), LOG_DEBUG, null, 'ElasticSearch (CR)');
};
$handleNode = function (NodeInterface $node, Context $context) use ($targetWorkspaceName, $indexer) {
$nodeFromContext = $context->getNodeByIdentifier($node->getIdentifier());
if ($nodeFromContext instanceof NodeInterface) {
$indexer($nodeFromContext, $targetWorkspaceName);
} else {
$documentIdentifier = $this->calculateDocumentIdentifier($node, $targetWorkspaceName);
if ($node->isRemoved()) {
$this->removeNode($node, $context->getWorkspaceName());
$this->logger->log(sprintf('NodeIndexer (%s): Removed node with identifier %s, no longer in workspace %s', $documentIdentifier, $node->getIdentifier(), $context->getWorkspaceName()), LOG_DEBUG, null, 'ElasticSearch (CR)');
} else {
$this->logger->log(sprintf('NodeIndexer (%s): Could not index node with identifier %s, not found in workspace %s', $documentIdentifier, $node->getIdentifier(), $context->getWorkspaceName()), LOG_DEBUG, null, 'ElasticSearch (CR)');
}
}
};
$workspaceName = $targetWorkspaceName ?: $node->getContext()->getWorkspaceName();
$dimensionCombinations = $this->contentDimensionCombinator->getAllAllowedCombinations();
if ($dimensionCombinations !== []) {
foreach ($dimensionCombinations as $combination) {
$context = $this->contextFactory->create(['workspaceName' => $workspaceName, 'dimensions' => $combination, 'invisibleContentShown' => true]);
$handleNode($node, $context);
}
} else {
$context = $this->contextFactory->create(['workspaceName' => $workspaceName, 'invisibleContentShown' => true]);
$handleNode($node, $context);
}
}
/**
* Returns a stable identifier for the Elasticsearch document representing the node
*
* @param NodeInterface $node
* @param string $targetWorkspaceName
* @return string
*/
protected function calculateDocumentIdentifier(NodeInterface $node, $targetWorkspaceName = null)
{
$contextPath = $node->getContextPath();
if ($targetWorkspaceName !== null) {
$contextPath = str_replace($node->getContext()->getWorkspace()->getName(), $targetWorkspaceName, $contextPath);
}
return sha1($contextPath);
}
/**
* Schedule node removal into the current bulk request.
*
* @param NodeInterface $node
* @param string $targetWorkspaceName
* @return void
*/
public function removeNode(NodeInterface $node, $targetWorkspaceName = null)
{
if ($this->settings['indexAllWorkspaces'] === false) {
// we are only supposed to index the live workspace.
// We need to check the workspace at two occasions; checking the
// $targetWorkspaceName and the workspace name of the node's context as fallback
if ($targetWorkspaceName !== null && $targetWorkspaceName !== 'live') {
return;
}
if ($targetWorkspaceName === null && $node->getContext()->getWorkspaceName() !== 'live') {
return;
}
}
$documentIdentifier = $this->calculateDocumentIdentifier($node, $targetWorkspaceName);
$this->currentBulkRequest[] = $this->documentDriver->delete($node, $documentIdentifier);
$this->currentBulkRequest[] = $this->indexerDriver->fulltext($node, [], $targetWorkspaceName);
$this->logger->log(sprintf('NodeIndexer (%s): Removed node %s (%s) from index.', $documentIdentifier, $node->getContextPath(), $node->getIdentifier()), LOG_DEBUG, null, 'ElasticSearch (CR)');
}
/**
* Perform the current bulk request
*
* @return void
*/
public function flush()
{
$bulkRequest = array_filter($this->currentBulkRequest);
if (count($bulkRequest) === 0) {
return;
}
$content = '';
foreach ($bulkRequest as $bulkRequestTuple) {
$tupleAsJson = '';
foreach ($bulkRequestTuple as $bulkRequestItem) {
$itemAsJson = json_encode($bulkRequestItem);
if ($itemAsJson === false) {
$this->logger->log('NodeIndexer: Bulk request item could not be encoded as JSON - ' . json_last_error_msg(), LOG_ERR, $bulkRequestItem, 'ElasticSearch (CR)');
continue 2;
}
$tupleAsJson .= $itemAsJson . chr(10);
}
$content .= $tupleAsJson;
}
if ($content !== '') {
$response = $this->requestDriver->bulk($this->getIndex(), $content);
foreach ($response as $responseLine) {
if (isset($response['errors']) && $response['errors'] !== false) {
$this->logger->log('NodeIndexer: ' . json_encode($responseLine), LOG_ERR, null, 'ElasticSearch (CR)');
}
}
}
$this->currentBulkRequest = [];
}
/**
* Update the index alias
*
* @return void
* @throws Exception
* @throws ApiException
* @throws \Exception
*/
public function updateIndexAlias()
{
$aliasName = $this->searchClient->getIndexName(); // The alias name is the unprefixed index name
if ($this->getIndexName() === $aliasName) {
throw new Exception('UpdateIndexAlias is only allowed to be called when $this->setIndexNamePostfix has been created.', 1383649061);
}
if (!$this->getIndex()->exists()) {
throw new Exception('The target index for updateIndexAlias does not exist. This shall never happen.', 1383649125);
}
$aliasActions = [];
try {
$indexNames = $this->indexDriver->indexesByAlias($aliasName);
if ($indexNames === []) {
// if there is an actual index with the name we want to use as alias, remove it now
$this->indexDriver->deleteIndex($aliasName);
} else {
foreach ($indexNames as $indexName) {
$aliasActions[] = [
'remove' => [
'index' => $indexName,
'alias' => $aliasName
]
];
}
}
} catch (ApiException $exception) {
// in case of 404, do not throw an error...
if ($exception->getResponse()->getStatusCode() !== 404) {
throw $exception;
}
}
$aliasActions[] = [
'add' => [
'index' => $this->getIndexName(),
'alias' => $aliasName
]
];
$this->indexDriver->aliasActions($aliasActions);
}
/**
* Remove old indices which are not active anymore (remember, each bulk index creates a new index from scratch,
* making the "old" index a stale one).
*
* @return array<string> a list of index names which were removed
*/
public function removeOldIndices()
{
$aliasName = $this->searchClient->getIndexName(); // The alias name is the unprefixed index name
$currentlyLiveIndices = $this->indexDriver->indexesByAlias($aliasName);
$indexStatus = $this->systemDriver->status();
$allIndices = array_keys($indexStatus['indices']);
$indicesToBeRemoved = [];
foreach ($allIndices as $indexName) {
if (strpos($indexName, $aliasName . '-') !== 0) {
// filter out all indices not starting with the alias-name, as they are unrelated to our application
continue;
}
if (array_search($indexName, $currentlyLiveIndices) !== false) {
// skip the currently live index names from deletion
continue;
}
$indicesToBeRemoved[] = $indexName;
}
array_map(function ($index) {
$this->indexDriver->deleteIndex($index);
}, $indicesToBeRemoved);
return $indicesToBeRemoved;
}
/**
* Perform indexing without checking about duplication document
*
* This is used during bulk indexing to improve performance
*
* @param callable $callback
* @throws \Exception
*/
public function withBulkProcessing(callable $callback)
{
$bulkProcessing = $this->bulkProcessing;
$this->bulkProcessing = true;
try {
/** @noinspection PhpUndefinedMethodInspection */
$callback->__invoke();
} catch (\Exception $exception) {
$this->bulkProcessing = $bulkProcessing;
throw $exception;
}
$this->bulkProcessing = $bulkProcessing;
}
}