You are here

public function Highlight::postprocessSearchResults in Search API 8

Postprocess search results before they are returned by the query.

If a processor is used for both pre- and post-processing a search query, the same object will be used for both calls (so preserving some data or state locally is possible).

Parameters

\Drupal\search_api\Query\ResultSetInterface $results: The search results.

Overrides ProcessorPluginBase::postprocessSearchResults

File

src/Plugin/search_api/processor/Highlight.php, line 238

Class

Highlight
Adds a highlighted excerpt to results and highlights returned fields.

Namespace

Drupal\search_api\Plugin\search_api\processor

Code

public function postprocessSearchResults(ResultSetInterface $results) {
  $query = $results
    ->getQuery();
  if (!$results
    ->getResultCount() || $query
    ->getProcessingLevel() != QueryInterface::PROCESSING_FULL || !($keys = $this
    ->getKeywords($query))) {
    return;
  }
  $excerpt_fulltext_fields = $this->index
    ->getFulltextFields();
  if (!empty($this->configuration['exclude_fields'])) {
    $excerpt_fulltext_fields = array_diff($excerpt_fulltext_fields, $this->configuration['exclude_fields']);
  }
  $result_items = $results
    ->getResultItems();
  if ($this->configuration['excerpt']) {
    $this
      ->addExcerpts($result_items, $excerpt_fulltext_fields, $keys);
  }
  if ($this->configuration['highlight'] != 'never') {
    $highlighted_fields = $this
      ->highlightFields($result_items, $keys);
    foreach ($highlighted_fields as $item_id => $item_fields) {
      $item = $result_items[$item_id];
      $item
        ->setExtraData('highlighted_fields', $item_fields);
    }
  }
}