You are here

public function DidYouMeanSpellCheck::query in Search API Spellcheck 8.3

Throws

\InvalidArgumentException

Overrides HandlerBase::query

File

src/Plugin/views/area/DidYouMeanSpellCheck.php, line 57

Class

DidYouMeanSpellCheck
Provides an area for messages.

Namespace

Drupal\search_api_spellcheck\Plugin\views\area

Code

public function query() {
  if ($this->query instanceof SearchApiQuery && $this->query
    ->getIndex()
    ->getServerInstance()
    ->supportsFeature('search_api_spellcheck')) {
    $keys = $this->query
      ->getKeys();

    // Don't set the option if $keys is NULL.
    if ($keys) {
      if (!is_array($keys)) {
        throw new \InvalidArgumentException('The selected parse mode for fulltext fields is not compatible to Search API Spellcheck.');
      }
      $this->query
        ->setOption('search_api_spellcheck', [
        // Strip non numeric array keys like '#collation'.
        'keys' => array_filter($keys, 'is_int', ARRAY_FILTER_USE_KEY),
        // This parameter specifies the maximum number of suggestions that the
        // spellchecker should return for a term.
        'count' => $this->options['search_api_spellcheck_count'],
        // If true and the backend supports it, this parameter directs the
        // backend to take the best suggestion for each token (if one exists)
        // and construct a new query from the suggestions. For example, if the
        // input query was "jawa class lording" and the best suggestion for
        // "jawa" was "java" and "lording" was "loading", then the resulting
        // collation would be "java class loading".
        'collate' => $this->options['search_api_spellcheck_collate'],
      ]);
    }
  }
  parent::query();
}