You are here

function apachesolr_term_indexing_callback in Apache Solr Search 6.3

Callback that converts term field into an array

1 string reference to 'apachesolr_term_indexing_callback'
taxonomy_apachesolr_field_mappings in ./apachesolr.module
Implements hook_apachesolr_field_mappings() on behalf of taxonomy module.

File

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

Code

function apachesolr_term_indexing_callback($node, $field_name, $index_key, $field_info) {

  // Keep ancestors cached
  static $ancestors = array();
  $fields = array();
  $vocab_names = array();
  $field_terms = array();
  $vid = $field_info['field']['vid'];
  foreach ($node->taxonomy as $tid => $term) {
    if ($term->vid == $vid) {
      $field_terms[$tid] = $term;
    }
  }
  if (!empty($field_terms) && function_exists('taxonomy_get_parents_all')) {
    foreach ($field_terms as $term) {

      // Triple indexing of tids lets us do effecient 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[$term->tid])) {
        $ancestors[$term->tid] = taxonomy_get_parents_all($term->tid);
      }
      foreach ($ancestors[$term->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.
        $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;
}