Information for: DEVELOPERS   PARTNERS

Overriding Solr index connection switching

Occasionally, you must override the auto switching behavior in Acquia Search for specific reasons (which can include the following):

  • You are implementing a single search page returning results from several Drupal websites. For this approach, all websites must read and write to a single Solr index and use a module such as the Apache Solr Multisite Search module to power your search pages.
  • Your subscription level isn’t entitled to several Acquia Search Solr indexes, and you must write into your single, available index from several environments.
  • You must read/write access from a local- or non-Acquia-hosted website.

Important

Sharing a single Solr index across websites has risks. For more information, see Sharing Solr indexes in read/write mode.

To stop Acquia Search from enabling auto switching, use one of the following methods:

Using code overrides to set the Acquia Search Solr connection

Acquia Search subscribers can choose to manually configure the connections in the settings.php file by overriding variables. For non-hosted subscribers, setting these variables is the only available method to use multiple Acquia Search Solr indexes.

Code override procedure

Note for Site Factory subscribers

Site Factory subscribers must modify the following provided code examples to use $GLOBALS['gardens_site_settings']['env'] instead of $_ENV['AH_SITE_ENVIRONMENT'], as the value of $_ENV['AH_SITE_ENVIRONMENT'] changes during the code deployment process.

Select the code override to use based on your installed Drupal version and the module in use, from the following tabs:

Use the acquia_search.settings.connection_override configuration object to define a connection.

Note

This requires acquia_connector-8.x-1.20 or higher.

To override the connection:

  • For Site Factory, place the following code inside a post-settings-php hook file.
  • For all other cases, you can put the following code in your settings.php file after the Acquia include statement.
/**
 * Acquia Search connection override for Drupal 8/9 using Search API Solr and Acquia Search
 * From: https://docs.acquia.com/acquia-search/multiple-cores/override/
 */
function acquia_search_override_example_20210427() {

  // EDIT THIS SECTION =================
  //
  // Edit [index ID] values according to your available Solr cores.
  $solr_core_mapping = [
    // Acquia environments.
    'prod' => '[prod index ID]',
    'test' => '[test index ID]',
    'dev' =>  '[dev index ID]',
    // Fallback Solr index to use for all other cases.
    // Including Local or other non-acquia-hosted Drupal environment
    'FALLBACK' => '[fallback index ID]'
  ];

  // END of editable part ==============

  // Choose fallback core by default.
  $chosen_core = $solr_core_mapping['FALLBACK'];

  // Try to pick a core from the list according to the current environment.
  $ah_env = @$GLOBALS['gardens_site_settings']['env'] ?: @$_ENV['AH_SITE_ENVIRONMENT'];
  if (!empty($ah_env) && isset($solr_core_mapping[$ah_env])) {
    $chosen_core = $solr_core_mapping[$ah_env];
  }

  return $chosen_core;
}
// Override the ID of the Search core to use.
// For acquia_search-8.x-2.x
$config['acquia_search.settings']['default_search_core'] = acquia_search_override_example_20210427();
// For acquia_search-8.x-3.x
$config['acquia_search.settings']['override_search_core'] = acquia_search_override_example_20210427();

Contact Acquia Support to obtain the following required values, which are used with the settings.php provided code overrides:

  • [colony]
  • [core ID]
  • [derived key]

If you are using the Search API module, override using the search_api_acquia_overrides variable. To override the variables in your settings.php file, add the following code after the Acquia include statement (being sure to also replace 'acquia_search' in the code with the machine name of the search_api server):

// For Drupal 7 using Search API Solr and Acquia Search
 if (isset($_ENV['AH_SITE_ENVIRONMENT'])) {
   if ($_ENV['AH_SITE_ENVIRONMENT'] == 'prod') {
         $conf['search_api_acquia_overrides']['acquia_search'] = array(
               'path' => '/solr/[core_ID]',
               'host' => '[colony].acquia-search.com',
               'derived_key' => '[derived_key]',
               'port' => '443',
               );
         }
   elseif ($_ENV['AH_SITE_ENVIRONMENT'] == 'test') {
         $conf['search_api_acquia_overrides']['acquia_search'] = array(
               'path' => '/solr/[core_ID]',
               'host' => '[colony].acquia-search.com',
               'derived_key' => '[derived_key]',
               'port' => '443',
               );
         }
   elseif ($_ENV['AH_SITE_ENVIRONMENT'] == 'dev') {
        $conf['search_api_acquia_overrides']['acquia_search'] = array(
               'path' => '/solr/[core_ID]',
               'host' => '[colony].acquia-search.com',
               'derived_key' => '[derived_key]',
               'port' => '443',
               );
         }
   } else {
      // Local or other non-acquia-hosted Drupal environment
         $conf['search_api_acquia_overrides']['acquia_search'] = array(
              'path' => '/solr/[core_ID]',
              'host' => '[colony].acquia-search.com',
              'derived_key' => '[derived_key]',
              'port' => '443',
              );
         }

Contact Acquia Support to obtain the following required values, which are used with the settings.php provided code overrides:

  • [colony]
  • [core ID]
  • [derived key]

When using the Apache Solr Search Integration module, override the apachesolr_environments variable. To override the variables in your settings.php file, add the following code after the Acquia include statement:

// For Drupal 7 using Apache Solr Search Integration and Acquia Search
 if (isset($_ENV['AH_SITE_ENVIRONMENT'])) {
   switch ($_ENV['AH_SITE_ENVIRONMENT']) {
    case 'test':
     $conf['apachesolr_environments']['acquia_search_server_1']['url']
         = 'http://[colony].acquia-search.com:80/solr/[core_ID]';
     $conf['apachesolr_environments']['acquia_search_server_1']['conf']
         ['acquia_search_key'] = '[derived_key]';
     break;
    case 'dev':
     $conf['apachesolr_environments']['acquia_search_server_1']['url'] =
         'http://[colony].acquia-search.com:80/solr/[core_ID]';
     $conf['apachesolr_environments']['acquia_search_server_1']['conf']
         ['acquia_search_key'] = '[derived_key]';
     break;
    }
   }
   else {
    // Set index to read-only in local development environments
     $conf['apachesolr_read_only'] = "1";
   }

Disabling auto switching

Note for Site Factory subscribers

Site Factory subscribers must modify the following provided code examples to use $GLOBALS['gardens_site_settings']['env'] instead of $_ENV['AH_SITE_ENVIRONMENT'], as the value of $_ENV['AH_SITE_ENVIRONMENT'] changes during the code deployment process.

Select the code override to use based on your installed Drupal version and the module in use, from the following tabs:

Important

EOL notice! Acquia Search with Solr 3/4 will reach end-of-life on Tuesday, March 1, 2022. For more information, see Acquia Search FAQ.

Acquia Search with Solr 7
Setting acquia_search.settings.read_only to TRUE avoids auto switching, causing the production Solr index to enforce read/write mode as shown in the following code example:

$config['acquia_search.settings']['read_only'] = TRUE;
  • Drupal 8
    Setting acquia_search.settings.disable_auto_switch to TRUE avoids auto switching, causing the production Solr index to be used in read/write mode. When the variable isn’t set or is set to FALSE, setting acquia_search.settings.disable_auto_read_only to TRUE disables the read-only behavior. Acquia Search will still attempt to switch to the right index.
  • Drupal 7
    Setting acquia_search_disable_auto_switch to TRUE avoids auto switching. If you don’t have the Acquia Search Multiple Indexes module configured or have another code override set, your website will connect to your production index. When the variable isn’t set or is set to FALSE, setting acquia_search_disable_auto_read_only to TRUE disables the read-only behavior. Acquia Search will still attempt to switch to the right index.