0

How can I use the function getAutocompleteSuggestions() in Drupal 8? In Drupal 7 you could do something like this:

$query->getIndex()->server()->getAutocompleteSuggestions($query, $this->getSearch(), $incomplete_key, $user_input);

Thank you for your help!

1

1 Answer 1

1

I've solved it with Solarium

//...
use Solarium\Client;
//...

// $value is the search value from the textfield. 
if (!empty($value)) {
    $searchAPIconfig = \Drupal::config('search_api.server.server');
    $searchAPIconfigData = $searchAPIconfig->getRawData();

    $config = array(
    'endpoint' => array(
      'localhost' => array(
        'host' => $searchAPIconfigData['backend_config']['host'],
        'port' => $searchAPIconfigData['backend_config']['port'],
        'path' => $searchAPIconfigData['backend_config']['path'],
      ),
    ),
    );

    $client = new Client($config);

    if ($client !== NULL) {
        $query = $client->createSuggester();
        $query->setQuery($value);
        $query->setDictionary('mySuggester');
        $query->setOnlyMorePopular(TRUE);
        $query->setCount(10);
        $query->setCollate(TRUE);

        $resultset = $client->suggester($query)->getData();
        $results = $resultset['suggest']['mySuggester'][$value]['suggestions'];
        $suggestions = array();
        foreach ($results as $key => $termResult) {
          $suggestions[] = $termResult['term'];
        }

        //...
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.