You are here

protected function SearchApiSolrBackend::setHighlighting in Search API Solr 8.3

Same name and namespace in other branches
  1. 8 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 $highlighted_fields: (optional) The solr fields to be highlighted.

1 call to SearchApiSolrBackend::setHighlighting()
SearchApiSolrBackend::setFields in src/Plugin/search_api/backend/SearchApiSolrBackend.php
Set the list of fields Solr should return as result.

File

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

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, array $highlighted_fields = []) {
  if (!empty($this->configuration['highlight_data'])) {
    $settings = Utility::getIndexSolrSettings($query
      ->getIndex());
    $highlighter = $settings['highlighter'];
    $hl = $solarium_query
      ->getHighlighting();
    $hl
      ->setSimplePrefix('[HIGHLIGHT]');
    $hl
      ->setSimplePostfix('[/HIGHLIGHT]');
    $hl
      ->setSnippets($highlighter['highlight']['snippets']);
    $hl
      ->setFragSize($highlighter['highlight']['fragsize']);
    $hl
      ->setMergeContiguous($highlighter['highlight']['mergeContiguous']);
    $hl
      ->setRequireFieldMatch($highlighter['highlight']['requireFieldMatch']);

    // Overwrite Solr default values only if required to have shorter request
    // strings.
    if (51200 != $highlighter['maxAnalyzedChars']) {
      $hl
        ->setMaxAnalyzedChars($highlighter['maxAnalyzedChars']);
    }
    if ('gap' !== $highlighter['fragmenter']) {
      $hl
        ->setFragmenter($highlighter['fragmenter']);
      if ('regex' !== $highlighter['fragmenter']) {
        $hl
          ->setRegexPattern($highlighter['regex']['pattern']);
        if (0.5 != $highlighter['regex']['slop']) {
          $hl
            ->setRegexSlop($highlighter['regex']['slop']);
        }
        if (10000 != $highlighter['regex']['maxAnalyzedChars']) {
          $hl
            ->setRegexMaxAnalyzedChars($highlighter['regex']['maxAnalyzedChars']);
        }
      }
    }
    if (!$highlighter['usePhraseHighlighter']) {
      $hl
        ->setUsePhraseHighlighter(FALSE);
    }
    if (!$highlighter['highlightMultiTerm']) {
      $hl
        ->setHighlightMultiTerm(FALSE);
    }
    if ($highlighter['preserveMulti']) {
      $hl
        ->setPreserveMulti(TRUE);
    }
    foreach ($highlighted_fields as $highlighted_field) {

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