Loading...


Related Products


Date Published: January 26, 2024

Preventing Pollution of Solr Search Data Between Environments

When you are making a database backup from a Production server by copying it to a local or staging environment, it can be useful to set the non-Production site to have a read-only search index so the Production index isn't polluted with data from other environments. In order to do this effectively, you'll need to make some changes in your codebase.

Acquia Search 3.1.x (Drupal 10)

You can add the below code to settings.local.php or as a starting point for a custom module that leverages the read_only setting via Acquia Search.

<?php
// Check if Acquia environment variable is not set or equals 'ide'.
if (!isset($_ENV['AH_SITE_ENVIRONMENT']) || $_ENV['AH_SITE_ENVIRONMENT'] === 'ide') {
  // If there's no 'AH_SITE_ENVIRONMENT' or we are on an Acquia Cloud IDE,
  // set Acquia Search to read-only mode.
  $settings['acquia_search']['read_only'] = TRUE;
}

Another option is to override the search indexes per environment (also, in settings.local.php or in a custom module):

<?php
// Check that Acquia environment variable is set
if (isset($_ENV['AH_SITE_ENVIRONMENT'])) {
  switch ($_ENV['AH_SITE_ENVIRONMENT']) {
    case 'dev':
      // use the search index from dev
      $settings['acquia_search']['override_search_core'] = 'ABCD-123456.dev.appname';
      break;
    case 'test':
      // use the search index from test
      $settings['acquia_search']['override_search_core'] = 'ABCD-123456.test.appname';
      break;
    case 'prod':
      // use the search index from prod
      // Site Factory may require a different value depending on site configuration
      $settings['acquia_search']['override_search_core'] = 'ABCD-123456.prod.appname';
      break;
    default:
      // All other Acquia environments (ide, cde/ode, etc.), get read-only from dev
      $settings['acquia_search']['read_only'] = TRUE;
      $settings['acquia_search']['override_search_core'] = 'ABCD-123456.prod.appname';
   }
} else {
  // Not an Acquia environment (local dev), so they get read-only from dev as well
  $settings['acquia_search']['read_only'] = TRUE;
  $settings['acquia_search']['override_search_core'] = 'ABCD-123456.dev.appname';
}

 

Search API (Drupal 7)

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";
    }
  }
}



Apache Solr module (Drupal 7)

Note: The read-only check box in the Apache Solr interface does not set this variable in the settings.php file.

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 Stage read-only

If you have Solr running on your Dev and Stage 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";
}
 

ADVANCED - Incorrect use of the following content can cause damage to your environment.

If you are running the 7.x-1.x version 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.

Important

This code can result in these overrides being made permanent on your database under some cases, such as going to the settings form and clicking Save. Using the first method avoids this problem by using a variable that is not available from the user interface, but still controls read-write/read-only mode.

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';
}
 

Did not find what you were looking for?

If this content did not answer your questions, try searching or contacting our support team for further assistance.

Back to Section navigation
Back to Site navigation