Hey Guys,
I thought I would aggregate some of the newbie knowledge i've gained from attempting to theme the apache solr search results on the normal pages provided.

There seem to be a few options.
-Change search-result.tpl.php in your theme folder to use the content ID given by apache solr to call a single item view that formats your result properly.
(I've been unsuccessful setting this up, but the tutorial I was following was old - used node id, and didn't describe how to set up the contextual filters.)

-The other option for newbies is to simply try to modify the search-result.tpl.php file. I've installed devel to get access to the variables on the search result page, but i'm new enough to not completely understand how to access these variables.

Are there other options when it comes to theme the search result pages? I'm also in the process of trying to add a few fields to the index because I THINK this is the only way for me to gain access to them from the search result page. Am I wrong? http://drupal.org/node/1516428

If you have any further clarifications on the previous two methods that could help me solve the riddle, that would be fantastic. Also open to alternative approaches. Best, J

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

zeezhao’s picture

By the way, if you also use the display suite module: http://drupal.org/project/ds

You can control whether search results are teasers, full nodes, etc.

stopshinal’s picture

This may be a good work-around..
Strange, but there is not a 'title' field available to be moved around the content type display.

zeezhao’s picture

Title appears to be fixed... but it can be moved around when you use one of the layouts in ds

The ds module also enables you to create "view modes". This is then available for customization under "Manage Display" for the content type.

Georgique’s picture

My example:

function inti_preprocess_search_results(&$variables) {  
  $nids = array();
  
  foreach ($variables['results'] as $result) {
    if (!is_object($result['node'])) {
      continue;
    }
    if ($variables['module'] == 'apachesolr_search') {
      if ($result['node']->entity_type == 'node') {
        $nids[] = $result['node']->entity_id;
      }
    }
    else {
      $nids[] = $result['node']->nid;
    }
  }

  if (!count($nids)) {
    return;
  }
  
  // In my view I have one contextual filter which is content->nid and can have multiple values
  $view = views_embed_view('publications', 'default', implode('+', $nids)); 

  // I want to see results count
  $variables['search_results_count'] = sizeof($nids);
  if ($view) $variables['search_results'] = $view; 
}

My search-results.tpl.php where you can see how I add search results count:

<?php if ($search_results): ?>
  <h2>
    <?php print t('Search results'); ?> 
    <?php print ($search_results_count) ? ' (' . $search_results_count . ')' : ''; ?>
  </h2>
  <ol class="search-results <?php print $module; ?>-results">
    <?php print $search_results; ?>
  </ol>
  <?php print $pager; ?>
<?php else : ?>
  <h2><?php print t('Your search yielded no results');?></h2>
  <?php print search_help('search#noresults', drupal_help_arg()); ?>
<?php endif; ?>
stopshinal’s picture

@4 - Are these both in your search-results.php, and used to reference a view which will display the results? If so - what are the other details (or screen shot) of your views config?

many thanks!

Georgique’s picture

@stopshinal
Sorry for delay. First piece of code is used in my module (it is called 'inti'). Second one is template.
In view you just have to add context argument Node:Nid which can get multiple values. It looks like that search module got an array of node ids and then sent it to the view.
I just don't like it because results are rendered twice, but now have not another solution. Also may be you should try Apachesolr Views Integration module.

Nick_vh’s picture

Status: Active » Fixed
Georgique’s picture

And one more notice. If we want to get count of search result, we should use not sizeof($nids) as was in code below, but $variables['response']->response->numFound;

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

nyleve101’s picture

Hi Georgique,

Thanks for the code! Is there any way to print different views depending on the content type?

Evey

drupalninja99’s picture

Category: support » feature
Status: Closed (fixed) » Patch (to be ported)
FileSize
58.25 KB
1.84 KB

DS Search is great, but it's unfortunate that there is not the same option for the Page builder in the Apache Solr module at /admin/config/search/apachesolr/search-pages.

What I did is create a small module that could stand on it's own (or just act as a patch to the apachesolr_search module if it is within scope enough) that gives you the option to override the search results for a specific Apachesolr page you have created. This is great bc you might want 1 search that uses a custom 'gallery teaser' view mode, and another that uses like a 'map view mode', etc..

This is a working version that uses a form_alter to add a 'view mode' dropdown that is similar to the one used in ds_search. The difference is that on the rendering side I am relying on the hook_preprocess_search_results() to do the overriding. There is probably a more efficient way to do this, most likely in a function that is higher up perhaps. But this solution was fairly easy.

I realize that this adds database overhead but so does ds_search and many would be fine with the db overhead in the search results if it meant they could customize the results to actually look like something useful.

Let me know what you think. If this is too out of scope I can release this as it's own module but I wanted to see if it would be better if it lived within the apachesolr project first.

If you just want to take a look at the code you can see it here: http://pastebin.com/GA8mEBwL.

Nick_vh’s picture

what if you could fetch the fields required for the view mode and use those in the theme?

Basically it would index all fields needed for a specific view mode that you have selected. There is some extra balast in the solr system but most of the fields are already indexed anyhow.

I do like the idea though, just not sure how we can easily implement this and how we can easily map solr fields to view mode fields.

The fact that this module does a node_load is not really acceptable for the main module, so I'm ok if you create a contrib module with this, but please warn people that this does add sql/entity_load overhead.

drupalninja99’s picture

Re: "what if you could fetch the fields required for the view mode and use those in the theme?" - that is certainly an option, altho this method is somewhat simpler to me and doesn't require re-indexing every time you change a field.

This method obviously has SQL overhead, just like the ds_search module approach but I think many people will take the trade-off since the actual search itself still uses SOLR and that is where db-searches get bogged down.

I can add this as a contrib module and see what kind of feedback I get.

drupalninja99’s picture

Here is the sandbox project I created for reference - http://drupal.org/sandbox/jaykali/1713498

Nick_vh’s picture

just to note : The ds_search module stores the node object to solr so it does not have SQL overhead when configured correctly.

drupalninja99’s picture

Oh okay I see that now, ya that is a good way to do it for sure.

drupalninja99’s picture

It looks like in apachesolr_search_ds_search_execute() that they use a node_load as a fallback if the node object hasn't been indexed already which is a pretty good idea.

Georgique’s picture

About mapping fields: what if we will create table for search results and use it? We just can store there something like search_id, search_snippet and nid. Then we implement hook_views_data() and work is done, we can use search results in view.

conniemh’s picture

I've been trying to find some non-DS solution to displaying specific fields in the search results and I gave the module a try on a dev site, and it works quite well! I only had around 50 items to index so there was no real db overhead. Following this to keep in the loop.

I'm not a coding expert by any means, but I've been trying to figure out what is being put into the "$snippet" variable in apachesolr_search.module or how to perhaps just create a small module to display only the fields I want to display but still maintaining some of the other module functionality like highlighted results. So this module almost fills the bill. I'm sure others would be interested too if you plan to pursue it.

Thanks!
Connie

Nick_vh’s picture

Status: Patch (to be ported) » Active
nielsaers’s picture

I'm having the same problem and I was wondering if there's already a solution that includes display suite.

nickf’s picture

Issue summary: View changes

I know this is an old post but it may prove useful to anybody who needs to theme the search results templates when using Apache Solr in Drupal 7.

See apachesolr_search.module (in apachesolr module directory):

/**
 * Implements hook_theme_registry_alter().
 */
function apachesolr_search_theme_registry_alter(&$theme_registry) {
  if (isset($theme_registry['search_results'])) {
    $theme_registry['search_results']['variables']['search_page'] = NULL;
  }
}

Copy search-result.tpl.php and search-results.tpl.php from core Search module directory to your theme.

Rename these new files to search_result.tpl.php and search_results.tpl.php. Clear your caches. Make desired template modifications.

arebacollins’s picture

Thank you so much nickf:

Please guide me further in implementing this: (If you would write sample code (and the pages this goes to) that would simply be awesome as Im still trying to decipher the document object of D7.

USE CASE:

a) I have two node types: "Plants", and "Pests". related using a field in the plants node type called "affecting_pests".

b) How do I modify the solr Search result view; in the case of a Pests result, to display " name of pest==>affected plant " in the results title, and instead of link to pest page, redirect to the parent plant that the pest affects?

EXAMPLE:
The Crop Tomato has several pests, among them Tomato blight. In the tomato page, I have linked all related pests into the page (panels page overrides the plants page with a rendered view with node objects and a view of the related pests and diseases ...e.t.c).

In Indexing, Solr indexes these related node items separately, so blight content is indexed separate from the tomato content. As a result, solr search results have the right content but link to the individual nodes, (I dont want blight result leading to the blight node page, but instead, to the tomato node page. Same for all pests related to tomato.

I would be happy to even add an anchor inside of the tomato page with the NID of the blight node type, so that the search result directs the user to the specific page on the tomato page where the instance of the searched node is placed.

Thanks a lot.