You are here

function apachesolr_multilingual_term_reference_indexing_callback_implementation in Apache Solr Multilingual 6.3

Same name and namespace in other branches
  1. 7 apachesolr_multilingual.index.inc \apachesolr_multilingual_term_reference_indexing_callback_implementation()

Callback that converts term_reference field into an array

TODO needs review!

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 call to apachesolr_multilingual_term_reference_indexing_callback_implementation()
apachesolr_multilingual_term_reference_indexing_callback in ./apachesolr_multilingual.module
Callback that converts term_reference field into an array

File

./apachesolr_multilingual.index.inc, line 15

Code

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

  // Keep ancestors cached
  $ancestors =& variable_static(__FUNCTION__, array());
  $fields = array();
  $vocab_names = array();
  if (!empty($node->{$field_name}) && function_exists('taxonomy_get_parents_all')) {
    $items = array();
    if (array_key_exists($node->language, $node->{$field_name}) && is_array($node->{$field_name}[$node->language])) {
      $items = $node->{$field_name}[$node->language];
    }
    else {
      list($lang, $items) = each($node->{$field_name});
      if (!is_array($items)) {
        $items = array();
      }
    }
    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,
        );

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

        // Store the translated term for later use.
        $vocab_names[$ancestor->vid][] = apachesolr_clean_text(function_exists('i18n_taxonomy_term_name') ? i18n_taxonomy_term_name($ancestor, $node->language) : $ancestor->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;
}