You are here

protected function SearchApiSolrBackend::setAutocompleteSuggesterQuery in Search API Solr 8.3

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

Set the suggester parameters for the solarium autocomplete query.

Parameters

\Drupal\search_api\Query\QueryInterface $query: A query representing the completed user input so far.

\Drupal\search_api_solr\Solarium\Autocomplete\Query $solarium_query: A Solarium autocomplete query.

string $user_input: The user input.

array $options: (optional) An array of options. e.g: 'dictionary' as string, 'context_filter_tags' as array of strings.

1 call to SearchApiSolrBackend::setAutocompleteSuggesterQuery()
SearchApiSolrBackend::getSuggesterSuggestions in src/Plugin/search_api/backend/SearchApiSolrBackend.php

File

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

Class

SearchApiSolrBackend
Apache Solr backend for search api.

Namespace

Drupal\search_api_solr\Plugin\search_api\backend

Code

protected function setAutocompleteSuggesterQuery(QueryInterface $query, AutocompleteQuery $solarium_query, $user_input, array $options = []) {
  if (isset($options['context_filter_tags']) && in_array('drupal/langcode:multilingual', $options['context_filter_tags'])) {
    $langcodes = $query
      ->getLanguages();
    if ($langcodes && count($langcodes) == 1) {
      $langcode = reset($langcodes);
      $options['context_filter_tags'] = str_replace('drupal/langcode:multilingual', 'drupal/langcode:' . $langcode, $options['context_filter_tags']);
      $options['dictionary'] = $langcode;
    }
    else {
      foreach ($options['context_filter_tags'] as $key => $tag) {
        if ('drupal/langcode:multilingual' === $tag) {
          unset($options['context_filter_tags'][$key]);
          break;
        }
      }
    }
  }
  $suggester_component = $solarium_query
    ->getSuggester();
  $suggester_component
    ->setQuery($user_input);
  $suggester_component
    ->setDictionary(!empty($options['dictionary']) ? $options['dictionary'] : LanguageInterface::LANGCODE_NOT_SPECIFIED);
  if (!empty($options['context_filter_tags'])) {
    $suggester_component
      ->setContextFilterQuery(Utility::buildSuggesterContextFilterQuery($options['context_filter_tags']));
  }
  $suggester_component
    ->setCount($query
    ->getOption('limit') ?? 10);

  // The search_api_autocomplete module highlights by itself.
  $solarium_query
    ->addParam('suggest.highlight', FALSE);
}