1

I'm really bumping my head on this.

I simply need to add tids from a term reference field on nodes returned by a solr search result.

It's a unique term reference.

I looked for the field in solr console and it's 'im_field_arbo'.

I followed this and that and came to this :

1.

    function MYMODULE_apachesolr_index_document_build(ApacheSolrDocument $document, $entity, $entity_type, $env_id) {
      if(isset($entity->field_arbo)) {
        $document->addField('im_field_arbo', $entity->field_arbo['und'][0]['tid']);
      }
    }

This doesn't throw back any errors.

2.

Then, to retrieve it, I tried either hook_apachesolr_query_alter, hook_apachesolr_query_prepare with :

    $query->addParam('im', 'im_field_arbo');

and hook_apachesolr_modify_query with :

    $params['im'] .= ',im_field_arbo';

Still, no trace of my field in my search results.

Any help ? Thx very much.


Update

I think my field is well indexed : _apachesolr_index_document_build_node works like a charm.

Now the question is : "How do I get them back in search results ?"

  • I know this isn't very helpful, but your code should work (with the addParam() method in hook_apachesolr_query_alter()). I have a nearly identical setup, just using a different field name, and it works great – Clive Jul 30 '13 at 11:17
  • Tell a lie, I'm actually using a hook named hook_apachesolr_index_document_build_node(), with _node on the end. Might be worth changing that, clearing the cache, and giving it a try – Clive Jul 30 '13 at 11:19
  • Thx @Clive , but still nothing... No error though (after I removed "entity_type" from index_document_build_node's args). – Gregory Kapustin Jul 30 '13 at 11:35
1

To show the field you have added to your Solr index you first need to add it to the search result. Add this to template.php

function template_preprocess_search_result(&$variables) {
  $variables['im_field_arbo'] = $variables['result']['fields']['im_field_arbo'];
}

Then in your search-result.tpl.php you can access the field like this:

<?php if ($im_field_arbo): ?>
  <?php print $im_field_arbo; ?>
<?php endif; ?>
  • Thx. Very weird : I can't get that function to act on search... Nor var_dump, dpm, even kill won't be fired. In my theme's template.php, either with template_preprocess_search_result or my_theme_preprocess_search_result... Any help ? – Gregory Kapustin Aug 2 '13 at 11:03
  • update : I of course checked preprocessing, with template_preprocess_mymodule_results, and no trace of the field in $variables. – Gregory Kapustin Aug 2 '13 at 11:31
  • You need to put the function template_preprocess_search_result in your themes template.php file. Rename template with the name of your theme. – Pontus Nilsson Aug 2 '13 at 13:42
  • Tried it already :( – Gregory Kapustin Aug 2 '13 at 13:53
  • Still nothing ; does it work also with solr search ? – Gregory Kapustin Aug 5 '13 at 9:39
-1

Try this,

/**
 * Implements hook_apachesolr_process_results().
 */
function mymodule_apachesolr_process_results(&$results) {
  foreach ($results as $key => &$result) {
    $result['foo'] = $result['node']->im_field_arbo;
  }
}

Call this variable in you search-result.tpl.php e.g.

<?php print $result['foo']; ?>

I think this code will work with the combination of:

function mymodule_apachesolr_query_alter(DrupalSolrQueryInterface $query) {
  $query->addParam('fl', 'im_field_arbo');
}

function mymodule_apachesolr_index_document_build(ApacheSolrDocument $document, $entity, $entity_type) {
  // I think you should do a foreach here, since you are using multi-valued field.
  foreach ($entity->field_arbo[LANGUAGE_NONE] as $key => $val) {
    $tid = $val['tid'];
    // The 1st letter indicates the data type and the last letter is
    // 'm' for multi-valued.
    $document->addField('im_field_arbo', $tid);
  }
}

I hope it helps.

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.