I have been banging my head on this for days now. I manage to get the images on my site indexed in solr using a custom module. Then I can add the image field in my custom solr view. So far so good.

The problem is that the field outputs as "public://folder/imagename.jpg" or so. How could I get the view to output not the link but the image itself? THis is probably some coding work but I cannot figure out how to do it. Any help very much appreciated.

Best,
Gerben

ps: this is connected to this D6 issue: http://drupal.org/node/576286

Comments

Gerben Zaagsma’s picture

We managed to solve this with help from user Gaele Strootman.

There are three steps to solve this:
1) add the image to the solr index using a custom module
2) add the image field to your apache solr view
3) add a template file for the image field that displays the actual image

ad 1) create a custom module to add images to the solr index.

You only need two files, my_module.info and my_module.module. We name the image field ss_field_image_uri.

my.module.info:

name = Apache Solr Image
description = Add image field to the solr index so we can display images in search results
package = Apache Solr
core = 7.x

dependencies[] = apachesolr_search

my_module.module:

/**
 * Add field_image uri to apache solr index, so that we can later display image in search results or custom apache solr views page.
 * @see apachesolr.api.php
 */

function apachesolr_image_apachesolr_index_document_build($document, $node, $namespace) {
  if (isset($node->field_image['und']['0']['uri'])) $document->addField('ss_field_image_uri', $node->field_image['und']['0']['uri']);
}

2) add the image field to your apache solr view

First re-index solr so the images actually get indexed and show up. Then add the image field to your view.

ad 3) add a template file for the image field that displays the actual image

This is the template we added. Since the field is called ss_field_image_uri the template name is: views-view-field--ss-field-image-uri.tpl.php. This is the code:

<?php

/**
 * @file
 * This template is used to print a single field in a view.
 *
 * It is not actually used in default Views, as this is registered as a theme
 * function which has better performance. For single overrides, the template is
 * perfectly okay.
 *
 * Variables available:
 * - $view: The view object
 * - $field: The field handler object that can process the input
 * - $row: The raw SQL result that can be used
 * - $output: The processed output that will normally be used.
 *
 * When fetching output from the $row, this construct should be used:
 * $data = $row->{$field->field_alias}
 *
 * The above will guarantee that you'll always get the correct data,
 * regardless of any changes in the aliasing that might happen if
 * the view is modified.
 */
?>
<?php 
/**
 * For additional parameters see
 * http://api.drupal.org/api/drupal/modules!image!image.field.inc/function/theme_image_formatter/7 
 */
?>
<?php $variables = array('item' => array('uri' => $row->{$field->field_alias})); ?>
<?php $variables['image_style'] = 'thumbnail'; ?>
<?php print theme_image_formatter($variables); ?>

Now clean your caches and the images should show up in your apache solr view. Note that you can change the image style in the code from 'thumbnail' to whatever image style you like.

Hope this helps anyone!

Gerben Zaagsma’s picture

Status: Active » Closed (fixed)

closing this, feel free to re-open if any issues arise.

sboutas’s picture

Hi. I did all the above but in the fields in my solr view I don't see anywhere the ss_field_image_uri.

Why is that?

Gerben Zaagsma’s picture

Can you explain which steps you took and where it goes wrong? Did you create a custom module and enable it? And did you re-index solr after that?

Gerben Zaagsma’s picture

Issue summary: View changes

added info

sboutas’s picture

I followed the first two steps of post #1. I created a custom module with the code provided and I named it apache_solr_image. Then I enabled the module, I cleared solr's index and reindex. Then I created a new view and when I press add field, I cannot find anywhere the ss_field_image_uri

sboutas’s picture

Also, the modules that I am using are the following:

Apache Solr Access - 7.x-1.5
Apache Solr framework - 7.x-1.5
Apache Solr Image - My Custom Module
Apache Solr Link - 7.x-1.x-dev
Apache Solr Multisite Search - 7.x-1.0
Apache Solr search - 7.x-1.5
Apache Solr Views Integration - 7.x-1.0-beta2

sboutas’s picture

Issue summary: View changes
Status: Closed (fixed) » Active

Any idea of how can I fix this?

Gerben Zaagsma’s picture

Apologies for the late reply, haven't checked in for a while.

The problem could be that the module name is wrong, it should be apachesolr_image (not apache_solr_image). So you have a module folder named apachesolr_image with two files in it: apachesolr_image.info and apachesolr_image.module.

hope this helps,
Gerben

sboutas’s picture

Hi Gerben. I did what you said and finally worked!!! Thank you very much for your help. Now I get the image field in the view but the image is not displayed. Maybe it has to do with the template. Where should I place this views-view-field--ss-field-image-uri.tpl.php template file? Inside the module or in another directory?

sboutas’s picture

Update:

I placed the template file inside the theme but now I get this error:

Notice: Undefined property: stdClass::$ss_field_image_uri in include()

Gerben Zaagsma’s picture

Good that you got the imahe field in the view. As for the template: you can actually check what it should be called by clicking Theme: Information in your view (bottom right of advanced section). You should place it in the templates folder inside your theme folder (where the other templates are). Also clean your caches after that.

sboutas’s picture

I checked in the Theme Information and the template is called:

Field Apache Solr: ss_field_image_uri (ID: ss_field_image_uri): views-view-field.tpl.php, views-view-field--ss-field-image-uri.tpl.php, views-view-field--multi-site.tpl.php, views-view-field--multi-site--ss-field-image-uri.tpl.php, views-view-field--page.tpl.php, views-view-field--page--ss-field-image-uri.tpl.php, views-view-field--multi-site--page.tpl.php, views-view-field--multi-site--page--ss-field-image-uri.tpl.php

But still when I am at the view the images are not loading and I get the error:
Notice: Undefined property: stdClass::$ss_field_image_uri in include()

Update:
Actually, I found out that this message is being displayed as many times as the nodes that they have empty field images. So, I have to modify somehow the template to be applied only when the field is not empty. Anyway, I still don't get thumbnails from the nodes that they actually have a field image. Below is the value example that I get from the image field:

public://image/experiment/2013/01/asacusa_0.jpg

Do you think that the problem might be the format?
I am using Apache Solr Multisite Search and the results are coming from three different sites.

sboutas’s picture

Hi. Just to let you know that I have found a temporary solution.
It appears that the public://image/experiment/2013/01/asacusa_0.jpg that i get in the image field is a part of the uri, which normally is public/image/experiment/2013/01/asacusa_0.jpg

Anyway this isn't the full link to the image, so I exposed the site solr field to know from which site the image is coming from and then with the Views php I have added a php field where I wrote some code that checks the site and then creates a new image uri based on the site and part of the image field that I got from the ss_field_image_uri.

After that I rewrote the result from the php field in text to display it as a thumbnail with html code so I don't need to load any template.

As a result I got my images now displayed in solr search results :)

I believe it would be nice though if the ss_field_image could contain the complete image path in a multisite solr index.

Gerben Zaagsma’s picture

The multisite thing is not something I am familiar with so can't help out there. Glad to hear you got it working anyway.

sboutas’s picture

Status: Active » Closed (fixed)