|
22 | 22 | use TYPO3\TYPO3CR\Domain\Service\ContextFactory;
|
23 | 23 | use TYPO3\TYPO3CR\Domain\Service\NodeTypeManager;
|
24 | 24 | use TYPO3\TYPO3CR\Search\Indexer\AbstractNodeIndexer;
|
| 25 | +use TYPO3\TYPO3CR\Search\Indexer\BulkNodeIndexerInterface; |
25 | 26 |
|
26 | 27 | /**
|
27 | 28 | * Indexer for Content Repository Nodes. Triggered from the NodeIndexingManager.
|
|
30 | 31 | *
|
31 | 32 | * @Flow\Scope("singleton")
|
32 | 33 | */
|
33 |
| -class NodeIndexer extends AbstractNodeIndexer |
| 34 | +class NodeIndexer extends AbstractNodeIndexer implements BulkNodeIndexerInterface |
34 | 35 | {
|
35 | 36 | /**
|
36 | 37 | * Optional postfix for the index, e.g. to have different indexes by timestamp.
|
@@ -88,6 +89,11 @@ class NodeIndexer extends AbstractNodeIndexer
|
88 | 89 | */
|
89 | 90 | protected $currentBulkRequest = [];
|
90 | 91 |
|
| 92 | + /** |
| 93 | + * @var boolean |
| 94 | + */ |
| 95 | + protected $bulkProcessing = false; |
| 96 | + |
91 | 97 | /**
|
92 | 98 | * Returns the index name to be used for indexing, with optional indexNamePostfix appended.
|
93 | 99 | *
|
@@ -162,24 +168,26 @@ public function indexNode(NodeInterface $node, $targetWorkspaceName = null)
|
162 | 168 |
|
163 | 169 | $mappingType = $this->getIndex()->findType(NodeTypeMappingBuilder::convertNodeTypeNameToMappingName($nodeType));
|
164 | 170 |
|
165 |
| - // Remove document with the same contextPathHash but different NodeType, required after NodeType change |
166 |
| - $this->logger->log(sprintf('NodeIndexer: Removing node %s from index (if node type changed from %s). ID: %s', $contextPath, $node->getNodeType()->getName(), $contextPathHash), LOG_DEBUG, null, 'ElasticSearch (CR)'); |
167 |
| - $this->getIndex()->request('DELETE', '/_query', [], json_encode([ |
168 |
| - 'query' => [ |
169 |
| - 'bool' => [ |
170 |
| - 'must' => [ |
171 |
| - 'ids' => [ |
172 |
| - 'values' => [$contextPathHash] |
173 |
| - ] |
174 |
| - ], |
175 |
| - 'must_not' => [ |
176 |
| - 'term' => [ |
177 |
| - '_type' => NodeTypeMappingBuilder::convertNodeTypeNameToMappingName($node->getNodeType()->getName()) |
178 |
| - ] |
179 |
| - ], |
| 171 | + if ($this->bulkProcessing === false) { |
| 172 | + // Remove document with the same contextPathHash but different NodeType, required after NodeType change |
| 173 | + $this->logger->log(sprintf('NodeIndexer: Search and remove duplicate document if needed. ID: %s', $contextPath, $node->getNodeType()->getName(), $contextPathHash), LOG_DEBUG, null, 'ElasticSearch (CR)'); |
| 174 | + $this->getIndex()->request('DELETE', '/_query', [], json_encode([ |
| 175 | + 'query' => [ |
| 176 | + 'bool' => [ |
| 177 | + 'must' => [ |
| 178 | + 'ids' => [ |
| 179 | + 'values' => [$contextPathHash] |
| 180 | + ] |
| 181 | + ], |
| 182 | + 'must_not' => [ |
| 183 | + 'term' => [ |
| 184 | + '_type' => NodeTypeMappingBuilder::convertNodeTypeNameToMappingName($node->getNodeType()->getName()) |
| 185 | + ] |
| 186 | + ], |
| 187 | + ] |
180 | 188 | ]
|
181 |
| - ] |
182 |
| - ])); |
| 189 | + ])); |
| 190 | + } |
183 | 191 |
|
184 | 192 | if ($node->isRemoved()) {
|
185 | 193 | // TODO: handle deletion from the fulltext index as well
|
@@ -541,4 +549,26 @@ public function removeOldIndices()
|
541 | 549 |
|
542 | 550 | return $indicesToBeRemoved;
|
543 | 551 | }
|
| 552 | + |
| 553 | + /** |
| 554 | + * Perform indexing without checking about duplication document |
| 555 | + * |
| 556 | + * This is used during bulk indexing to improve performance |
| 557 | + * |
| 558 | + * @param callable $callback |
| 559 | + * @throws \Exception |
| 560 | + */ |
| 561 | + public function withBulkProcessing(callable $callback) |
| 562 | + { |
| 563 | + $bulkProcessing = $this->bulkProcessing; |
| 564 | + $this->bulkProcessing = true; |
| 565 | + try { |
| 566 | + /** @noinspection PhpUndefinedMethodInspection */ |
| 567 | + $callback->__invoke(); |
| 568 | + } catch (\Exception $exception) { |
| 569 | + $this->bulkProcessing = $bulkProcessing; |
| 570 | + throw $exception; |
| 571 | + } |
| 572 | + $this->bulkProcessing = $bulkProcessing; |
| 573 | + } |
544 | 574 | }
|
0 commit comments