Adding multiple images to your apachesolr search results.

It's possible that this is not the most elegant solution, but it works for me.

It adds images with a specific imagefield to the search index, and when the node appears in the apachesolr search results, the images are returned together with the snippets and all.

I divided into these main steps.

1. the coding

2. the theming

1. the coding

First we need to add the images to the index.

function MYMODULE_apachesolr_update_index(&$document, $node) {
if (count($node->field_CUSTOMIMAGEFIELD)) {
//add the cck image field called field_main_image as a separate field to the SOLR search index schema
foreach ($node->field_CUSTOMIMAGEFIELD as $image) {
$document->setMultiValue('sm_field_CUSTOMIMAGEFIELD', $image['filepath']);
}
}
}

Modification of the search query to solr so the imagefields are returned as well.

function MYMODULE_apachesolr_modify_query(&$query, &$params, $caller) {
$params['fl'] .= ',sm_field_CUSTOMIMAGEFIELD';
}

When a node gets indexed, the image field should be added to the index.

Hook _apachesolr_update_index does so.

function MYMODULE_apachesolr_update_index(&$document, $node) {
if (count($node->field_CUSTOMIMAGEFIELD)) {
//add the cck image field called field_main_image as a separate field to the SOLR search index schema
foreach ($node->field_CUSTOMIMAGEFIELD as $image) {
$document->setMultiValue('sm_field_CUSTOMIMAGEFIELD', $image['filepath']);
}
}

The index does not know that the image is actually a string.
The hook cck_fields_alter below changes this mapping so the filepath will be added as a string to the index.
A callback function is also added.

function MYMODULE_apachesolr_cck_fields_alter(&$mappings) {
$mappings['per-field']['field_CUSTOMIMAGEFIELD'] = array(
'callback' => 'MYMODULE_apachesolr_callback',
'index_type' => 'string'
);
}

The function callback referenced above:

/**
* A function that gets called during indexing.
* @node The current node being indexed
* @fieldname The current field being indexed
*
* @return an array of arrays. Each inner array is a value, and must be
* keyed 'value' => $value
*/
function MYMODULE_apachesolr_callback($node, $fieldname) {
$fields = array();
foreach ($node->$fieldname as $field) {
// In this case we are indexing the filemime type. While this technically
// makes it possible that we could search for nodes based on the mime type
// of their file fields, the real purpose is to have facet blocks during
// searching.
$fields[] = array('value' => $field['filemime']);
}
return $fields;
}

2. the theming

search-result.tpl.php add the following:

this is a draft version, you should improve this.

if($show_galleries){
if (isset($result['fields']['sm_field_photogallery_image'])) {
foreach($result['fields']['sm_field_photogallery_image']['value'] as $image){
if($i>$MAX_AMOUNT-1){break;}$i++;
?>


}

}
}