You are here

public function SearchApiElasticsearchBackend::getAutocompleteSuggestions in Elasticsearch Connector 8

Same name and namespace in other branches
  1. 8.7 src/Plugin/search_api/backend/SearchApiElasticsearchBackend.php \Drupal\elasticsearch_connector\Plugin\search_api\backend\SearchApiElasticsearchBackend::getAutocompleteSuggestions()
  2. 8.5 src/Plugin/search_api/backend/SearchApiElasticsearchBackend.php \Drupal\elasticsearch_connector\Plugin\search_api\backend\SearchApiElasticsearchBackend::getAutocompleteSuggestions()
  3. 8.6 src/Plugin/search_api/backend/SearchApiElasticsearchBackend.php \Drupal\elasticsearch_connector\Plugin\search_api\backend\SearchApiElasticsearchBackend::getAutocompleteSuggestions()

Helper function. Get Autocomplete suggestions.

Parameters

QueryInterface $query:

SearchApiAutocompleteSearch $search:

string $incomplete_key:

string $user_input:

File

src/Plugin/search_api/backend/SearchApiElasticsearchBackend.php, line 1466
Contains the SearchApiElasticsearchBackend object.

Class

SearchApiElasticsearchBackend
Plugin annotation @SearchApiBackend( id = "elasticsearch", label = @Translation("Elasticsearch"), description = @Translation("Index items using an Elasticsearch server.") )

Namespace

Drupal\elasticsearch_connector\Plugin\search_api\backend

Code

public function getAutocompleteSuggestions(QueryInterface $query, SearchApiAutocompleteSearch $search, $incomplete_key, $user_input) {
  $suggestions = array();

  // Turn inputs to lower case, otherwise we get case sensitivity problems.
  $incomp = \Unicode::strtolower($incomplete_key);
  $index = $query
    ->getIndex();
  $index_fields = $this
    ->getIndexFields($query);
  $complete = $query
    ->getOriginalKeys();
  $query
    ->keys($user_input);
  try {

    // TODO: Make autocomplete to work as autocomplete instead of exact string
    // match.
    $response = $this
      ->search($query);
  } catch (\Exception $e) {
    watchdog('Elasticsearch Search API', String::checkPlain($e
      ->getMessage()), array(), WATCHDOG_ERROR);
    return array();
  }
  $matches = array();
  if (isset($response['results'])) {
    $items = $index
      ->loadItems(array_keys($response['results']));
    foreach ($items as $id => $item) {
      $node_title = $index
        ->datasource()
        ->getItemLabel($item);
      $matches[$node_title] = $node_title;
    }
    if ($matches) {

      // Eliminate suggestions that are too short or already in the query.
      foreach ($matches as $name => $node_title) {
        if (drupal_strlen($name) < 3 || isset($keys_array[$name])) {
          unset($matches[$name]);
        }
      }

      // The $count in this array is actually a score. We want the
      // highest ones first.
      arsort($matches);

      // Shorten the array to the right ones.
      $additional_matches = array_slice($matches, $limit - count($suggestions), NULL, TRUE);
      $matches = array_slice($matches, 0, $limit, TRUE);
      foreach ($matches as $node => $name) {
        $suggestions[] = $name;
      }
    }
    $keys = trim($keys . ' ' . $incomplete_key);
    return $suggestions;
  }
}