You are here

public function SearchApiSolrBackend::getSuggesterSuggestions 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::getSuggesterSuggestions()
  2. 4.x src/Plugin/search_api/backend/SearchApiSolrBackend.php \Drupal\search_api_solr\Plugin\search_api\backend\SearchApiSolrBackend::getSuggesterSuggestions()

Autocompletion suggestions for some user input using Suggester component.

Parameters

\Drupal\search_api\Query\QueryInterface $query: A query representing the base search, with all completely entered words in the user input so far as the search keys.

\Drupal\search_api_autocomplete\SearchInterface $search: An object containing details about the search the user is on, and settings for the autocompletion. See the class documentation for details. Especially $search->getOptions() should be checked for settings, like whether to try and estimate result counts for returned suggestions.

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.

array $options: 'dictionary' as string, 'context_filter_tags' as array of strings.

Return value

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

Overrides SolrAutocompleteInterface::getSuggesterSuggestions

File

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

Class

SearchApiSolrBackend
Apache Solr backend for search api.

Namespace

Drupal\search_api_solr\Plugin\search_api\backend

Code

public function getSuggesterSuggestions(QueryInterface $query, $search, $incomplete_key, $user_input, $options = []) {
  $suggestions = [];
  if ($solarium_query = $this
    ->getAutocompleteQuery($incomplete_key, $user_input)) {
    try {
      $suggestion_factory = new SuggestionFactory($user_input);
      $this
        ->setAutocompleteSuggesterQuery($query, $solarium_query, $user_input, $options);

      // Call an object oriented equivalent to hook_search_api_solr_query_alter().
      $this
        ->alterSuggesterAutocompleteQuery($solarium_query, $query);
      $result = $this
        ->getSolrConnector()
        ->execute($solarium_query);
      $suggestions = $this
        ->getAutocompleteSuggesterSuggestions($result, $suggestion_factory);

      // Filter out duplicate suggestions.
      $this
        ->filterDuplicateAutocompleteSuggestions($suggestions);
    } catch (SearchApiException $e) {
      watchdog_exception('search_api_solr', $e);
    }
  }
  return $suggestions;
}