Developer documentation

Last updated on
30 April 2025

This page contains some documentations for developers who want to work with the Search API Solr Search module, either to build on it, use it in custom ways or to write patches for it.

Extending the service class

For many modifications of the behaviour, using the hooks documented in search_api_solr.api.php will suffice. However, more and advanced modifications are possible when overriding methods on the service class itself, to change how certain situations are handled.

To do this (without hacking the class directly), you simply need to create a subclass of SearchApiSolrService in which you override the methods whose inner workings you want to change:

class ExampleSearchService extends SearchApiSolrService {
  // Change how autocompletion works on your server.
  public function getAutocompleteSuggestions(SearchApiQueryInterface $query, SearchApiAutocompleteSearch $search, $incomplete_key, $user_input) {
    // …
  }
}

The preQuery() and postQuery() methods can be used to easily change the query sent to Solr or the results returned (equivalent to hook_search_api_solr_query_alter() and hook_search_api_solr_search_results_alter().

Next step is - include file with that subclass into the .info file of your module, just like this:
files[] = path/to/file/filename

Then, for Solr servers to actually use your custom service class, you need to implement hook_search_api_service_info_alter():

// In your .module file:

/**
 * Implements hook_search_api_service_info_alter().
 */
function MODULE_search_api_service_info_alter(array &$service_info) {
  $service_info['search_api_solr_service']['class'] = 'ExampleSearchService';
}

Alternatively, you can also use hook_search_api_service_info() to add a new service class, so you can also have a Solr server that uses the default service class.

Retrieving / changing the connection class

For communicating with Solr, the Solr service class uses a so-called connection class, an instance of a class implementing the SearchApiSolrConnectionInterface interface. By default, SearchApiSolrConnection is used, but like the service class this can easily be changed. You can either create your own connection class from scratch, or just extend the SearchApiSolrConnection class and override the methods you want to change.

For the service class to actually use your connection class, use the setConnectionClass() method:

/**
 * Implements hook_search_api_server_load().
 */
function MODULE_search_api_server_load(array $servers) {
  foreach ($servers as $server) {
    if ($server->class === 'search_api_solr_service') {
      $server->setConnectionClass('ExampleSolrConnection');
    }
  }
}

Similarly, you can retrieve the connection class the service class is using by calling the getConnectionClass() method, or retrieve the loaded connection object with getSolrConnection(). You can then, e.g., change settings on the connection object, or use it for custom requests.

Making a custom request

Sometimes you might want to make a custom request to a URL on the Solr server – for example, retrieving information from the Luke servlet (e.g., top terms). You can do this by using the SearchApiSolrConnectionInterface::makeServletRequest() method:

$server = search_api_server_load($id);
$connection = $solr->getSolrConnection();
$result = $connection->makeServletRequest('admin/luke', array('show' => 'index'));

Help improve this page

Page status: Not set

You can: