You are here

protected function SearchApiSolrBackend::getHighlighting in Search API Solr 4.x

Same name and namespace in other branches
  1. 8.3 src/Plugin/search_api/backend/SearchApiSolrBackend.php \Drupal\search_api_solr\Plugin\search_api\backend\SearchApiSolrBackend::getHighlighting()
  2. 8.2 src/Plugin/search_api/backend/SearchApiSolrBackend.php \Drupal\search_api_solr\Plugin\search_api\backend\SearchApiSolrBackend::getHighlighting()

Extract and format highlighting information for a specific item.

Will also use highlighted fields to replace retrieved field data, if the corresponding option is set.

Parameters

array $data: The data extracted from a Solr result.

string $solr_id: The ID of the result item.

\Drupal\search_api\Item\ItemInterface $item: The fields of the result item.

array $field_mapping: Mapping from search_api field names to Solr field names.

1 call to SearchApiSolrBackend::getHighlighting()
SearchApiSolrBackend::extractResults in src/Plugin/search_api/backend/SearchApiSolrBackend.php
Extract results from a Solr response.

File

src/Plugin/search_api/backend/SearchApiSolrBackend.php, line 3986

Class

SearchApiSolrBackend
Apache Solr backend for search api.

Namespace

Drupal\search_api_solr\Plugin\search_api\backend

Code

protected function getHighlighting(array $data, $solr_id, ItemInterface $item, array $field_mapping) {
  if (isset($data['highlighting'][$solr_id]) && !empty($this->configuration['highlight_data'])) {
    $prefix = '<strong>';
    $suffix = '</strong>';
    try {
      $highlight_config = $item
        ->getIndex()
        ->getProcessor('highlight')
        ->getConfiguration();
      if ($highlight_config['highlight'] === 'never') {
        return;
      }
      $prefix = $highlight_config['prefix'];
      $suffix = $highlight_config['suffix'];
    } catch (SearchApiException $exception) {

      // Highlighting processor is not enabled for this index.
    }
    $snippets = [];
    $keys = [];
    foreach ($field_mapping as $search_api_property => $solr_property) {
      if (!empty($data['highlighting'][$solr_id][$solr_property])) {
        foreach ($data['highlighting'][$solr_id][$solr_property] as $value) {
          $keys[] = Utility::getHighlightedKeys($value);

          // Contrary to above, we here want to preserve HTML, so we just
          // replace the [HIGHLIGHT] tags with the appropriate format.
          $snippets[$search_api_property][] = Utility::formatHighlighting($value, $prefix, $suffix);
        }
      }
    }
    if ($snippets) {
      $item
        ->setExtraData('highlighted_fields', $snippets);
      $item
        ->setExtraData('highlighted_keys', array_unique(array_merge(...$keys)));
    }
  }
}