Skip to content

Commit 3ec67c9

Browse files
committed
[FEATURE] allow to access the raw ElasticSearch Hit given a node
This feature is the basis for Result Highlighting.
1 parent 1f81ace commit 3ec67c9

2 files changed

Lines changed: 52 additions & 1 deletion

File tree

Classes/Flowpack/ElasticSearch/ContentRepositoryAdaptor/Eel/ElasticSearchQueryBuilder.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,16 @@ class ElasticSearchQueryBuilder implements QueryBuilderInterface, ProtectedConte
7575
*/
7676
protected $totalItems;
7777

78+
/**
79+
* This (internal) array stores, for the last search request, a mapping from Node Identifiers
80+
* to the full ElasticSearch Hit which was returned.
81+
*
82+
* This is needed to e.g. use result highlighting.
83+
*
84+
* @var array
85+
*/
86+
protected $elasticSearchHitsIndexedByNodeFromLastRequest;
87+
7888
/**
7989
* The ElasticSearch request, as it is being built up.
8090
* @var array
@@ -430,6 +440,19 @@ public function getFrom() {
430440
return $this->from;
431441
}
432442

443+
/**
444+
* This low-level method can be used to look up the full ElasticSearch hit given a certain node.
445+
*
446+
* @param NodeInterface $node
447+
* @return array the ElasticSearch hit for the node as array, or NULL if it does not exist.
448+
*/
449+
public function getFullElasticSearchHitForNode(NodeInterface $node) {
450+
if (isset($this->elasticSearchHitsIndexedByNodeFromLastRequest[$node->getIdentifier()])) {
451+
return $this->elasticSearchHitsIndexedByNodeFromLastRequest[$node->getIdentifier()];
452+
}
453+
return NULL;
454+
}
455+
433456
/**
434457
* Execute the query and return the list of nodes as result.
435458
*
@@ -456,6 +479,7 @@ public function fetch() {
456479
}
457480

458481
$nodes = array();
482+
$elasticSearchHitPerNode = array();
459483

460484
/**
461485
* TODO: This code below is not fully correct yet:
@@ -482,6 +506,7 @@ public function fetch() {
482506
$node = $this->contextNode->getNode($nodePath);
483507
if ($node instanceof NodeInterface) {
484508
$nodes[$node->getIdentifier()] = $node;
509+
$elasticSearchHitPerNode[$node->getIdentifier()] = $hit;
485510
if ($this->limit > 0 && count($nodes) >= $this->limit) {
486511
break;
487512
}
@@ -492,6 +517,8 @@ public function fetch() {
492517
$this->logger->log('Query Log (' . $this->logMessage . ') Number of returned results: ' . count($nodes), LOG_DEBUG);
493518
}
494519

520+
$this->elasticSearchHitsIndexedByNodeFromLastRequest = $elasticSearchHitPerNode;
521+
495522
return array_values($nodes);
496523
}
497524

Classes/Flowpack/ElasticSearch/ContentRepositoryAdaptor/Eel/ElasticSearchQueryResult.php

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@
1111
* The TYPO3 project - inspiring people to share! *
1212
* */
1313

14+
use TYPO3\Eel\ProtectedContextAwareInterface;
1415
use TYPO3\Flow\Persistence\QueryResultInterface;
16+
use TYPO3\TYPO3CR\Domain\Model\NodeInterface;
1517

16-
class ElasticSearchQueryResult implements QueryResultInterface {
18+
class ElasticSearchQueryResult implements QueryResultInterface, ProtectedContextAwareInterface {
1719

1820
/**
1921
* @var ElasticSearchQuery
@@ -152,4 +154,26 @@ public function count() {
152154

153155
return $this->count;
154156
}
157+
158+
/**
159+
* Returns the ElasticSearch "hit" (e.g. the raw content being transferred back from ElasticSearch)
160+
* for the given node.
161+
*
162+
* Can be used for example to access highlighting information.
163+
*
164+
* @param NodeInterface $node
165+
* @return array the ElasticSearch hit, or NULL if it does not exist.
166+
* @api
167+
*/
168+
public function searchHitForNode(NodeInterface $node) {
169+
return $this->elasticSearchQuery->getQueryBuilder()->getFullElasticSearchHitForNode($node);
170+
}
171+
172+
/**
173+
* @param string $methodName
174+
* @return boolean
175+
*/
176+
public function allowsCallOfMethod($methodName) {
177+
return TRUE;
178+
}
155179
}

0 commit comments

Comments
 (0)