You are here

protected function SearchApiSolrBackend::setHighlighting in Search API Solr 8.2

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 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 3020

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 = []) {
  if (!empty($this->configuration['highlight_data'])) {
    $settings = $query
      ->getIndex()
      ->getThirdPartySettings('search_api_solr') + search_api_solr_default_index_third_party_settings();
    $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);
    }
  }
}