Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use Drupal\search_api\Query\QueryInterface;
- use Drupal\search_api\SearchApiException;
- use Drupal\search_api_autocomplete\Entity\SearchApiAutocompleteSearch;
- use Drupal\search_api_autocomplete\Suggestion;
- trait SolrBackendImprovedTrait {
- public function supportsFeature($feature) {
- return $feature === 'search_api_autocomplete' ? $feature : parent::supportsFeature($feature); // TODO: Change the autogenerated stub
- }
- public function getAutocompleteSuggestions(QueryInterface $query, SearchApiAutocompleteSearch $search, $incomplete_key, $user_input) {
- $suggestions = [];
- try {
- $query->keys($incomplete_key);
- $query->setOption('search_api_spellcheck', 'true');
- $query->setOption('search_api_spellcheck.count', 1);
- $this->search($query);
- $response = $query->getResults();
- if (!empty($response->getAllExtraData()['search_api_solr_response']['spellcheck']['suggestions'])) {
- $variants = $response->getAllExtraData()['search_api_solr_response']['spellcheck']['suggestions'];
- $replace = array();
- while ($variants) {
- $word = array_shift($variants);
- $replace[$word] = array_shift($variants)['suggestion'][0];
- }
- $corrected = str_ireplace(array_keys($replace), array_values($replace), $user_input);
- if ($corrected != $user_input) {
- $suggestion = new Suggestion(null, '', $this->t('Did you mean:'), '', $corrected);
- array_unshift($suggestions, $suggestion);
- }
- }
- } catch (SearchApiException $e) {
- watchdog_exception('search_api_solr', $e, "%type during autocomplete Solr query: !message in %function (line %line of %file).", array(), WATCHDOG_WARNING);
- }
- return $suggestions;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement