Integrating Views and Apache Solr (apachesolr_views)

Error message

The spam filter installed on this site is currently unavailable. Per site policy, we are unable to accept new submissions until that problem is resolved. Please try resubmitting the form in a couple of minutes.

Apache Solr integration with drupal is already a well-known way to boost search functionality on Drupal, either by increasing performance or by the flexibility provided for the search pages and results. It has introduced faceted search in a very organic fashion, and there is little doubt about its popularity. It can even be found in use on Drupal's official community site .

But the Drupal developer faces many decisions when building a search system with Apache Solr. For example, we recently needed to decide on an implementation that integrated Drupal and Apache Solr with Views and CCK. This led us to work with Apache Solr Views, a poweful module that allows you to use Views to query the Apache Solr index. Below are some helpful tips on going through the task of getting these projects to work together.

Indexing CCK fields and other data

By default, Apache Solr indexes some CCK fields, but it does so only with text fields that use Option Widgets. This is to avoid cluttering Apache Solr with too much data. However, there is an easy way of telling Apache Solr to index your CCK field, and this will be important for building views later. This is accomplished easily by implementing the hook hook_apachesolr_cck_fields_alter(). Check the documentation: There is a simple mapping array you have to return to index CCK fields. If your project requires you to index more complex data, perhaps you can combine the content of several fields to generate a term. Then, you can just go the source of the index process on Apache Solr by implementing hook_apachesolr_update_index():

<?php
/**
 * Implementation of hook_apachesolr_update_index()
 */
function my_custom_apachesolr_update_index(&$document, $node) {

 
// Example
  // Index og description and a custom field content
 
if (isset($node->og_description) && !empty($node->og_description)) {
   
$field = array(
     
'name' => 'myfield',
     
'index_type' => 'text',
    );
   
$key = apachesolr_index_key($field);
   
$content = $node->og_description . '-' . $node->field_custom[0]['value'];
   
$document->$key = $content;
  }
}
?>

Here you can index any data, but only store and index the necessary data. Use the apachesolr_index_key() to generate the key name, for standards' sake. The new data will be available after re-indexing your site's content.

Creating an Apache Solr View

Apache Solr Views works based on setting up a new Views base table for every Drupal table. For the 'node' table, there is a corresponding 'apachesolr_node' counterpart. This prevents the view from generating MySQL queries. Instead, the handlers will be fed by Apache Solr search results. So, if you want to create a view to browse Apache Solr results, you will need to start by selecting one of the Apache Solr base tables. After that, building your Views follows the the same path as normal Views. However, it is important to note that only some handlers have been ported. You can use handlers from the default Views, but not all of them work. If there's not an Apache Solr Views counterpart for a handler, you may have to tweak the code.

Exposing data to Views (as Apache Solr Views data)

For many fields, filters, sorts and arguments: If you need to create a new handler for a custom field, you might have success using an existing Apache Solr Views handler. However, you will need to implement hook_views_data_alter() to expose new data to Views:

<?php
/**
 * Implementation of hook_views_data_alter()
 */
function my_custom_views_data_alter(&$data) {
 
$data['apachesolr_node']['sort_ss_field_custom'] = array(
   
'title' => t('My Custom field'),
   
'help' => t('My Custom field help text.'),
   
'sort' => array(
     
'handler' => 'apachesolr_views_handler_sort',
    ),
   
'argument' => array(
     
'handler' => 'apachesolr_views_handler_argument',
    ),
  );
}
?>

This works well for sorting and arguments. For a field handler, try using views_handler_field. If this doesn't work, depending on your field's nature, you might have to implement a custom handler. The details of creating a custom handler can be found in the developer documentation for Views (or the full API documentation). In short, you should implement hook_views_handlers() to tell Views about your handler and extend some existing handler. Then, re-implement some functions inside the class. I recommend taking the time to read Views documentation and looking at Views code in order to understand how handlers work. The critical point here is that you can extend a basic Views handler and make it work with Apache Solr Views as easy as:

<?php
/**
 * My Custom field class
 */
class my_custom_handler_field_custom_field extends views_handler_field_node {
  function
query() {
   
$this->field_alias = $this->query->add_solr_field('ts_custom_field');
  }
  function
render($values) {
   
// Implement rendering, if needed
 
}
}
?>

The real catch is the query() implementation. You need to add the field name, which you can check on Apache Solr index information page.

Conclusion

Working with Apache Solr Views provided us with the well-known flexibility and power to create and display content using CCK and Views, while benefiting from the performance, scalability and search-related flexibility of Apache Solr. Together we had an ultimate UI for searching and browsing content. Although some related modules need more time to mature, they can provide a great advantage for any implementation of search on sizable projects.