0

I've got a content type of Journal, and another content type of Article. All of the data associated with the Journal itself is stored on that content type, and not duplicated in the Article; the Article just has an entity reference to the Journal. So, for instance, Journal has a Year field, but Article has no Year field. (Conceptually, a Journal is a collection of Articles.)

For the most part, this entity reference relationship works great (and avoids duplicate information.) However, when doing things like searching, it can have some side effects. For example, if someone searches for a keyword, gets back a list of Articles with that keyword, they're then not able to filter that down by Year using facets, since that content type doesn't technically have a year associated with it, just a reference to a different content type that does. I'm hoping to find a way around that, but I'm not sure how to get it working.

I'm still pretty confused on the relationship between the Solr server we have and the Drupal site. It was my idea that once all was indexed, the Solr server takes the search string, and just ships back nodes/entities/etc. that match. If that's wrong or right, I'm still not sure if my problem has to do with Solr or FacetAPI.

What I've done so far is below, but it seems to have no effect on the search results. (In other words, if I search for a year, the articles aren't being returned as results; I'm not even worrying about Facets yet, even though those are the ultimate goal.)

function issue_archive_apachesolr_index_document_build_node(ApacheSolrDocument $document, $node, $env_id){
  if($node->type == "article") {
    // Load the journal this article is associated with, and get it's year
    // The is_ prefix is to denote a single (instead of multi-value) integer
    $journal = node_load($node->field_journal['und'][0]['target_id']);
    $document->setField('is_article_year', intval($journal->field_year['und'][0]['value']));
  }
}

function issue_archive_apachesolr_query_prepare(DrupalSolrQueryInterface $query) {
  // Make the indexed fields available for search results
  $query->addParam('fl', 'is_article_year');
  // Filter where 'index_field_name' = 'indexed_value'
  $query->addFilter('is_article_year', 'indexed_value');
}
0

Figured it out. Adding the answer in case anyone else needs it. Code below...

function issue_archive_apachesolr_index_document_build_node(ApacheSolrDocument $document, $node, $env_id) {
  if($node->type == "article") {
    $node = node_load($node->nid);
    $journal = node_load($node->field_journal['und'][0]['target_id']);
    $document->addField('its_field_year', $journal->field_year['und'][0]['value'] + 0);
    $document->addField('itm_field_year', $journal->field_year['und'][0]['value'] + 0);
  }
}

function issue_archive_apachesolr_query_prepare(DrupalSolrQueryInterface $query) {
  $query->addParam('qf', 'its_field_year');
  $query->addParam('qf', 'itm_field_year');
}

Using the solr_devel module, I saw that the journal type had two fields listed in the entity/document for year, one with the its_ prefix, and the other with the itm_prefix. I added both of those, and added the query field (I think that's what it stands for?) parameters to the query.

This got me the exact results I was looking for.

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.