I implemented the functionality of allowing the admin to delete solr index recently. The code can be seen here.

Solr provides a simple way of deleting indexing using SolrClient::deleteByQuery. I have provided two types of deleting index:

  • Deleting the complete index in one-go.

  • Deleteing index modularily. (For example, deleting index for records belonging to book and page modules only)

The idea was to make the admin select the delete option: All or let the admin choose the modules. I made these options available to the admin through Moodle Quick Forms. Here is a code snippet:

class search_admin_form extends moodleform {
 
  function definition() {
 
    	$mform = & $this->_form;
    	....
	....
	$modcheckboxarray = array();
	$modcheckboxarray[] =& $mform->createElement('advcheckbox', 'all', '', 'All Modules', array('group' => 1));
	$modcheckboxarray[] =& $mform->createElement('advcheckbox', 'book', '', 'Book', array('group' => 2));
	$modcheckboxarray[] =& $mform->createElement('advcheckbox', 'page', '', 'Page', array('group' => 2));
	$mform->addGroup($modcheckboxarray, 'modadvcheckbox', '', array(' '), false);
	$mform->closeHeaderBefore('modadvcheckbox');
	....
	....
  }
}
 
$mform = new search_admin_form();
 
if ($data = $mform->get_data()) {
  //do stuff here
  //and call search_delete_index() function
 
}

If the admin chooses checkbox: all the $client->deleteByQuery('*:*') is executed, deleting the entire solr index. If, on the other hand the admin chooses only some modules to delete their index, the name of the modules are concatenated together separated by a string, stored as a stdClass $data->module and passed as a parameter into the search_delete_index function, thus executing $client->deleteByQuery('modules:' .$data)

That for the first part: deletion. After deletion, I need to handle the the config settings, so that the admin is able to re-index. This is done by re-setting the values in the config_plugin table. This is done through the below simple code:

foreach ($mods as $key => $name) {
        set_config($name . '_indexingstart', 0, 'search');
        set_config($name . '_indexingend', 0, 'search');
        set_config($name . '_lastindexrun', 0, 'search');
        set_config($name . '_docsignored', 0, 'search');
        set_config($name . '_docsprocessed', 0, 'search');
        set_config($name . '_recordsprocessed', 0, 'search');
}

Here, $mods will be a simple array containing the names of all modules or only those modules whose index was selected for deletion.