|
| 1 | +<?php |
| 2 | +namespace Flowpack\ElasticSearch\ContentRepositoryAdaptor\Driver\Version5; |
| 3 | + |
| 4 | +/* |
| 5 | + * This file is part of the Flowpack.ElasticSearch.ContentRepositoryAdaptor package. |
| 6 | + * |
| 7 | + * (c) Contributors of the Neos Project - www.neos.io |
| 8 | + * |
| 9 | + * This package is Open Source Software. For the full copyright and license |
| 10 | + * information, please view the LICENSE file which was distributed with this |
| 11 | + * source code. |
| 12 | + */ |
| 13 | + |
| 14 | +use Flowpack\ElasticSearch\ContentRepositoryAdaptor\Driver\AbstractNodeTypeMappingBuilder; |
| 15 | +use Flowpack\ElasticSearch\ContentRepositoryAdaptor\Driver\Version2; |
| 16 | +use Flowpack\ElasticSearch\Domain\Model\Document as ElasticSearchDocument; |
| 17 | +use TYPO3\Flow\Annotations as Flow; |
| 18 | +use TYPO3\TYPO3CR\Domain\Model\NodeInterface; |
| 19 | + |
| 20 | +/** |
| 21 | + * Indexer driver for Elasticsearch version 5.x |
| 22 | + * |
| 23 | + * @Flow\Scope("singleton") |
| 24 | + */ |
| 25 | +class IndexerDriver extends Version2\IndexerDriver |
| 26 | +{ |
| 27 | + /** |
| 28 | + * {@inheritdoc} |
| 29 | + */ |
| 30 | + public function document($indexName, NodeInterface $node, ElasticSearchDocument $document, array $documentData) |
| 31 | + { |
| 32 | + if ($this->isFulltextRoot($node)) { |
| 33 | + // for fulltext root documents, we need to preserve the "__fulltext" field. That's why we use the |
| 34 | + // "update" API instead of the "index" API, with a custom script internally; as we |
| 35 | + // shall not delete the "__fulltext" part of the document if it has any. |
| 36 | + return [ |
| 37 | + [ |
| 38 | + 'update' => [ |
| 39 | + '_type' => $document->getType()->getName(), |
| 40 | + '_id' => $document->getId(), |
| 41 | + '_index' => $indexName, |
| 42 | + '_retry_on_conflict' => 3 |
| 43 | + ] |
| 44 | + ], |
| 45 | + // http://www.elasticsearch.org/guide/en/elasticsearch/reference/2.4/docs-update.html |
| 46 | + [ |
| 47 | + 'script' => [ |
| 48 | + 'stored' => 'updateFulltextParts', |
| 49 | + 'params' => [ |
| 50 | + 'newData' => $documentData |
| 51 | + ] |
| 52 | + ], |
| 53 | + 'upsert' => $documentData |
| 54 | + ] |
| 55 | + ]; |
| 56 | + } |
| 57 | + |
| 58 | + // non-fulltext-root documents can be indexed as-they-are |
| 59 | + return [ |
| 60 | + [ |
| 61 | + 'index' => [ |
| 62 | + '_type' => $document->getType()->getName(), |
| 63 | + '_id' => $document->getId() |
| 64 | + ] |
| 65 | + ], |
| 66 | + $documentData |
| 67 | + ]; |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * {@inheritdoc} |
| 72 | + */ |
| 73 | + public function fulltext(NodeInterface $node, array $fulltextIndexOfNode, $targetWorkspaceName = null) |
| 74 | + { |
| 75 | + $closestFulltextNode = $node; |
| 76 | + while (!$this->isFulltextRoot($closestFulltextNode)) { |
| 77 | + $closestFulltextNode = $closestFulltextNode->getParent(); |
| 78 | + if ($closestFulltextNode === null) { |
| 79 | + // root of hierarchy, no fulltext root found anymore, abort silently... |
| 80 | + $this->logger->log(sprintf('NodeIndexer: No fulltext root found for node %s (%)', $node->getPath(), $node->getIdentifier()), LOG_WARNING, null, 'ElasticSearch (CR)'); |
| 81 | + |
| 82 | + return null; |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + $closestFulltextNodeContextPath = $closestFulltextNode->getContextPath(); |
| 87 | + if ($targetWorkspaceName !== null) { |
| 88 | + $closestFulltextNodeContextPath = str_replace($node->getContext()->getWorkspace()->getName(), $targetWorkspaceName, $closestFulltextNodeContextPath); |
| 89 | + } |
| 90 | + $closestFulltextNodeDocumentIdentifier = sha1($closestFulltextNodeContextPath); |
| 91 | + |
| 92 | + if ($closestFulltextNode->isRemoved()) { |
| 93 | + // fulltext root is removed, abort silently... |
| 94 | + $this->logger->log(sprintf('NodeIndexer (%s): Fulltext root found for %s (%s) not updated, it is removed', $closestFulltextNodeDocumentIdentifier, $node->getPath(), $node->getIdentifier()), LOG_DEBUG, null, 'ElasticSearch (CR)'); |
| 95 | + |
| 96 | + return null; |
| 97 | + } |
| 98 | + |
| 99 | + $this->logger->log(sprintf('NodeIndexer (%s): Updated fulltext index for %s (%s)', $closestFulltextNodeDocumentIdentifier, $closestFulltextNodeContextPath, $closestFulltextNode->getIdentifier()), LOG_DEBUG, null, 'ElasticSearch (CR)'); |
| 100 | + |
| 101 | + $upsertFulltextParts = []; |
| 102 | + if (!empty($fulltextIndexOfNode)) { |
| 103 | + $upsertFulltextParts[$node->getIdentifier()] = $fulltextIndexOfNode; |
| 104 | + } |
| 105 | + |
| 106 | + return [ |
| 107 | + [ |
| 108 | + 'update' => [ |
| 109 | + '_type' => AbstractNodeTypeMappingBuilder::convertNodeTypeNameToMappingName($closestFulltextNode->getNodeType()->getName()), |
| 110 | + '_id' => $closestFulltextNodeDocumentIdentifier |
| 111 | + ] |
| 112 | + ], |
| 113 | + // http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/docs-update.html |
| 114 | + [ |
| 115 | + // first, update the __fulltextParts, then re-generate the __fulltext from all __fulltextParts |
| 116 | + 'script' => [ |
| 117 | + 'stored' => 'regenerateFulltext', |
| 118 | + 'params' => [ |
| 119 | + 'identifier' => $node->getIdentifier(), |
| 120 | + 'nodeIsRemoved' => $node->isRemoved(), |
| 121 | + 'nodeIsHidden' => $node->isHidden(), |
| 122 | + 'fulltext' => $fulltextIndexOfNode |
| 123 | + ], |
| 124 | + ], |
| 125 | + 'upsert' => [ |
| 126 | + '__fulltext' => $fulltextIndexOfNode, |
| 127 | + '__fulltextParts' => $upsertFulltextParts |
| 128 | + ] |
| 129 | + ] |
| 130 | + ]; |
| 131 | + } |
| 132 | +} |
0 commit comments