You are here

public function apachesolr_views_query::execute in Apache Solr Views 6

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

Executes the query and fills the associated view object with according values.

Values to set: $view->result, $view->total_rows, $view->execute_time, $view->pager['current_page'].

Overrides views_plugin_query::execute

File

./apachesolr_views_query.inc, line 306

Class

apachesolr_views_query
Class for handling a view that gets its data not from the database, but from a Solr server.

Code

public function execute(&$view) {
  $start_time = views_microtime();
  $view->result = array();
  $this->_view_arguments = $view->argument;
  $this->_view_filters = $view->filter;
  $this->_base_path = $view
    ->get_path();
  $this->_current_display = $view->current_display;
  $this->_current_views_name = $view->name;

  // Let the pager modify the query to add limits.
  $this->pager
    ->pre_execute($this->_query, $args);
  try {

    // If no text query is set we set a default of all
    // @todo Check the execution flow, when does apachesolr modify query get run
    if (empty($this->_query)) {
      watchdog('Apachesolr Views', 'Warning, no query string set');
    }
    $response = $this->_solr_service
      ->search($this->_query, $this->offset, $this->limit, $this->_params);
    $view->total_rows = $total = $response->response->numFound;

    // The response is cached so that it is accessible to the blocks and
    // anything else that needs it beyond the initial search.
    // TODO: allow this to be configurable on the Views UI
    if (!empty($this->_base_path)) {
      apachesolr_static_response_cache($response);
      apachesolr_has_searched(TRUE);
      apachesolr_current_query($this);
    }
    if ($total > 0) {
      $results = $response->response->docs;

      // Process dates
      $date_fields = array(
        'created',
        'changed',
      );
      foreach (array_values($date_fields) as $field) {
        if (empty($this->_used_fields[$field])) {
          unset($date_fields[$field]);
        }
      }
      if (!empty($date_fields)) {
        foreach ($results as $doc) {
          foreach ($date_fields as $field) {
            $doc->{$field} = strtotime($doc->{$field});
          }
        }
      }
      $base_fields = array();
      foreach ($results as $doc) {
        $base_fields[] = $doc->{$this->base_field};
      }

      // If there has been SQL fields added via add_field(), run a SQL query.
      if ($this->has_sql_fields) {
        $sql_table = $this->_db_query
          ->ensure_table($this->sql_base_table);
        $replace = array_fill(0, sizeof($base_fields), '%d');
        $in = '(' . implode(", ", $replace) . ')';
        $this->_db_query
          ->add_where(0, "{$sql_table}.{$this->base_field} IN {$in}", $base_fields);
        $base_alias = $this->_db_query
          ->add_field($sql_table, $this->base_field);
        $sql_query = $this->_db_query
          ->query();
        $sql_args = $this->_db_query
          ->get_where_args();
        $sql_result = db_query($sql_query, $sql_args);

        // Merge results from the SQL query into the $result set.
        while ($sql_row = db_fetch_object($sql_result)) {
          foreach ($results as $key => $doc) {
            if ($doc->{$this->base_field} == $sql_row->{$base_alias}) {
              foreach ($sql_row as $field => $value) {
                $results[$key]->{$field} = $value;
              }
            }
          }
        }
      }
      $view->result = $results;
    }
  } catch (Exception $e) {
    watchdog('Apache Solr', $e
      ->getMessage(), NULL, WATCHDOG_ERROR);
    apachesolr_failure(t('Solr search'), is_null($query) ? $this->_keys : $query
      ->get_query_basic());
  }
  $this->pager->total_items = $view->total_rows;
  $this->pager
    ->update_page_info();
  $this->pager
    ->post_execute($view->result);
  $view->execute_time = views_microtime() - $start_time;
}