1

I would like to index the value/s from EntityReference, to be included in Solr results.

Example, My entityreference field has nodes "Foo" and "Bar" in "Lorem Ipsum" taxonomy term.

I'm using Solr to render my Taxonomy page.

Is there a chance to include "Foo" and "Bar" to my "Lorem Ipsum" Solr results and sorted properly (in order)?

So it is like merging Entity Reference values and Solr results.

1 Answer 1

0

Here's what I did to merge entity reference nodes to Solr results (Solr-generated taxonomy terms)

I did a _term_load()_ on _theme_preprocess_search_results()_ to pull all the entities from entity reference field and load them using _entity_load()_ and get the renderable array by using _entity_view()_ and render it by render().

After assigning those referenced nodes into a variable, I printed the variable in search-results.tpl.php above the _$search_results_.

It should display the referenced nodes into Solr page, above the actual Solr results.

To prevent displaying the node twice, I filtered all nodes from entity reference field not to show in current Solr page.

foreach ($term->field_article_reference[LANGUAGE_NONE] as $key => $val) {
  // Exlclude items that is already in term's entity reference field.
  $excluded_ids[] = $val['target_id'];
}
foreach ($excluded_ids as $excluded_id) {
  $query->addFilter('entity_id', $excluded_id, TRUE);
}

To maintain the total number of rows, here's what I did.

// To get rows variable subract 11 from the total number of reference
// items, first page should start with first item 0.
// Start variable should be the product of $rows and the page number, if
// page number exists. And it should always shows 11 total rows.
$total_rows = 11;
$param = drupal_get_query_parameters();
if (empty($param['page'])) {
  $rows = $total_rows - count($term->field_hero_stories[LANGUAGE_NONE]);
  $start = 0;
  $query->addParam('start', $start);
  $query->addParam('rows', $rows);
}
else {
  $rows = $total_rows - count($term->field_hero_stories[LANGUAGE_NONE]);
  $start = $rows * $param['page'];
  $query->addParam('start', $start);
  $query->addParam('rows', $total_rows);
}

Note: Maximum items from reference field should not be equal nor more than to the total number of rows.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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