Last updated May 29, 2013. Created by drunken monkey on May 20, 2013.
Log in to edit this page.

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:

<?php
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().

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

<?php
// 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:

<?php
/**
* 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:

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

Looking for support? Visit the Drupal.org forums, or join #drupal-support in IRC.

Comments

Im trying to extend the solr service class but i can't figure out what im doing wrong. I'm following the example above and if i have an solr server it throws an error:

SearchApiException: Search server with machine name test specifies illegal service class search_api_solr_service. in SearchApiServer->ensureProxy() (line 133 of \sites\all\modules\search_api\includes\server_entity.inc).

I guess this is fine an intendended. But if i follow the steps above and make a new solr server after that I can fill in some basic stuff (everything on the first page of creation a solr server). After that when i hit create server i come a almost blank page which says: "Please configure the used service." and a button which says: "Create server". If i push the button it reloads and stays on same page.

You can reproduce the issue if you make a class extending searchapisolrservice and using a hook.

<?php
class SuggestSolr extends SearchApiSolrService {
 
// Change how autocompletion works on your server.
 
public function getAutocompleteSuggestions(SearchApiQueryInterface $query, SearchApiAutocompleteSearch $search, $incomplete_key, $user_input) {
   
// …
 
}
}
function
MODULE_search_api_service_info_alter(array &$service_info) {
 
$service_info['search_api_solr_service']['class'] = 'SuggestSolr';
}
?>

Any help is much appreciated.