You are here

protected function SearchApiSolrBackend::setSpellcheck in Search API Solr 8.3

Same name and namespace in other branches
  1. 4.x src/Plugin/search_api/backend/SearchApiSolrBackend.php \Drupal\search_api_solr\Plugin\search_api\backend\SearchApiSolrBackend::setSpellcheck()

Adds spellcheck features to the search query.

Parameters

\Solarium\Component\ComponentAwareQueryInterface $solarium_query: The Solarium query.

\Drupal\search_api\Query\QueryInterface $query: The Search API query.

array $spellcheck_options: The spellcheck options to add.

Throws

\Drupal\Component\Plugin\Exception\PluginException

\Drupal\search_api\SearchApiException

\Drupal\search_api_solr\SearchApiSolrException

2 calls to SearchApiSolrBackend::setSpellcheck()
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:
SearchApiSolrBackend::setAutocompleteSpellCheckQuery in src/Plugin/search_api/backend/SearchApiSolrBackend.php
Set the spellcheck parameters for the solarium autocomplete query.

File

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

Class

SearchApiSolrBackend
Apache Solr backend for search api.

Namespace

Drupal\search_api_solr\Plugin\search_api\backend

Code

protected function setSpellcheck(ComponentAwareQueryInterface $solarium_query, QueryInterface $query, array $spellcheck_options = []) {

  /** @var \Solarium\Component\Spellcheck $spellcheck */
  $spellcheck = $solarium_query
    ->getSpellcheck();
  $schema_languages = $this
    ->getSchemaLanguageStatistics();
  $dictionaries = [];
  foreach ($query
    ->getLanguages() as $language_id) {
    if (isset($schema_languages[$language_id]) && $schema_languages[$language_id]) {

      // Convert zk-hans to zk_hans.
      $dictionaries[] = str_replace('-', '_', $language_id);
    }
  }
  if ($dictionaries) {
    $spellcheck
      ->setDictionary($dictionaries);
  }
  else {
    $spellcheck
      ->setDictionary(LanguageInterface::LANGCODE_NOT_SPECIFIED);
  }
  if (!empty($spellcheck_options['keys'])) {
    $spellcheck
      ->setQuery(implode(' ', $spellcheck_options['keys']));
  }
  if (!empty($spellcheck_options['count'])) {
    $spellcheck
      ->setCount($spellcheck_options['count']);
  }
  if (!empty($spellcheck_options['collate'])) {
    $spellcheck
      ->setCollate($spellcheck_options['collate']);
  }
}