diff --git a/sites/all/modules/contrib/apachesolr/apachesolr.module b/sites/all/modules/contrib/apachesolr/apachesolr.module
index c71ed95..6e721a8 100644
--- a/sites/all/modules/contrib/apachesolr/apachesolr.module
+++ b/sites/all/modules/contrib/apachesolr/apachesolr.module
@@ -1035,6 +1035,12 @@ function apachesolr_get_solr($env_id = NULL) {
 function apachesolr_load_all_environments() {
   $environments = &drupal_static(__FUNCTION__);
 
+  // Sometimes this function is called when the 'apachesolr_index_bundles' is
+  // not created yet.
+  if (!db_table_exists('apachesolr_index_bundles')) {
+    return array();
+  }
+
   if (isset($environments)) {
     return $environments;
   }
@@ -1044,24 +1050,22 @@ function apachesolr_load_all_environments() {
     $environments = $cache->data;
   }
   else {
-    $environments = db_query('SELECT * FROM {apachesolr_environment}')->fetchAllAssoc('env_id', PDO::FETCH_ASSOC);
-    foreach ($environments as $id => &$environment) {
-      $environment['conf'] = array();
-      $conf = db_query('SELECT name, value FROM {apachesolr_environment_variable} WHERE env_id = :env_id', array('env_id' => $id))->fetchAllAssoc('name', PDO::FETCH_ASSOC);
-      foreach ($conf as $name => $data) {
-        $environment['conf'][$name] = unserialize($data['value']);
-      }
-    }
-
+    // If ctools is available use it's crud functions to load the environments.
     if (module_exists('ctools')) {
       ctools_include('export');
-      $defaults = ctools_export_load_object('apachesolr_environment', 'all');
-      foreach ($defaults as $env_id => $default) {
-        if (!isset($environments[$env_id])) {
-          $environments[$env_id] = (array) $default;
-        }
+      $environments = ctools_export_load_object('apachesolr_environment', 'all');
+      // Convert environments to array.
+      foreach ($environments as &$environment) {
+        $environment = (array) $environment;
       }
     }
+    else {
+      $environments = db_query('SELECT * FROM {apachesolr_environment}')->fetchAllAssoc('env_id', PDO::FETCH_ASSOC);
+    }
+
+    // Load conf and index bundles. We don't use 'subrecords callback' property
+    // of ctools export API.
+    apachesolr_environment_load_subrecords($environments);
 
     cache_set('apachesolr:environments', $environments, 'cache_apachesolr');
   }
@@ -1703,15 +1707,8 @@ function apachesolr_entity_info_alter(&$entity_info) {
  *   The bundles that should be indexed.
  */
 function apachesolr_get_index_bundles($env_id, $entity_type) {
-  // This could be called before the schema is installed so it is wise to
-  // Verify this
-  if (db_table_exists('apachesolr_index_bundles')) {
-    return db_query("SELECT bundle FROM {apachesolr_index_bundles} WHERE env_id = :env_id AND entity_type = :entity_type", array(
-      ':env_id' => $env_id,
-      ':entity_type' => $entity_type,
-      ))->fetchCol();
-  }
-  return array();
+  $environment = apachesolr_environment_load($env_id);
+  return !empty($environment['index_bundles'][$entity_type]) ? $environment['index_bundles'][$entity_type] : array();
 }
 
 /**
@@ -2422,6 +2419,60 @@ function theme_apachesolr_settings_title($vars) {
 }
 
 /**
+ * Export callback to load the view subrecords, which are the index bundles.
+ */
+function apachesolr_environment_load_subrecords(&$environments) {
+  // This could be called before the schema is installed so it is wise to
+  // Verify this
+  if (db_table_exists('apachesolr_index_bundles')) {
+    $all_index_bundles = db_select('apachesolr_index_bundles', 'ib')
+      ->fields('ib', array('env_id', 'entity_type', 'bundle'))
+      ->condition('env_id', array_keys($environments), 'IN')
+      ->orderBy('env_id')
+      ->orderBy('entity_type')
+      ->orderBy('bundle')
+      ->execute()
+      ->fetchAll(PDO::FETCH_ASSOC);
+
+    $all_index_bundles_keyed = array();
+    foreach ($all_index_bundles as $env_info) {
+      extract($env_info);
+      $all_index_bundles_keyed[$env_id][$entity_type][] = $bundle;
+    }
+  }
+
+  $all_variables = db_select('apachesolr_environment_variable', 'v')
+    ->fields('v', array('env_id', 'name', 'value'))
+    ->condition('env_id', array_keys($environments), 'IN')
+    ->orderBy('env_id')
+    ->orderBy('name')
+    ->orderBy('value')
+    ->execute()
+    ->fetchAll(PDO::FETCH_ASSOC);
+
+  $variables = array();
+  foreach ($all_variables as $variable) {
+    extract($variable);
+    $variables[$env_id][$name] = unserialize($value);
+  }
+
+  foreach ($environments as $env_id => &$environment) {
+    $index_bundles = !empty($all_index_bundles_keyed[$env_id]) ? $all_index_bundles_keyed[$env_id] : array();
+    $conf = !empty($variables[$env_id]) ? $variables[$env_id] : array();
+    if (is_array($environment)) {
+      // Environment is an array.
+      $environment['index_bundles'] = (empty($environment['index_bundles']) || !empty($index_bundles)) ? $index_bundles : $environment['index_bundles'];
+      $environment['conf'] = (empty($environment['conf']) || !empty($conf)) ? $conf : $environment['conf'];
+    }
+    elseif (is_object($environment)) {
+      // Environment is an object.
+      $environment->index_bundles = (empty($environment->index_bundles) || !empty($index_bundles)) ? $index_bundles : $environment->index_bundles;
+      $environment->conf = (empty($environment->conf) || !empty($conf)) ? $conf : $environment->conf;
+    }
+  }
+}
+
+/**
  * Callback for saving Apache Solr environment CTools exportables.
  *
  * CTools uses objects, while Apache Solr uses arrays; turn CTools value into an
@@ -2464,7 +2515,45 @@ function apachesolr_ctools_environment_export($environment, $indent) {
   // Re-load the enviroment, since in some cases the conf
   // is stripped since it's not in the actual schema.
   $environment = (object) apachesolr_environment_load($environment->env_id);
+
+  $index_bundles = array();
+  foreach (entity_get_info() as $type => $info) {
+    if ($bundles = apachesolr_get_index_bundles($environment->env_id, $type)) {
+      $index_bundles[$type] = $bundles;
+    }
+  }
   $additions_top = array();
-  $additions_bottom = array('conf' => $environment->conf);
+  $additions_bottom = array('conf' => $environment->conf, 'index_bundles' => $index_bundles);
   return ctools_export_object('apachesolr_environment', $environment, $indent, NULL, $additions_top, $additions_bottom);
 }
+
+/**
+ * Implements hook_features_revert().
+ *
+ * Delete index bundles when reseting a feature.
+ */
+function apachesolr_environment_features_revert($module_name) {
+  $environments = module_invoke($module_name, 'apachesolr_environments');
+
+  if (empty($environments)) {
+    return;
+  }
+
+  foreach ($environments as $environment) {
+    // Delete environment from database to have it load from code next time.
+    apachesolr_environment_delete($environment->env_id);
+
+    // Delete all index bundles for this environment.
+    db_delete('apachesolr_index_bundles')
+      ->condition('env_id', $environment->env_id)
+      ->execute();
+
+    // Set bundles to how they are on code.
+    if (!empty($environment->index_bundles)) {
+      module_load_include('inc', 'apachesolr', 'apachesolr.index');
+      foreach ($environment->index_bundles as $entity_type => $bundles) {
+        apachesolr_index_set_bundles($environment->env_id, $entity_type, $bundles);
+      }
+    }
+  }
+}
