You are here

public function SearchApiViewsHandlerArgumentMoreLikeThis::query in Search API 7

Set up the query for this argument.

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

Overrides SearchApiViewsHandlerArgument::query

File

contrib/search_api_views/includes/handler_argument_more_like_this.inc, line 77
Contains SearchApiViewsHandlerArgumentMoreLikeThis.

Class

SearchApiViewsHandlerArgumentMoreLikeThis
Views argument handler providing a list of related items for search servers supporting the "search_api_mlt" feature.

Code

public function query($group_by = FALSE) {
  try {
    $server = $this->query
      ->getIndex()
      ->server();
    if (!$server
      ->supportsFeature('search_api_mlt')) {
      $class = search_api_get_service_info($server->class);
      watchdog('search_api_views', 'The search service "@class" does not offer "More like this" functionality.', array(
        '@class' => $class['name'],
      ), WATCHDOG_ERROR);
      $this->query
        ->abort();
      return;
    }
    $index_fields = array_keys($this->query
      ->getIndex()->options['fields']);
    if (empty($this->options['fields'])) {
      $fields = $index_fields;
    }
    else {
      $fields = array_intersect($this->options['fields'], $index_fields);
    }
    if ($this->query
      ->getIndex()
      ->datasource() instanceof SearchApiCombinedEntityDataSourceController) {
      $id = $this->options['entity_type'] . '/' . $this->argument;
    }
    else {
      $id = $this->argument;
    }
    $mlt = array(
      'id' => $id,
      'fields' => $fields,
    );
    $this->query
      ->getSearchApiQuery()
      ->setOption('search_api_mlt', $mlt);
  } catch (SearchApiException $e) {
    $this->query
      ->abort($e
      ->getMessage());
  }
}