Dezső BICZÓ

Personal Developer Site

Store Search API Solr settings in settings.php

2015-06-01 15:30:00

If you use different Solr servers for different version (dev, stage, prod) of your sites, then you can not store the Solr server settings in a feature. The easiest and best solution to this problem: store the Solr server settings in the settings.php. Here is a short code snippet which will help you with that...

Put these lines to a module file, for example that feature's module file, which store your Search API index settings.

/**
 * Implements hook_default_search_api_server().
 */
function MY_MODULE_default_search_api_server() {
  $items = array();
 
  $host = variable_get('solr_search_api_host', 'localhost');
  $scheme = variable_get('solr_search_api_scheme', 'http');
  $port = variable_get('solr_search_api_port', '8983');
  $path = variable_get('solr_search_api_path', '\\/solr');
 
  $items['solr'] = entity_import('search_api_server', '{
    "name" : "Master Solr server",
    "machine_name" : "master_solr_server",
    "description" : "",
    "class" : "search_api_solr_service",
    "options" : {
      "clean_ids" : true,
      "site_hash" : true,
      "scheme" : "' . $scheme . '",
      "host" : "' . $host . '",
      "port" : "' . $port . '",
      "path" : "' . $path . '",
      "http_user" : "",
      "http_pass" : "",
      "excerpt" : 1,
      "retrieve_data" : 1,
      "highlight_data" : 1,
      "skip_schema_check" : 0,
      "solr_version" : "4",
      "http_method" : "AUTO"
    },
    "enabled" : "1"
 }');
 
  return $items;
}

After that you can override Solr server settings from the settings.php like this:

$conf['solr_search_api_host'] = 'my.solr.server';
$conf['solr_search_api_scheme'] = 'http';
$conf['solr_search_api_port'] = '8080';
$conf['solr_search_api_path'] = '\\/solr';

Also there is a Search API Solr overrides module, which can do the same, but why would we use an other module, if we can do this without it...

Recent Comments

Tweets by Me