Skip to content

Commit

Permalink
Fix Solr highlighting in Blender. (#3610)
Browse files Browse the repository at this point in the history
Injects highlighting data to Solr records when they're created so that it's present when the blended result set is created.
  • Loading branch information
EreMaijala committed Apr 23, 2024
1 parent f78abfc commit 415a6ae
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 42 deletions.
5 changes: 5 additions & 0 deletions module/VuFind/src/VuFind/RecordDriver/PluginManager.php
Expand Up @@ -181,9 +181,14 @@ public function getSolrRecord(
);
$recordType = $this->has($key) ? $key : $keyPrefix . $defaultKeySuffix;

// Extract highlighting details injected earlier by
// \VuFindSearch\Backend\Solr\Response\Json\RecordCollectionFactory
$hl = $data['__highlight_details'] ?? [];
unset($data['__highlight_details']);
// Build the object:
$driver = $this->get($recordType);
$driver->setRawData($data);
$driver->setHighlightDetails($hl);
return $driver;
}

Expand Down
Expand Up @@ -103,11 +103,6 @@ public function attach(SharedEventManagerInterface $manager)
Service::EVENT_PRE,
[$this, 'onSearchPre']
);
$manager->attach(
Service::class,
Service::EVENT_POST,
[$this, 'onSearchPost']
);
}

/**
Expand Down Expand Up @@ -146,33 +141,4 @@ public function onSearchPre(EventInterface $event)
}
return $event;
}

/**
* Inject highlighting results.
*
* @param EventInterface $event Event
*
* @return EventInterface
*/
public function onSearchPost(EventInterface $event)
{
// Do nothing if highlighting is disabled or context is wrong
$command = $event->getParam('command');
if (!$this->active || $command->getContext() != 'search') {
return $event;
}

// Inject highlighting details into record objects:
if ($command->getTargetIdentifier() === $this->backend->getIdentifier()) {
$result = $command->getResult();
$hlDetails = $result->getHighlighting();
foreach ($result->getRecords() as $record) {
$id = $record->getUniqueId();
if (isset($hlDetails[$id])) {
$record->setHighlightDetails($hlDetails[$id]);
}
}
}
return $event;
}
}
Expand Up @@ -83,14 +83,9 @@ protected function setUp(): void
public function testAttach()
{
$mock = $this->createMock(\Laminas\EventManager\SharedEventManagerInterface::class);
$this->expectConsecutiveCalls(
$mock,
'attach',
[
[\VuFindSearch\Service::class, 'pre', [$this->listener, 'onSearchPre']],
[\VuFindSearch\Service::class, 'post', [$this->listener, 'onSearchPost']],
]
);
$mock->expects($this->once())
->method('attach')
->with(\VuFindSearch\Service::class, 'pre', [$this->listener, 'onSearchPre']);
$this->listener->attach($mock);
}

Expand Down
Expand Up @@ -101,7 +101,12 @@ public function factory($response)
);
}
$collection = new $this->collectionClass($response);
$hlDetails = $response['highlighting'] ?? [];
foreach ($response['response']['docs'] ?? [] as $doc) {
// If highlighting details were provided, merge them into the record for future use:
if (isset($doc['id']) && ($hl = $hlDetails[$doc['id']] ?? [])) {
$doc['__highlight_details'] = $hl;
}
$collection->add(call_user_func($this->recordFactory, $doc), false);
}
return $collection;
Expand Down

0 comments on commit 415a6ae

Please sign in to comment.