You are here

public function elasticsearch_connector_views_query::execute in Elasticsearch Connector 7

Same name and namespace in other branches
  1. 7.5 modules/elasticsearch_connector_views/elasticsearch_connector_views_query.inc \elasticsearch_connector_views_query::execute()
  2. 7.2 modules/elasticsearch_connector_views/elasticsearch_connector_views_query.inc \elasticsearch_connector_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

modules/elasticsearch_connector_views/elasticsearch_connector_views_query.inc, line 139
Class for handling a view that gets its data not from the database, but from a Solr server.

Class

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

Code

public function execute(&$view) {
  $view->result = array();
  $view->total_rows = 0;
  $view->execute_time = 0;
  $base_table_parts = explode('__', $view->base_table);
  $cluster_id = $base_table_parts[1];
  $index = $base_table_parts[2];
  $type = $base_table_parts[3];
  try {
    $start = microtime(TRUE);
    $client = elasticsearch_connector_get_client_by_id($cluster_id);
    if ($client) {
      $view->execute_time = microtime(TRUE) - $start;

      // Execute the search.
    }

    // Execute search.
    $response = $client
      ->search(array(
      'index' => $index,
      'type' => $type,
      'body' => $this->query_params,
    ));

    // Store results.
    if (!empty($response['hits']['hits'])) {
      foreach ($response['hits']['hits'] as $doc) {
        $result_doc = array();
        foreach ($doc['fields'] as $field_name => $field_value) {

          // Handle multivalue with concatenation for now.
          $result_doc[$field_name] = implode(' | ', $field_value);
        }
        $result_doc['_source'] = $doc['_source'];
        $view->result[] = (object) $result_doc;
      }
    }

    // Store response into the object.
    $this->response = $response;

    // Store the results.
    $this->pager->total_items = $view->total_rows = $response['hits']['total'];
    $this->pager
      ->update_page_info();

    // We shouldn't use $results['performance']['complete'] here, since
    // extracting the results probably takes considerable time as well.
    $view->execute_time = $response['took'];
  } catch (Exception $e) {
    $this->errors[] = $e
      ->getMessage();
  }
  if ($this->errors) {
    foreach ($this->errors as $msg) {
      drupal_set_message(check_plain($msg), 'error');
    }
    $view->result = array();
    $view->total_rows = 0;
    $view->execute_time = 0;
    return;
  }
}