You are here

public function SearchApiMoreLikeThis::query in Search API 8

Set up the query for this argument.

The argument sent may be found at $this->argument.

Overrides SearchApiStandard::query

File

src/Plugin/views/argument/SearchApiMoreLikeThis.php, line 68

Class

SearchApiMoreLikeThis
Defines a contextual filter for displaying a "More Like This" list.

Namespace

Drupal\search_api\Plugin\views\argument

Code

public function query($group_by = FALSE) {
  try {
    $server = $this->query
      ->getIndex()
      ->getServerInstance();
    if (!$server
      ->supportsFeature('search_api_mlt')) {
      $backend_id = $server
        ->getBackendId();
      $this
        ->getLogger()
        ->error('The search backend "@backend_id" does not offer "More like this" functionality.', [
        '@backend_id' => $backend_id,
      ]);
      $this->query
        ->abort();
      return;
    }
    $fields = $this->options['fields'] ?? [];
    if (!$fields) {
      $fields = array_keys($this->query
        ->getIndex()
        ->getFields());
    }
    $mlt = [
      'id' => $this->argument,
      'fields' => $fields,
    ];
    $this->query
      ->getSearchApiQuery()
      ->setOption('search_api_mlt', $mlt);
  } catch (SearchApiException $e) {
    $this->query
      ->abort($e
      ->getMessage());
  }
}