Read-only Apache Solr search index

When you are making a database dump from a production server and copying it to a staging website or a development environment, it can be useful to set the development site to have a read-only search index. In order to do this, you'll need to make some changes in Drupal.

Apache Solr module (Drupal 6, 7)

Find your website's settings.php file (commonly located in the sites/default/ directory), and then after the acquia_require line, add the following text:

$conf['apachesolr_read_only'] = "1";

Adding this line to the configuration file is useful because you won't have to remember to set it every time a database dump is made.

Making dev and staging read-only

If you have Solr running on your dev and staging environments on Acquia Cloud, you may end up with duplicate search results if all of the environments are set to the read-write mode. You can use this code to conditionally check if you're on production, and if not, make the Solr index read-only. This way you can still test your search, but you won't add non-production content to the index.


if (isset($_ENV['AH_SITE_ENVIRONMENT'])) {
  if ($_ENV['AH_SITE_ENVIRONMENT'] != 'prod') {
    // For sites that are hosted in Acquia Cloud and are not the production instance
    $conf['apachesolr_read_only'] = "1";
  }
}
else {
  // For sites that aren't hosted in Acquia Cloud (like a local installation)
  $conf['apachesolr_read_only'] = "1";
}

If you are running the 7.x-1.x or 6.x-3.x versions of the Apache Solr Search module, under certain use cases it's a logical and recommended practice to set read-only mode — but there are potential issues to be aware of before doing so. For example, if you have more than one environment and want to have some remain read-write, while others are read-only, then you would use the following method.

If you have a given environment acquia_search_server_1:


if (isset($_ENV['AH_SITE_ENVIRONMENT'])) {
  if ($_ENV['AH_SITE_ENVIRONMENT'] != 'prod') { 
    // For websites that are hosted in Acquia Cloud and are not the production instance 
    $conf['apachesolr_environments']['acquia_search_server_1']['conf']['apachesolr_read_only'] = '1';
  }
}
else
{ 
  // For websites that aren't hosted in Acquia Cloud (such as a local installation)
  $conf['apachesolr_environments']['acquia_search_server_1']['conf']['apachesolr_read_only'] = '1';
}

Search API

For Search API Solr, it is easiest to manually set the index status to Read-Only using the check box in the user interface.

You can also set this flag via code, which should be implemented in a custom module. The following example code (for Drupal 7 only) It will automatically mark any Search API Indexes as read only whenever the code is not running on an Acquia-Hosted & production environment.:


/**
 * Implements hook_search_api_index_load()
 *
 * Forces read-only mode on Search API indexes if the current environment
 * is NOT an Acquia-Hosted production environment. (Including a local copy
 * of this site)
 * 
 * This code should go inside a custom module, changing "MYMODULE" to the 
 * module's name.
 */
 function MYMODULE_search_api_index_load($indexes) {
  // Assume that we need to override and force read-only mode.
  $set_read_only = TRUE;
  
  // Avoid forcing index read-only mode, if on Acquia Hosting + in a production environment
  if (isset($_ENV["AH_SITE_ENVIRONMENT"]) && isset($_ENV["AH_PRODUCTION"])) {
    return;
  }
  
  // Cycle through all Search API Solr indexes and force to read_only mode.
  if (!empty($set_read_only)) {
    foreach ($indexes as &$index) {
      $index->read_only = "1";
    }
  }
}

Contact supportStill need assistance? Contact Acquia Support