You are here

public function Suggester::getAutocompleteSuggestions in Search API Solr 8.2

Same name and namespace in other branches
  1. 8.3 src/Plugin/search_api_autocomplete/suggester/Suggester.php \Drupal\search_api_solr\Plugin\search_api_autocomplete\suggester\Suggester::getAutocompleteSuggestions()
  2. 4.x src/Plugin/search_api_autocomplete/suggester/Suggester.php \Drupal\search_api_solr\Plugin\search_api_autocomplete\suggester\Suggester::getAutocompleteSuggestions()

Retrieves autocompletion suggestions for some user input.

For example, when given the user input "teach us", with "us" being considered incomplete, \Drupal\search_api_autocomplete\SuggestionInterface objects representing the following suggestions might be returned:

[
  [
    'prefix' => t('Did you mean:'),
    'user_input' => 'reach us',
  ],
  [
    'user_input' => 'teach us',
    'suggestion_suffix' => 'ers',
  ],
  [
    'user_input' => 'teach us',
    'suggestion_suffix' => ' swimming',
  ],
];

Parameters

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

string $incomplete_key: The start of another fulltext keyword for the search, which should be completed. Might be empty, in which case all user input up to now was considered completed. Then, additional keywords for the search could be suggested.

string $user_input: The complete user input for the fulltext search keywords so far.

Return value

\Drupal\search_api_autocomplete\Suggestion\SuggestionInterface[] An array of autocomplete suggestions.

Overrides SuggesterInterface::getAutocompleteSuggestions

File

src/Plugin/search_api_autocomplete/suggester/Suggester.php, line 109

Class

Suggester
Provides a suggester plugin that retrieves suggestions from the server.

Namespace

Drupal\search_api_solr\Plugin\search_api_autocomplete\suggester

Code

public function getAutocompleteSuggestions(QueryInterface $query, $incomplete_key, $user_input) {
  if (!($backend = static::getBackend($this
    ->getSearch()
    ->getIndex()))) {
    return [];
  }
  $config = $this
    ->getConfiguration();
  $options['context_filter_tags'] = [];
  if ($config['search_api_solr/site_hash']) {
    $options['context_filter_tags'][] = 'search_api_solr/site_hash:' . Utility::getSiteHash();
  }
  if (!empty($config['search_api/index']) && 'any' != $config['search_api/index']) {
    $options['context_filter_tags'][] = 'search_api/index:' . $config['search_api/index'];
  }
  if ('any' != $config['drupal/langcode']) {
    $options['context_filter_tags'][] = 'drupal/langcode:' . $config['drupal/langcode'];
  }
  return $backend
    ->getSuggesterSuggestions($query, $this
    ->getSearch(), $incomplete_key, $user_input, $options);
}