You are here

protected function SearchApiSolrService::getExcerpt in Search API Solr 7

Extract and format highlighting information for a specific item from a Solr response.

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

2 calls to SearchApiSolrService::getExcerpt()
SearchApiSolrService::extractResults in includes/service.inc
Extract results from a Solr response.
SearchApiSolrService::searchMultiple in includes/service.inc
Implements SearchApiMultiServiceInterface::searchMultiple().

File

includes/service.inc, line 1417

Class

SearchApiSolrService
Search service class using Solr server.

Code

protected function getExcerpt($response, $id, array &$fields, array $field_mapping, array $fulltext_fields) {
  if (!isset($response->highlighting->{$id})) {
    return FALSE;
  }
  $output = '';
  $highlighting = $response->highlighting->{$id};
  $highlight_fields = !empty($this->options['highlight_data']);
  $create_excerpt = !empty($this->options['excerpt']);
  if (!$highlight_fields && !$create_excerpt) {
    return FALSE;
  }

  // Collect highlighted field values for the excerpt and set them in the
  // field values, if requested.
  $excerpt_parts = array();
  $field_mapping = array_flip($field_mapping);
  $fulltext_fields = drupal_map_assoc($fulltext_fields);
  foreach ($highlighting as $solr_property => $values) {
    $values = (array) $values;
    if (empty($field_mapping[$solr_property])) {
      continue;
    }
    $search_api_property = $field_mapping[$solr_property];

    // Only use fields that were actually searched for the excerpt.
    if (isset($fulltext_fields[$search_api_property])) {

      // Remember the highlighted value so we can use it for the excerpt, if
      // requested.
      $excerpt_parts = array_merge($excerpt_parts, $values);
    }
    if (!$highlight_fields) {
      continue;
    }
    $values = $this
      ->sanitizeHighlightValue($values, $search_api_property);

    // Remove highlight prefixes and suffixes so we can compare values in
    // order to replace the corresponding items.
    $orig_values = preg_replace('#\\[(/?)HIGHLIGHT\\]#', '', $values);
    $field_values = array();
    if (!empty($fields[$search_api_property])) {
      $field_values = $this
        ->sanitizeHighlightValue($fields[$search_api_property]);
    }
    foreach ($field_values as $delta => $field_value) {
      foreach ($orig_values as $num => $item) {
        if ($item === $field_value) {
          $field_values[$delta] = $this
            ->formatHighlighting($values[$num]);
          $change = TRUE;
          continue 2;
        }
      }
    }
    if (!empty($change)) {
      $fields[$search_api_property] = array(
        '#value' => $field_values,
        '#sanitize_callback' => FALSE,
      );
    }
  }

  // Create an excerpt, if requested.
  if ($create_excerpt && $excerpt_parts) {
    $excerpt = array();
    $excerpt_length = 0;
    foreach ($excerpt_parts as $value) {

      // Excerpts don't have HTML (except for the highlighting tags, of
      // course).
      $value = strip_tags($value);
      foreach ($this
        ->extractHighlightingSnippets($value) as $snippet) {
        $excerpt[] = $snippet;
        $excerpt_length += drupal_strlen($snippet);

        // Restrict ourselves to three snippets or 300 characters.
        if (count($excerpt) >= 3 || $excerpt_length >= 300) {
          break 2;
        }
      }
    }
    if ($excerpt) {
      $output = implode(' … ', $excerpt) . ' …';
    }
  }
  return $output;
}