/**
  * Submit requests for more spelling suggestions.
  *
  * @param Spellcheck $spellcheck Aggregating spellcheck object
  * @param string     $query      Spellcheck query
  *
  * @return void
  */
 protected function aggregateSpellcheck(Spellcheck $spellcheck, $query)
 {
     while (next($this->dictionaries) !== false) {
         $params = new ParamBag();
         $params->set('spellcheck', 'true');
         $params->set('spellcheck.dictionary', current($this->dictionaries));
         $queryObj = new Query($query, 'AllFields');
         $collection = $this->backend->search($queryObj, 0, 0, $params);
         $spellcheck->mergeWith($collection->getSpellcheck());
     }
 }
 /**
  * Format hierarchical facets accordingly
  *
  * @param EventInterface $event Event
  *
  * @return EventInterface
  */
 public function onSearchPost(EventInterface $event)
 {
     $backend = $event->getParam('backend');
     if ($backend != $this->backend->getIdentifier()) {
         return $event;
     }
     $context = $event->getParam('context');
     if ($context == 'search' || $context == 'retrieve') {
         $this->processHierarchicalFacets($event);
     }
     return $event;
 }
 /**
  * Submit requests for more spelling suggestions.
  *
  * @param Spellcheck $spellcheck Aggregating spellcheck object
  * @param string     $query      Spellcheck query
  *
  * @return void
  */
 protected function aggregateSpellcheck(Spellcheck $spellcheck, $query)
 {
     while (next($this->dictionaries) !== false) {
         $params = new ParamBag();
         $params->set('spellcheck', 'true');
         $params->set('spellcheck.dictionary', current($this->dictionaries));
         $queryObj = new Query($query, 'AllFields');
         try {
             $collection = $this->backend->search($queryObj, 0, 0, $params);
             $spellcheck->mergeWith($collection->getSpellcheck());
         } catch (\Exception $ex) {
             if ($this->logger) {
                 $this->logger->err("Exception thrown when aggregating spellcheck, ignoring.", array('exception' => $ex));
             }
         }
     }
 }
 /**
  * Inject highlighting results.
  *
  * @param EventInterface $event Event
  *
  * @return EventInterface
  */
 public function onSearchPost(EventInterface $event)
 {
     // Do nothing if highlighting is disabled or context is wrong
     if (!$this->active || $event->getParam('context') != 'search') {
         return $event;
     }
     // Inject highlighting details into record objects:
     $backend = $event->getParam('backend');
     if ($backend == $this->backend->getIdentifier()) {
         $result = $event->getTarget();
         $hlDetails = $result->getHighlighting();
         foreach ($result->getRecords() as $record) {
             $id = $record->getUniqueId();
             if (isset($hlDetails[$id])) {
                 $record->setHighlightDetails($hlDetails[$id]);
             }
         }
     }
 }
 /**
  * Fetch local records for all the found dedup records
  *
  * @param EventInterface $event Event
  *
  * @return void
  */
 protected function fetchLocalRecords($event)
 {
     $config = $this->serviceLocator->get('VuFind\\Config');
     $searchConfig = $config->get($this->searchConfig);
     $dataSourceConfig = $config->get($this->dataSourceConfig);
     $recordSources = isset($searchConfig->Records->sources) ? $searchConfig->Records->sources : '';
     $sourcePriority = $this->determineSourcePriority($recordSources);
     $params = $event->getParam('params');
     $buildingPriority = $this->determineBuildingPriority($params);
     $idList = [];
     // Find out the best records and list their IDs:
     $result = $event->getTarget();
     foreach ($result->getRecords() as $record) {
         $fields = $record->getRawData();
         if (!isset($fields['merged_boolean'])) {
             continue;
         }
         $localIds = $fields['local_ids_str_mv'];
         $dedupId = $localIds[0];
         $priority = 99999;
         $undefPriority = 99999;
         // Find the document that matches the source priority best:
         $dedupData = [];
         foreach ($localIds as $localId) {
             $localPriority = null;
             list($source) = explode('.', $localId, 2);
             if (!empty($buildingPriority)) {
                 if (isset($buildingPriority[$source])) {
                     $localPriority = -$buildingPriority[$source];
                 } elseif (isset($dataSourceConfig[$source]['institution'])) {
                     $institution = $dataSourceConfig[$source]['institution'];
                     if (isset($buildingPriority[$institution])) {
                         $localPriority = -$buildingPriority[$institution];
                     }
                 }
             }
             if (!isset($localPriority)) {
                 if (isset($sourcePriority[$source])) {
                     $localPriority = $sourcePriority[$source];
                 } else {
                     $localPriority = ++$undefPriority;
                 }
             }
             if (isset($localPriority) && $localPriority < $priority) {
                 $dedupId = $localId;
                 $priority = $localPriority;
             }
             $dedupData[$source] = ['id' => $localId, 'priority' => isset($localPriority) ? $localPriority : 99999];
         }
         $fields['dedup_id'] = $dedupId;
         $idList[] = $dedupId;
         // Sort dedupData by priority:
         uasort($dedupData, function ($a, $b) {
             return $a['priority'] - $b['priority'];
         });
         $fields['dedup_data'] = $dedupData;
         $record->setRawData($fields);
     }
     if (empty($idList)) {
         return;
     }
     // Fetch records and assign them to the result:
     $localRecords = $this->backend->retrieveBatch($idList)->getRecords();
     foreach ($result->getRecords() as $record) {
         $dedupRecordData = $record->getRawData();
         if (!isset($dedupRecordData['dedup_id'])) {
             continue;
         }
         // Find the corresponding local record in the results:
         $foundLocalRecord = null;
         foreach ($localRecords as $localRecord) {
             if ($localRecord->getUniqueID() == $dedupRecordData['dedup_id']) {
                 $foundLocalRecord = $localRecord;
                 break;
             }
         }
         if (!$foundLocalRecord) {
             continue;
         }
         $localRecordData = $foundLocalRecord->getRawData();
         // Copy dedup_data for the active data sources:
         foreach ($dedupRecordData['dedup_data'] as $dedupDataKey => $dedupData) {
             if (!$recordSources || isset($sourcePriority[$dedupDataKey])) {
                 $localRecordData['dedup_data'][$dedupDataKey] = $dedupData;
             }
         }
         // Copy fields from dedup record to local record
         $localRecordData = $this->appendDedupRecordFields($localRecordData, $dedupRecordData, $recordSources, $sourcePriority);
         $foundLocalRecord->setRawData($localRecordData);
         $foundLocalRecord->setHighlightDetails($record->getHighlightDetails());
         $result->replace($record, $foundLocalRecord);
     }
 }