4

I'm trying to add some custom fields(image field, text field) in Apache Solr's search result. I ended up found the code below; however, I don't know:

  1. Where to put this code (is it template.php?)
  2. How to get the information to display them.

`

function mymodule_apachesolr_update_index($document, $node, $namespace) {
  // When indexing nodes, add field from my custom content type.
  if ($node->type === 'my_contenttype') {
    // Grab the image URI and the description.
    $image_uri = $node->field_image['und'][0]['uri'];
    $description = $node->field_description['und'][0]['safe_value'];
    // Add the image and description to the document in the solr index.
    $document->addField('ss_my_contenttype_image', $image_uri);
    $document->addField('ts_my_contenttype_description', $description);
  }
}

function mymodule_apachesolr_query_prepare($query) {
  // Add the image.
  $query->addParam('fl', 'ss_my_contenttype_image');
  // Add the description text.
  $query->addParam('fl', 'ts_my_contenttype_description');
}

I appreciate any advice. Thank you.

5

If you have D7 and latest Solr search integration module then you can follow these steps:

  • Create a custom module with the following code (If your field is single value text then you should add "ts_" prefix to its name.)

    function mymodule_apachesolr_query_alter($query) {
      $query->addParam('fl', 'ts_field_name');
    }
    
  • Add the following function in the template.php file

    function YOUR_THEME_preprocess_search_result(&$variables) {
      $variables['ts_field_name'] = $variables['result']['fields']['ts_field_name'];
    }
    
  • Display the field in search-result.tpl.php

    <?php if ($ts_field_name): ?>
      <?php print $ts_field_name; ?>
    <?php endif; ?>
    

This is a quick way that doesn't use any theme function like theme_apachesolr_search_snippets() for search results snippets.

You can find more information here:

0

Those functions would go to a custom module, in this case named mymodule.module. You can dig out the queried fields in your theme, either in template.php (in mytheme_preprocess_search_result(), where you would typically perhaps process the fields further and modify the $variables array to include the modified fields) or in your search-result.tpl.php directly.

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.