hook_apachesolr_cck_fields_alter(&$mappings)

Instructs the apachesolr module on how to index and display cck fields via the $mappings array, which can be added to or altered by modules implementing the hook. The default $mappings array provided by the apachesolr module only handles text fields with option widgets, nodereference fields and userreference fields:

     $mappings['text'] = array(
        'optionwidgets_select' => array(
          'display_callback' => 'apachesolr_cck_text_field_callback',
          'indexing_callback' => 'apachesolr_cck_text_indexing_callback',
          'index_type' => 'string',
        ),
        'optionwidgets_buttons' => array(
          'display_callback' => 'apachesolr_cck_text_field_callback',
          'indexing_callback' => 'apachesolr_cck_text_indexing_callback',
          'index_type' => 'string',
        ),
      );
      $mappings['nodereference'] = array(
        'nodereference_buttons' => array(
          'display_callback' => 'apachesolr_cck_nodereference_field_callback',
          'indexing_callback' => 'apachesolr_cck_nodereference_indexing_callback',
          'index_type' => 'integer',
        ),
        'nodereference_select' => array(
          'display_callback' => 'apachesolr_cck_nodereference_field_callback',
          'indexing_callback' => 'apachesolr_cck_nodereference_indexing_callback',
          'index_type' => 'integer',
        ),
        'nodereference_autocomplete' => array(
          'display_callback' => 'apachesolr_cck_nodereference_field_callback',
          'indexing_callback' => 'apachesolr_cck_nodereference_indexing_callback',
          'index_type' => 'integer',
        ),
      );
      $mappings['userreference'] = array(
        'userreference_buttons' => array(
          'display_callback' => 'apachesolr_cck_userreference_field_callback',
          'indexing_callback' => 'apachesolr_cck_userreference_indexing_callback',
          'index_type' => 'integer',
        ),
        'userreference_select' => array(
          'display_callback' => 'apachesolr_cck_userreference_field_callback',
          'indexing_callback' => 'apachesolr_cck_userreference_indexing_callback',
          'index_type' => 'integer',
        ),
        'userreference_autocomplete' => array(
          'display_callback' => 'apachesolr_cck_userreference_field_callback',
          'indexing_callback' => 'apachesolr_cck_userreference_indexing_callback',
          'index_type' => 'integer',
        ),
      );
      

Fields with option widgets (text fields, such as drop-downs, that are limited to a fixed set of enumerated values) are mapped to default callbacks because these fields generally present good cases for faceted navigation. These fields are therefore automatically indexed and appear as possible facets within the apachesolr module administrative interface by default.

Mapping by Field Type

In your own module _alter hook implementation you can add additional field types such as:

  $mappings['number_integer']['number'] = array(
  
    //Allows modules to alter cck field text where it is displayed
    'display_callback' => 'your_number_display_callback_here', //optional
    
    //Prepares field for indexing in apachesolr
    'indexing_callback' => 'your_indexing_callback_here', //required
    
    //Establishes the data type to be used for storing your field data in the 
    //Solr index.
    'index_type' => 'integer', //required
  );

hook_apachesolr_cck_fields_alter parameter definitions:

display_callback (optional)

Allows modules to alter cck field text where it is displayed (breadcrumb navigation, facet displays, etc).

facet_block_callback (optional)

Apachesolr provides a default facet block display callback, apachesolr_facet_block, but allows you to construct your own facet block handler should the provided theming hooks and/or the display_callback be insufficient for your needs. [link to separate page with the example apachesolr_facet_block?]

index_type (required)

Establishes the data type to be used for storing your field data in the Solr search index. Note: some types require you to transform your Drupal data to be into the type that you select in order for indexing to succeed. Possible data types include:

  • text
  • string
  • integer
  • sint
  • double
  • boolean
  • date
  • float
  • tdate
  • tint
  • tlong
  • tfloat
  • tdouble
indexing_callback (required)

All cck fields must specify an indexing callback to prepare fields for indexing in apachesolr. The default text field indexing callback, apachesolr_cck_text_indexing_callback, provides an example of how to construct such a hook:

function apachesolr_cck_text_indexing_callback($node, $field_name, $cck_info) {
  $fields = array();
  if (isset($node->{$field_name})) {
    $index_key = apachesolr_index_key($cck_info);
    foreach ($node->$field_name as $field) {
      if ($index_value =  (isset($field['safe']) && strlen($field['safe'])) ? $field['safe'] : FALSE) {
        $fields[] = array(
          'key' => $index_key,
          'value' => $index_value,
        );
      }
    }
  }
  return $fields;
} 

Note: the $index_key generated above via apachesolr_index_key serves to generate a solr dynamic field definition to match the index_type you specified in the index_type parameter of the field mapping array (see above).

Mapping on a Per-Field Basis

You can also add a mapping for a specific field. This will take precedence over any mapping for a general field type. A field-specific mapping would look like:

$mappings['per-field']['field_model_name'] = array(
  'display_callback' => 'your_display_callback_here', //optional
  'indexing_callback' => 'your_indexing_callback_here', //required
  'index_type' => 'string', //required
);

or

$mappings['per-field']['field_model_price'] = array(
  'display_callback' => 'your_number_display_callback_here', //optional
  'indexing_callback' => 'your_indexing_callback_here', //required
  'index_type' => 'float', //required
);

Comments

ShaneOnABike’s picture

I thought it might be useful to also include the following documentation for integer widget usage. Generally, Apachesolr hooks into strings with widgets properly but doesn't support integer widgets just yet.

  $mappings['number_integer'] = array(
   'optionwidgets_select' => array(
      // This function is called when teh facet is being indexed
      'indexing_callback' => 'mymodule_cck_checklist_indexing_callback',
      // This function will get called when the facet links are displayed
      // in facet blocks at search time.
      'display_callback' => 'mymodule_cck_checklist_display_callback',
     'index_type' => 'integer'),
   'optionwidgets_buttons' => array(
      'indexing_callback' => 'mymodule_cck_checklist_indexing_callback',
      // This function will get called when the facet links are displayed
      // in facet blocks at search time.
      'display_callback' => 'mymodule_cck_checklist_display_callback',
      'index_type' => 'integer'),
  );

and for the integration of a cck checklist

function olesolr_cck_checklist_indexing_callback($node, $field_name, $cck_info) {
  // $fields is an array because we send back a key => value pair for every
  // value of the field.
  $fields = array();

  // Don't do anything if this node doesn't have this field.
  if (isset($node->$field_name)) {
    // Get the index key based on the $cck_info.
    $index_key = apachesolr_index_key($cck_info);
    //dsm($index_key);

    // Go through each item and setup the appropriate key
    foreach ($node->$field_name as $field) {
      foreach($field as $key => $value) {
        if ($index_value =  (isset($value) ? $value : FALSE)) {
          $fields[] = array(
            'key' => $index_key,
            'value' => $index_value,
          );
        }
      }
    }
  }
 return $fields;
}
Sarenc’s picture

This is very helpful. One useful addition would be the method to use the select label as the display rather than the value itself - Right now all my filters are 0s and 1s. Would this be done in mymodule_cck_checklist_display_callback()? Any help would be appreciated.

EDIT: I discovered this is only for the 2.X beta versions (should have read the title!). You can then use the mymodule_cck_checklist_display_callback() to customize the display.