Hi

I'm trying to show facets from linked node types, so for example I have reviews of books (node type 'review') and each one contains a cck nodereference field to one or more books.

I don't want the user to see books, I want them to see reviews, but to be able to select reviews of genre 'non-fiction' - genre is a facet of book though and not review. Or by age range, again a facet of book not review.

This is probably Solr 101 stuff but I've been searching and still stumped as to how I code my specific relationships.

TIA

Comments

Anonymous’s picture

Status: Active » Closed (fixed)

Ah, I see the dev version...

Aldus’s picture

Priority: Normal » Critical
Status: Closed (fixed) » Active

I have exactly the same issue. Did you find out how to solve it? I can't find this functionality in the dev or beta versions

Anonymous’s picture

I didn't I'm afraid :(

I'm going to dig into the Apache Solr documentation at some point, just not had chance to as yet... perhaps someone else reading this will have and may help us out!

30equals’s picture

subscribe

Scott Reynolds’s picture

Lets not have duplicate issues going all over the place. #776220: Using Facets from referenced node?

Solr is a document style database. What this means is that you don't have relational style joins.

So that means that in order to do what you want you have to de-normalize your data. This can be done via hook_apachesolr_update_index. So when so the example that started this issue

// THIS HAS NOT BEEN TESTED. TREAT AS PSEUDOCODE
function MY_MODULE_apachesolr_update_index(&$document, $node) {
  if ($node->type == 'review') {
     foreach ($node->field_books as $book_nodereference) {
       foreach (array('facet_1', 'facet_2') as $facet) {
          $document->$facet = node_load($book_nodereference['nid'])->$facet;
       }
     }
  }
}

Essentially, that takes all the facet fields you want to use from the book node type (e.g. 'facet_1' and 'facet_2') and puts them on the review type as well.

Scott Reynolds’s picture

Priority: Critical » Normal

Def not critical as it doesn't prevent the module from working. Please be mindful of that @aldus

Aldus’s picture

sorry for the duplicate and wrong priority setting, and thank you Scott for the very clear explanation. I'll go with Views for creating relational facets

jpmckinney’s picture

Status: Active » Fixed

Scott explains how to solve the problem in #5 and offers some code. Here is a cleaner version:

function MY_MODULE_apachesolr_update_index(&$document, $node) {
  if ($node->type == 'review') {
     foreach ($node->field_books as $nodereference) {
        $book = node_load($nodereference['nid']);
        if ($book && $book->field_genre) {
          foreach ($book->field_genre as $genre) {
            $document->setMultiValue('sm_genre', $genre['value']);
          }
        }
     }
  }
}

This gets the genre from the book Drupal node onto the review Solr document.

To facet on sm_genre, you need to implement another hook:

function MY_MODULE_apachesolr_facets() {
  $facets['sm_genre'] = array(
    'info' => t('Review genre'),
    'facet_field' => 'sm_genre',
  );
  return $facets;

You then need to enable this filter and add its block to a region.

Anonymous’s picture

Status: Fixed » Closed (fixed)

That's a wonderful example, thanks!

...and thanks for all your help on this, hopefully I will get chance over the summer to get the solr search up and running again on http://booksforkeeps.co.uk and implement this, will be great to have so much data easily findable.