You are here

public function SearchApiSearch::getResults in Fast Autocomplete 8

Return the results for the given key.

Parameters

\Drupal\fac\FacConfigInterface $fac_config: The FacConfig entity.

string $langcode: The language code.

string $key: The query string to get results for.

Return value

array The result entity ids for the given key.

Overrides SearchBase::getResults

File

src/Plugin/Search/SearchApiSearch.php, line 256

Class

SearchApiSearch
Provides a Search API search plugin.

Namespace

Drupal\fac\Plugin\Search

Code

public function getResults(FacConfigInterface $fac_config, $langcode, $key) {
  $results = [];
  $plugin_config = $fac_config
    ->getSearchPluginConfig();
  try {

    /* @var \Drupal\search_api\IndexInterface $index */
    $index = $this->entityTypeManager
      ->getStorage('search_api_index')
      ->load($plugin_config['index']);
  } catch (InvalidPluginDefinitionException $e) {
    return $results;
  }
  $query = $this->queryHelper
    ->createQuery($index);
  $query
    ->addTag('fac');
  $query
    ->addTag('fac_' . $fac_config
    ->id());
  $query
    ->keys($key);
  if ($plugin_config['text_fields']) {
    $query
      ->setFulltextFields($plugin_config['text_fields']);
  }
  if ($plugin_config['language_filter']) {
    $langcodes = array_filter($plugin_config['langcode_includes']);
    $langcodes[] = $langcode;
    $query
      ->setLanguages($langcodes);
  }
  $query
    ->range(0, $fac_config
    ->getNumberOfResults());
  $query
    ->sort($plugin_config['sort_field'], $plugin_config['sort_direction']);
  try {
    $items = $query
      ->execute()
      ->getResultItems();
    foreach ($items as $item) {
      $entity = $item
        ->getOriginalObject()
        ->getValue();
      $results[] = [
        'entity_type' => $entity
          ->getEntityTypeId(),
        'entity_id' => $entity
          ->id(),
      ];
    }
  } catch (SearchApiException $e) {
    return $results;
  }
  return $results;
}