You are here

protected function SearchApiSolrBackend::getExcerpt in Search API Solr 8

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.

Return value

bool|string FALSE if no excerpt is returned from Solr, the excerpt string otherwise.

1 call to SearchApiSolrBackend::getExcerpt()
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 2278

Class

SearchApiSolrBackend
Apache Solr backend for search api.

Namespace

Drupal\search_api_solr\Plugin\search_api\backend

Code

protected function getExcerpt($data, $solr_id, ItemInterface $item, array $field_mapping) {
  if (!isset($data['highlighting'][$solr_id])) {
    return FALSE;
  }
  $output = '';

  // @todo using the spell field is not the optimal solution.
  // @see https://www.drupal.org/node/2735881
  if (!empty($this->configuration['excerpt']) && !empty($data['highlighting'][$solr_id]['spell'])) {
    foreach ($data['highlighting'][$solr_id]['spell'] as $snippet) {
      $snippet = strip_tags($snippet);
      $snippet = preg_replace('/^.*>|<.*$/', '', $snippet);
      $snippet = SearchApiSolrUtility::formatHighlighting($snippet);

      // The created fragments sometimes have leading or trailing punctuation.
      // We remove that here for all common cases, but take care not to remove
      // < or > (so HTML tags stay valid).
      $snippet = trim($snippet, "\0../:;=?..@[..`");
      $output .= $snippet . ' … ';
    }
  }
  if (!empty($this->configuration['highlight_data'])) {
    $item_fields = $item
      ->getFields();
    foreach ($field_mapping as $search_api_property => $solr_property) {
      if (!empty($data['highlighting'][$solr_id][$solr_property])) {
        $snippets = [];
        foreach ($data['highlighting'][$solr_id][$solr_property] as $value) {

          // Contrary to above, we here want to preserve HTML, so we just
          // replace the [HIGHLIGHT] tags with the appropriate format.
          $snippets[] = [
            'raw' => preg_replace('#\\[(/?)HIGHLIGHT\\]#', '', $value),
            'replace' => SearchApiSolrUtility::formatHighlighting($value),
          ];
        }
        if ($snippets) {
          $values = $item_fields[$search_api_property]
            ->getValues();
          foreach ($values as $value) {
            foreach ($snippets as $snippet) {
              if ($value instanceof TextValue) {
                if ($value
                  ->getText() === $snippet['raw']) {
                  $value
                    ->setText($snippet['replace']);
                }
              }
              else {
                if ($value == $snippet['raw']) {
                  $value = $snippet['replace'];
                }
              }
            }
          }
          $item_fields[$search_api_property]
            ->setValues($values);
        }
      }
    }
  }
  return $output;
}