Thursday 22 January 2015

Drupal 7: Programatically Make Solr 'Read Only' on none live environments . Acquia

The following code is no longer needed once 'Adding Multiple Search Cores' is set up.  However it's worth documentation as the Multi search core facility may not be available for all.

Here's a piece of code I wrote to make a search 'Read Only' - also in here is how you can set an index up - and leave that one live for testing. 



if ($_ENV['AH_SITE_ENVIRONMENT'] == 'prod' ) {
  // check for live environment
}
else {

  $result = db_query('SELECT name, server, read_only, machine_name
 FROM {search_api_index}
 ');
  $rowcount = $result->rowCount();
  //print "Query $rowcount 
";
 foreach ($result as $record) {
  // print_r($record);
 $read_only_value = $record->read_only;
    // print "Read only value = $read_only_value - v1
";
 // machine_name check here has only been added for pre testing before going live with this search
 // delete when this search is added for real.
 if ($read_only_value == '0' && machine_name != 'candidate_cv_search'){
      $updated = db_update('search_api_index') // Table name no longer needs {}
 ->fields(array(
              'read_only' => 1,
          ))
          ->condition('read_only', '0', '=')
          ->execute();
    }
  }

}


No comments: