You are here

public function apachesolr_views_query::execute in Apache Solr Views 7

Same name and namespace in other branches
  1. 6 apachesolr_views_query.inc \apachesolr_views_query::execute()

Executes the query.

Assigns the resulting values to the view object. Values to set: $view->result, $view->total_rows, $view->execute_time.

Overrides views_plugin_query::execute

File

./apachesolr_views_query.inc, line 122
Views query plugin for Apache Solr Views. Gets its data not from the database, but from a Solr server.

Class

apachesolr_views_query
@file Views query plugin for Apache Solr Views. Gets its data not from the database, but from a Solr server.

Code

public function execute(&$view) {
  try {
    $start = microtime(TRUE);

    // Execute the search.
    // Load search query.
    // Get the Apache Solr "environment id".
    if (strpos($view->base_table, 'apachesolr__') === 0) {
      $env_id = substr($view->base_table, 12);
    }
    else {
      $env_id = apachesolr_default_environment();
    }
    $solr = apachesolr_get_solr($env_id);
    $context = array(
      'search_type' => 'apachesolr_views_query',
      'view_name' => $view->name,
      'current_display' => $view->current_display,
    );
    $query = new ApachesolrViewsSolrBaseQuery('apachesolr', $solr, $this->query_params, '', current_path(), $context, $view);

    // Add sorting. The setSolrsort method can't be used, because it doesn't support multiple sorting criteria.
    $query
      ->replaceParam('sort', $this->orderby);
    $query->page = $this->pager->current_page;

    // Boost parameters if apachesolr_search module is available.
    apachesolr_search_add_boost_params($query);

    // Execute search.
    list($final_query, $response) = apachesolr_do_query($query);
    apachesolr_has_searched($solr
      ->getId(), TRUE);
    if ($response) {

      // Store results.
      $view->result = $response->response->docs;

      // Store apachesolr cached response.
      $this->apachesolr_response = $response;

      // Store the results.
      $this->pager->total_items = $view->total_rows = $this->apachesolr_response->response->numFound;
      $this->pager
        ->update_page_info();
    }
  } catch (Exception $e) {
    $view->result = array();
    $view->total_rows = 0;
    if (!empty($view->live_preview)) {
      drupal_set_message($e
        ->getMessage(), 'error');
    }
    else {
      vpr('Exception in @human_name[@view_name]: @message', array(
        '@human_name' => $view->human_name,
        '@view_name' => $view->name,
        '@message' => $e
          ->getMessage(),
      ));
    }
  }
  $view->execute_time = microtime(TRUE) - $start;
}