I have Content Type A and Content Type B. They both use the same vocabulary. I want to achieve the following:

Let's say that node1 of content type A is associated with the terms term1 and term2. When a search returns node1 as a result (based on its body or other fields), I would also like to present to the user all nodes of Content Type B that are associated with term1 and term2.

Is that possible with the apachesolr module?


Apachesolr's "More like this" block, that you can display on your nodes, is similar, but not particularly configurable. It uses both terms and text to determine related nodes, so you can't just base it on a particular term reference field. Also, it appears on the actual nodes (in your example, it would appear only once a user has clicked on and is viewing node1), not on the search results page.

OPTION 1 ----------

If you're looking to include this type of block on nodes, it'd be possible to do precisely what you want with just a view.

  • Create a content view that filters out only the node types you want.
  • Make sure it accepts a term id as a contextual filter to use (i.e. based on the currently viewed node and its terms, it will filter the list of nodes it displays to ones with precisely those terms). To accomplish this, add a contextual filter to your view that is "Content: term_reference_field"!
  • Then, set the default value of this contextual filter to come from the current node if it's not in the URL: select 'Taxonomy term id from URL' as the default value and then select 'Load default filter from node page'. Under that, select the vocabulary that your term reference is based on. Also make sure you select how to handle multiple values (if the current node being viewed has multiple terms selected, do all terms have to match for a related node to be shown, or should just a single value match be enough?).
  • That should be all you need! Test your view out by providing some sample inputs in the 'Preview' area and making sure that the resulting nodes match via their terms!

enter image description here

OPTION 2 ----------

Now, if you're looking to do this on the search results page, that is a little trickier, because you have to look at multiple nodes (all the ones in the results) rather than just a single one.

Here's how I would do that:

  • In your view, from above, make sure you expand the 'More' section at the bottom of the contextual filter, and check that it should be able to accept multiple parameters (if you want to base the 'Related nodes' view on all the nodes from the search result rather than just the top one
  • Implement a hook_preprocess_search_results() function in your custom theme - this should be placed in your template.php file.

      function YOURTHEME_preprocess_search_results(&$variables) {
      // Get nodeids from search results
      $node_ids = array();
      foreach ($variables['results'] as $result) {
        if (!is_object($result['node'])) {
        $node_ids[] = $result['node']->entity_id;
      // Ensure node ids exist
      if (!count($node_ids)) {
    // Now that you have the nids of all the search results on the current page, call the above view with those nodes as parameters 
    // The second argument should be the display on your view you want to call, could be 'page', 'block' or whatever custom one you have
    $view_output = views_embed_view('YOUR_VIEW_MACHINE_NAME', 'block', implode('+', $node_ids));
    if ($view_output) {   
      $variables['my_view_results'] = $view_output;
  • Finally, now that you've set the variable $my_view_results above (with nodes from the search results at view contextual filters!!!), you can call place that variable anywhere in your search_results.tpl.php template file and place it anywhere: above, below or anywhere on your content

      <?php print $my_view_results; ?>

Sorry that Option #2 is kind of complicated. Someone may come up with a more straight-forward answer, but I can only think of using a view and providing it with the right parameters by hooking into hook_preprocess_search_results() to grab the right node ids that are appearing on the search page. Good luck. Let us know if this works out!

  • Thanks a lot for your reply Boriana! I actually want to do this on the search results page. So, when it returns results of Content Type A, I would also like to show results from Content Type B that have the same terms as nodes of Cont. Type A. If you could expand on Option 2, I'd really appreciate it. I think that the core part that I need to know is if there is a way to programmatically do a search to the solr engine.
    – F1234k
    Sep 17 '13 at 18:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.