You are here

function apachesolr_term_reference_indexing_callback in Apache Solr Search 8

Same name and namespace in other branches
  1. 7 apachesolr.index.inc \apachesolr_term_reference_indexing_callback()

Callback that converts term_reference field into an array

Parameters

object $node:

string $field_name:

string $index_key:

array $field_info:

Return value

array $fields fields that will be indexed for this term reference

1 string reference to 'apachesolr_term_reference_indexing_callback'
field_apachesolr_field_mappings in ./apachesolr.module
Implements hook_apachesolr_field_mappings().

File

./apachesolr.index.inc, line 1001
Functions related to Apache Solr indexing operations.

Code

function apachesolr_term_reference_indexing_callback($node, $field_name, $index_key, array $field_info) {

  // Keep ancestors cached
  $ancestors =& drupal_static(__FUNCTION__, array());
  $fields = array();
  $vocab_names = array();
  if (!empty($node->{$field_name}) && function_exists('taxonomy_get_parents_all')) {
    $field = $node->{$field_name};
    list($lang, $items) = each($field);
    foreach ($items as $item) {

      // Triple indexing of tids lets us do efficient searches (on tid)
      // and do accurate per field or per-vocabulary faceting.
      // By including the ancestors to a term in the index we make
      // sure that searches for general categories match specific
      // categories, e.g. Fruit -> apple, a search for fruit will find
      // content categorized with apple.
      if (!isset($ancestors[$item['tid']])) {
        $ancestors[$item['tid']] = taxonomy_get_parents_all($item['tid']);
      }
      foreach ($ancestors[$item['tid']] as $ancestor) {

        // Index parent term against the field. Note that this happens
        // regardless of whether the facet is set to show as a hierarchy or not.
        // We would need a separate field if we were to index terms without any
        // hierarchy at all.
        // If the term is singular, then we cannot add another value to the
        // document as the field is single
        if ($field_info['multiple']) {
          $fields[] = array(
            'key' => $index_key,
            'value' => $ancestor->tid,
          );
        }
        $fields[] = array(
          'key' => 'tid',
          'value' => $ancestor->tid,
        );
        $fields[] = array(
          'key' => 'im_vid_' . $ancestor->vid,
          'value' => $ancestor->tid,
        );
        $name = apachesolr_clean_text($ancestor->name);
        $vocab_names[$ancestor->vid][] = $name;

        // We index each name as a string for cross-site faceting
        // using the vocab name rather than vid in field construction .
        $fields[] = array(
          'key' => 'sm_vid_' . apachesolr_vocab_name($ancestor->vid),
          'value' => $name,
        );
      }
    }

    // Index the term names into a text field for MLT queries and keyword searching.
    foreach ($vocab_names as $vid => $names) {
      $fields[] = array(
        'key' => 'tm_vid_' . $vid . '_names',
        'value' => implode(' ', $names),
      );
    }
  }
  return $fields;
}