--- modules/views_data_export/plugins/views_data_export_plugin_display_export.inc	2013-04-02 15:28:02 +0000
+++ modules/views_data_export/plugins/views_data_export_plugin_display_export.inc	2013-04-03 14:05:30 +0000
@@ -477,21 +477,32 @@
           // TODO: Handle external databases.
           $result = db_query_range('SELECT * FROM {' . $this->index_tablename() . '} ORDER BY ' . $this->batched_execution_state->sandbox['weight_field_alias'] . ' ASC', 0, $this->get_option('segment_size'));
           $this->view->result = array();
-          foreach ($result as $item_hashed) {
-            $item = new stdClass();
-            // We had to shorten some of the column names in the index, restore
-            // those now.
-            foreach ($item_hashed as $hash => $value) {
-              if (isset($this->batched_execution_state->sandbox['field_aliases'][$hash])) {
-                $item->{$this->batched_execution_state->sandbox['field_aliases'][$hash]} = $value;
-              }
-              else {
-                $item->{$hash} = $value;
-              }
-            }
-            // Push the restored $item in the views result array.
-            $this->view->result[] = $item;
-          }
+          $query_plugin = get_class($this->view->query);
+          if ($query_plugin == 'views_plugin_query_default') {
+            foreach ($result as $item_hashed) {
+              $item = new stdClass();
+              // We had to shorten some of the column names in the index, restore
+              // those now.
+              foreach ($item_hashed as $hash => $value) {
+                if (isset($this->batched_execution_state->sandbox['field_aliases'][$hash])) {
+                  $item->{$this->batched_execution_state->sandbox['field_aliases'][$hash]} = $value;
+                }
+                else {
+                  $item->{$hash} = $value;
+                }
+              }
+              // Push the restored $item in the views result array.
+              $this->view->result[] = $item;
+            }
+          }
+          elseif ($query_plugin == 'SearchApiViewsQuery') {
+            foreach ($result as $row) {
+              $item = unserialize($row->data);
+              $item->{$this->batched_execution_state->sandbox['weight_field_alias']} = $row->{$this->batched_execution_state->sandbox['weight_field_alias']};
+              $this->view->result[] = $item;
+            }
+          }
+
           $this->view->_post_execute();
           break;
       }
@@ -586,13 +597,17 @@
     
       // Replace the query object.
       $view->query = $query;
+      
+      $view->execute();
+    }
+    elseif ($query_plugin == 'SearchApiViewsQuery') {
+      $this->store_search_api_result(clone($view));
     }
     else {
       exit(0);
       //TODO Make it work for other query plugins.      
     }
     
-    $view->execute();
   }
 
   /**
@@ -768,6 +783,56 @@
     $conn_info = Database::getConnectionInfo($name);
     return $conn_info['default']['driver'];
   }
+  
+  /**
+   * Based on views_data_export_plugin_query_default_batched::execute().
+   */
+  function store_search_api_result($view) {
+    $display_handler = &$view->display_handler;
+    $start = microtime(TRUE);
+
+    try {
+      
+      // Get all the view results.
+      $view->query->set_limit(NULL);
+      $view->query->set_offset(0);
+      $view->query->execute($view);
+      $weight_alias = 'vde_weight';
+      $display_handler->batched_execution_state->sandbox['weight_field_alias'] = $weight_alias;
+ 
+      $schema = array(
+        'fields' => array(
+          $weight_alias => array('type' => 'int'),
+          'data' => array('type' => 'blob'),
+      ));
+
+      db_create_table($display_handler->index_tablename(), $schema);
+      
+      if (!empty($view->result)) {
+        $insert_query = db_insert($display_handler->index_tablename())->fields(array($weight_alias, 'data'));
+        $weight = 0;
+        foreach ($view->result as $item) {
+          $insert_query->values(array(
+            $weight_alias => $weight,
+            'data' => serialize($item),
+          ));
+          $weight++;
+        }
+        $insert_query->execute();
+      }
+
+      $view->result = array();
+      // Now create an index for the weight field, otherwise the queries on the
+      // index will take a long time to execute.
+      db_add_unique_key($display_handler->index_tablename(), $weight_alias, array($weight_alias));
+    }
+    catch (Exception $e) {
+      $view->result = array();
+      debug('Exception: ' . $e->getMessage());
+    }
+    
+    $view->execute_time = microtime(TRUE) - $start;
+  }
 }
 
 class views_data_export_plugin_query_default_batched extends views_plugin_query_default {
