Rendering an Image in Apache SOLR Search results (Drupal 7)

Introduction

There are a few ways to get extra information into the output of your Apache Solr Search results. The most efficient ways involve getting the information into the solr index and pulling it directly from the solr index. We do not want to introduce extra node_load()  calls to render our search results. We do not want to have to use Views either.

So, using just apachesolr and some functions, I’m going to show how to get an image field into the search index and pull it back out in our result output. I know I’m not handling alt and title attributes in this example. I’ll let you figure that one out.

This is a step-by-step breakdown of my experience getting this to work. In this example I’m attempting to render an image field called field_forum_image for the forum (content type) nodes in my search results. You can use the same procedure for adding other fields, but please make sure you understand the solr prefixes (e.g., ss, ts, tm, etc.) before doing so. You can find these in the schema.xml file with in the apachesolr module.

Prerequisites

  • You must have a custom module in place. You’ll need this to be able to register a few functions.
  • You should be familiar with preprocess functions and how to use them in your theme’s template.php file.
  • You should have a way of inspecting variables (e.g., devel, xdebug, etc.)
  • You should have apachesolr enabled
  • You should already have a search page configured and serving results from solr
  • You should have access to view the solr schema; if you do not have this, you can just hope the field is added without being able to check for sure

Procedure

Step 1

Add implementation of hook_apachesolr_index_document_build()  to your custom module:

Step 2

Clear the Drupal caches

Step 3

Queue all content for reindexing (/admin/config/search/apachesolr/settings/solr/index)

Step 4 (optional)

Check if the field appears in your solr schema.

You can check this in your solr index (e.g., /solr/admin/schema.jsp)

Step 5

Make the field available to theme_preprocess_search_result()  implementations:

Step 6

Implement a preprocess function in your theme’s template.php file (my theme is called “custom”).

You can inspect the variable however you’d like. Here I’m using the dpm()  function from devel.

Step 7

Clear the Drupal cache and your database cache, then perform a search.

You should now see the dpm()  output.

You may have to clear the caches several times, and refresh several times before the dpm()  output appears and before the field appears within it.

Step 8

If  you see the field and proper values, proceed. Otherwise, figure out why you’re not seeing the values.

Step 9

Update your preprocess function to actually do something with the field’s value. In this example I’m rendering the image with an image style called past-forum.

Step 10

Make use of the new variable in your template file. I copied my template file from /modules/search/search-result.tpl.php to my theme’s templates directory as search-result–apachesolr-search–node–forum.tpl.php. I knew what to call this file by using $conf['theme_debug'] = TRUE;  in my settings.php file (google it!)

I updated its code to include the field:

Step 11

Search again and see if it worked.

If you don’t see your field, try clearing Drupal and browser caches and refreshing the page a few times.

In Summary

After following those steps you should be seeing the image in the output, all without burdening the Drupal database with the task of processing each node to get the image. Also, we did this without using Views, which may have introduced a significant hit on performance.

Tagged with: , ,
Posted in Development

Leave a Reply

Your email address will not be published. Required fields are marked *

*