Displaying CCK field data in Solr results

The Apache Solr Search module adds a lot of functionality to the base Drupal system. You can easily search by title, body, and CCK fields. One you'll often run into is many times the designer and/or client wants CCK field data in the search results. You can easily search, and filter by the CCK fields, but how do you get them back out of the Solr index to display? You could do something extremely inefficient like running node_load() for every returned node ID, but there are better answers. A simple method is the Apache Solr Views module, which integrates Solr into Views. It allows you to use Views to easily customize your searches, but there are still many limitations and there is no release version. Try Apache Solr Views first, and if it doesn't meet your needs, then it is time to learn about hook_apachesolr_modify_query().

As with every Drupal hook, a custom module must be created. You'll need an .info file and a .module file; I name them in the form site_name_solr.info and site_name_solr.module Start with an info file:

name = "ApacheSolr - (Site name)"
description = "ApacheSolr - (Site name) search customization."
package = (Site name)
core = 6.x
dependencies[] = apachesolr

Then make a basic module file:

<?php
// $Id$
/**
 * @file
 * ApacheSolr - Site name customizations
 */

/**
 * Implementation of hook_apachesolr_modify_query(), changes not visible to user.
 */
function site_name_site_apachesolr_modify_query(&$query, &$params) {
  echo "Testing";
}

hook_apachesolr_modify_query() receives two variables passed by reference, $query and $params. To display CCK fields, we only need the $param variable. Notice the hook is called "modify query"; it has a sibling named "prepare query". The difference between the two is that changes in "prepare query" will be seen by the user. If you add a node type filter in "prepare query" the change will show on the node type filter block. If you do the same in "modify query" the filter will be hidden from the user. Enable the module and you'll see the word "Testing" appear on every search result page. Now add some actual functionality to this hook.

Many CCK fields are automatically added to the search index using Solr dynamic fields specified in apachesolr/schema.xml. Each type of dynamic field is has a unique prefix so Solr knows what type of content it is. When Drupal submits a CCK field to the Solr index, it adds these prefixes to the field names. Here are some common prefixes from the schema.xml file:

   <dynamicField name="is_*"  type="integer" indexed="true"  stored="true" multiValued="false"/>
   <dynamicField name="im_*"  type="integer" indexed="true"  stored="true" multiValued="true"/>
   <dynamicField name="sm_*"  type="string"    indexed="true"  stored="true" multiValued="true"/>
   <dynamicField name="tm_*"  type="text"    indexed="true"  stored="true" multiValued="true" termVectors="true"/>
   <dynamicField name="ss_*"  type="string"    indexed="true"  stored="true" multiValued="false"/>
   <dynamicField name="ts_*"  type="text"    indexed="true"  stored="true" multiValued="false" termVectors="true"/>
   <dynamicField name="ds_*" type="date"    indexed="true"  stored="true" multiValued="false"/>
   <dynamicField name="dm_*" type="date"    indexed="true"  stored="true" multiValued="true"/>
   <dynamicField name="bm_*"  type="boolean" indexed="true"  stored="true" multiValued="true"/>
   <dynamicField name="bs_*"  type="boolean" indexed="true"  stored="true" multiValued="false"/>
   <dynamicField name="fs_*"  type="sfloat"  indexed="true"  stored="true" multiValued="false"/>
   <dynamicField name="fm_*"  type="sfloat"  indexed="true"  stored="true" multiValued="true"/>

A CCK field named field_profile_first_name, becomes ss_cck_field_profile_first_name in the Solr index. Go to admin/reports/apachesolr in the Drupal admin section to determine the correct Solr index field name and confirm the field has been automatically added. Record the name(s) of every field you'd like to retrieve and open your new .module file. To retrieve the field(s), edit your implementation of hook_apachesolr_modify_query() adding a comma separated list of Solr field names to the 'fl' key in the $params variable.

function site_name_site_apachesolr_modify_query(&$query, &$params) {
  $params['fl'] .= ',ss_cck_field_profile_first_name,ss_cck_field_profile_last_name';
}

Now your fields will be returned in the search results. To print the list of returned fields, add print_r($results); to your search-results.tpl.php.

External reference:
Adding Custom Fields to Apache Solr Search Results

Comments

I tried the steps above and I am not getting the word "Testing" appear on every search result page.
Please let me know if you have any thoughts on this.

Thanks

h622_solr.info

; $Id$
name = "ApacheSolr - (h622)"
description = "ApacheSolr - (h622) search customization."
package = (h622)
core = 6.x
dependencies[] = apachesolr

h622_solr.module

// $Id$
/**
 * @file
 * ApacheSolr - Site name customizations
 */

/**
 * Implementation of hook_apachesolr_modify_query(), changes not visible to user.
 */
function h622_apachesolr_modify_query(&$query, &$params) {
  echo "Testing";
}

You haven't named the hook_apachesolr_modify_query() hook correctly. If your module is named h622_solr.module, then the hook must be named h622_solr_apachesolr_modify_query(). There is more information here: Telling Drupal about your module .