You are here

protected function SearchApiSolrBackend::setHighlighting in Search API Solr 8

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::setHighlighting()
  2. 8.2 src/Plugin/search_api/backend/SearchApiSolrBackend.php \Drupal\search_api_solr\Plugin\search_api\backend\SearchApiSolrBackend::setHighlighting()
  3. 4.x src/Plugin/search_api/backend/SearchApiSolrBackend.php \Drupal\search_api_solr\Plugin\search_api\backend\SearchApiSolrBackend::setHighlighting()

Sets the highlighting parameters.

(The $query parameter currently isn't used and only here for the potential sake of subclasses.)

Parameters

\Solarium\QueryType\Select\Query\Query $solarium_query: The Solarium select query object.

\Drupal\search_api\Query\QueryInterface $query: The query object.

array $query_fields: The solr fields to be highlighted.

1 call to SearchApiSolrBackend::setHighlighting()
SearchApiSolrBackend::search in src/Plugin/search_api/backend/SearchApiSolrBackend.php
Options on $query prefixed by 'solr_param_' will be passed natively to Solr as query parameter without the prefix. For example you can set the "Minimum Should Match" parameter 'mm' to '75%' like this:

File

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

Class

SearchApiSolrBackend
Apache Solr backend for search api.

Namespace

Drupal\search_api_solr\Plugin\search_api\backend

Code

protected function setHighlighting(Query $solarium_query, QueryInterface $query, $highlighted_fields = []) {
  $excerpt = !empty($this->configuration['excerpt']);
  $highlight = !empty($this->configuration['highlight_data']);
  if ($highlight || $excerpt) {
    $highlighter = \Drupal::config('search_api_solr.standard_highlighter');
    $hl = $solarium_query
      ->getHighlighting();
    $hl
      ->setSimplePrefix('[HIGHLIGHT]');
    $hl
      ->setSimplePostfix('[/HIGHLIGHT]');
    if ($highlighter
      ->get('maxAnalyzedChars') != $highlighter
      ->getOriginal('maxAnalyzedChars')) {
      $hl
        ->setMaxAnalyzedChars($highlighter
        ->get('maxAnalyzedChars'));
    }
    if ($highlighter
      ->get('fragmenter') != $highlighter
      ->getOriginal('fragmenter')) {
      $hl
        ->setFragmenter($highlighter
        ->get('fragmenter'));
    }
    if ($highlighter
      ->get('usePhraseHighlighter') != $highlighter
      ->getOriginal('usePhraseHighlighter')) {
      $hl
        ->setUsePhraseHighlighter($highlighter
        ->get('usePhraseHighlighter'));
    }
    if ($highlighter
      ->get('highlightMultiTerm') != $highlighter
      ->getOriginal('highlightMultiTerm')) {
      $hl
        ->setHighlightMultiTerm($highlighter
        ->get('highlightMultiTerm'));
    }
    if ($highlighter
      ->get('preserveMulti') != $highlighter
      ->getOriginal('preserveMulti')) {
      $hl
        ->setPreserveMulti($highlighter
        ->get('preserveMulti'));
    }
    if ($highlighter
      ->get('regex.slop') != $highlighter
      ->getOriginal('regex.slop')) {
      $hl
        ->setRegexSlop($highlighter
        ->get('regex.slop'));
    }
    if ($highlighter
      ->get('regex.pattern') != $highlighter
      ->getOriginal('regex.pattern')) {
      $hl
        ->setRegexPattern($highlighter
        ->get('regex.pattern'));
    }
    if ($highlighter
      ->get('regex.maxAnalyzedChars') != $highlighter
      ->getOriginal('regex.maxAnalyzedChars')) {
      $hl
        ->setRegexMaxAnalyzedChars($highlighter
        ->get('regex.maxAnalyzedChars'));
    }
    if ($excerpt) {

      // If the field doesn't exist yet getField() will add it.
      $excerpt_field = $hl
        ->getField('spell');
      $excerpt_field
        ->setSnippets($highlighter
        ->get('excerpt.snippets'));
      $excerpt_field
        ->setFragSize($highlighter
        ->get('excerpt.fragsize'));
      $excerpt_field
        ->setMergeContiguous($highlighter
        ->get('excerpt.mergeContiguous'));
    }
    if ($highlight && !empty($highlighted_fields)) {
      foreach ($highlighted_fields as $highlighted_field) {

        // We must not set the fields at once using setFields() to not break
        // the excerpt feature above.
        $hl
          ->addField($highlighted_field);
      }

      // @todo the amount of snippets need to be increased to get highlighting
      //   of multi value fields to work.
      // @see https://drupal.org/node/2753635
      $hl
        ->setSnippets(1);
      $hl
        ->setFragSize(0);
      $hl
        ->setMergeContiguous($highlighter
        ->get('highlight.mergeContiguous'));
      $hl
        ->setRequireFieldMatch($highlighter
        ->get('highlight.requireFieldMatch'));
    }
  }
}