You are here

protected function SearchApiSolrBackend::getAutocompleteTermSuggestions in Search API Solr 4.x

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::getAutocompleteTermSuggestions()
  2. 8.2 src/Plugin/search_api/backend/SearchApiSolrBackend.php \Drupal\search_api_solr\Plugin\search_api\backend\SearchApiSolrBackend::getAutocompleteTermSuggestions()

Get the term suggestions from the autocomplete query result.

Parameters

\Solarium\Core\Query\Result\ResultInterface $result: An autocomplete query result.

\Drupal\search_api_autocomplete\Suggestion\SuggestionFactory $suggestion_factory: The suggestion factory.

string $incomplete_key: The start of another fulltext keyword for the search, which should be completed.

Return value

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

1 call to SearchApiSolrBackend::getAutocompleteTermSuggestions()
SearchApiSolrBackend::getAutocompleteSuggestions in src/Plugin/search_api/backend/SearchApiSolrBackend.php
Implements autocomplete compatible to AutocompleteBackendInterface.

File

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

Class

SearchApiSolrBackend
Apache Solr backend for search api.

Namespace

Drupal\search_api_solr\Plugin\search_api\backend

Code

protected function getAutocompleteTermSuggestions(ResultInterface $result, SuggestionFactory $suggestion_factory, $incomplete_key) {
  $suggestions = [];
  if ($terms_results = $result
    ->getComponent(ComponentAwareQueryInterface::COMPONENT_TERMS)) {
    $autocomplete_terms = [];
    foreach ($terms_results as $fields) {
      foreach ($fields as $term => $count) {
        $autocomplete_terms[$term] = $count;
      }
    }
    foreach ($autocomplete_terms as $term => $count) {
      $suggestion_suffix = mb_substr($term, mb_strlen($incomplete_key));
      $suggestions[] = $suggestion_factory
        ->createFromSuggestionSuffix($suggestion_suffix, $count);
    }
  }
  return $suggestions;
}