You are here

function apachesolr_index_node_solr_document in Apache Solr Search 7

Same name and namespace in other branches
  1. 8 apachesolr.index.inc \apachesolr_index_node_solr_document()
  2. 6.3 apachesolr.index.inc \apachesolr_index_node_solr_document()

Builds the node-specific information for a Solr document.

Parameters

ApacheSolrDocument $document: The Solr document we are building up.

object $node: The entity we are indexing.

string $entity_type: The type of entity we're dealing with.

string $env_id: The type of entity we're dealing with.

Return value

array A set of ApacheSolrDocument documents

2 string references to 'apachesolr_index_node_solr_document'
apachesolr_entity_info_alter in ./apachesolr.module
Implements hook_entity_info_alter().
hook_apachesolr_entity_info_alter in ./apachesolr.api.php
Add information to index other entities. There are some modules in http://drupal.org that can give a good example of custom entity indexing such as apachesolr_user, apachesolr_term

File

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

Code

function apachesolr_index_node_solr_document(ApacheSolrDocument $document, $node, $entity_type, $env_id) {

  // None of these get added unless they are explicitly in our schema.xml
  $document->label = apachesolr_clean_text($node->title);

  // Build the node body.
  $language = !empty($node->language) ? $node->language : LANGUAGE_NONE;
  $build = node_view($node, 'search_index', $language);

  // Remove useless html crap out of the render.
  unset($build['#theme']);

  // Allow cache if it's present
  $build['#cache'] = true;

  // Render it into html
  $text = drupal_render($build);
  $document->content = apachesolr_clean_text($text);

  // Adding the teaser
  if (isset($node->teaser)) {
    $document->teaser = apachesolr_clean_text($node->teaser);
  }
  else {

    // If there is no node teaser we will have to generate the teaser
    // ourselves. We have to be careful to not leak the author and other
    // information that is normally also not visible.
    $field_langcode = field_language('node', $node, 'body');
    if (!empty($node->body[$field_langcode][0]['safe_summary'])) {
      $document->teaser = apachesolr_clean_text($node->body[$field_langcode][0]['safe_summary']);
    }
    else {
      $document->teaser = truncate_utf8($document->content, 300, TRUE);
    }
  }

  // Author information
  if ($node->uid == 0 || strlen($node->name) == 0) {

    // @see user_validate_name(). !'0' === TRUE.
    $document->ss_name = '0';
  }
  else {
    $document->ss_name = $node->name;

    // We want the name to be searchable for keywords.
    $document->tos_name = $node->name;
  }

  // Index formatted username so it can be searched and sorted on.
  $account = (object) array(
    'uid' => $node->uid,
    'name' => $node->name,
  );
  $username = format_username($account);
  $document->ss_name_formatted = $username;
  $document->tos_name_formatted = $username;
  $document->is_uid = $node->uid;
  $document->bs_status = $node->status;
  $document->bs_sticky = $node->sticky;
  $document->bs_promote = $node->promote;
  $document->is_tnid = $node->tnid;
  $document->bs_translate = $node->translate;

  // Timestamp of the node
  $document->ds_created = apachesolr_date_iso($node->created);
  $document->ds_changed = apachesolr_date_iso($node->changed);

  // Comment counts + time
  if (isset($node->last_comment_timestamp) && !empty($node->comment_count)) {
    $document->ds_last_comment_timestamp = apachesolr_date_iso($node->last_comment_timestamp);
    $document->ds_last_comment_or_change = apachesolr_date_iso(max($node->last_comment_timestamp, $node->changed));
    $document->is_comment_count = $node->comment_count;
  }
  else {
    $document->ds_last_comment_or_change = apachesolr_date_iso($node->changed);
  }

  // Fetch extra data normally not visible, including comments.
  // We do this manually (with module_implements instead of node_invoke_nodeapi)
  // because we want a keyed array to come back. Only in this way can we decide
  // whether to index comments or not.
  $extra = array();
  $excludes = variable_get('apachesolr_exclude_nodeapi_types', array());
  $exclude_nodeapi = isset($excludes[$node->type]) ? $excludes[$node->type] : array();
  foreach (module_implements('node_update_index') as $module) {

    // Invoke nodeapi if this module has not been excluded, for example,
    // exclude 'comment' for a type to skip indexing its comments.
    if (empty($exclude_nodeapi[$module])) {
      $function = $module . '_node_update_index';
      if ($output = $function($node)) {
        $extra[$module] = $output;
      }
    }
  }

  // Adding the text of the comments
  if (isset($extra['comment'])) {
    $comments = $extra['comment'];

    // Remove comments from the extra fields
    unset($extra['comment']);
    $document->ts_comments = apachesolr_clean_text($comments);

    // @todo: do we want to reproduce apachesolr_add_tags_to_document() for comments?
  }

  // If there are other extra fields, add them to the document
  if (!empty($extra)) {

    // Use an omit-norms text field since this is generally going to be short; not
    // really a full-text field.
    $document->tos_content_extra = apachesolr_clean_text(implode(' ', $extra));
  }

  // Add additional indexing based on the body of each record.
  apachesolr_index_add_tags_to_document($document, $text);

  //  Generic use case for future reference. Callbacks can
  //  allow you to send back multiple documents
  $documents = array();
  $documents[] = $document;
  return $documents;
}