You are here

function apachesolr_realtime_index_now in Apache Solr Real-Time 7

Prepare entity as document for adding to Solr index.

Parameters

object $entity: Entity and its fields.

string $type: The type of entity.

2 calls to apachesolr_realtime_index_now()
apachesolr_realtime_entity_insert in ./apachesolr_realtime.module
Implements hook_entity_insert().
apachesolr_realtime_entity_update in ./apachesolr_realtime.module
Implements hook_entity_update().

File

./apachesolr_realtime.module, line 62
Module file for apachesolr_realtime

Code

function apachesolr_realtime_index_now($entity, $type) {

  // If the entity-type/bundle isn't indexable, abort.
  if (!apachesolr_entity_should_index($entity, $type)) {
    return;
  }
  elseif (!_apachesolr_realtime__entity_should_index($entity, $type)) {
    apachesolr_entity_delete($entity, $type);
    return;
  }

  // Entity should be indexed, so send it to solr.
  list($id, , $bundle) = entity_extract_ids($type, $entity);

  // Create entity Object and assign type and id.
  $item = new stdClass();
  $item->entity_type = $type;
  $item->entity_id = $id;

  // Prepare entity as document and send to solr.
  // Obtain the Solr environments.
  $env_ids = _apachesolr_realtime_get_env_ids($entity->type);
  foreach ($env_ids as $env_id) {
    $doc = apachesolr_index_entity_to_documents($item, $env_id);
    $indexed = @apachesolr_index_send_to_solr($env_id, $doc);

    // Only continue if document was successfully indexed
    if ($indexed) {

      // Commit the changes immediately.
      apachesolr_realtime_commit($env_id);

      // Find the last-changed timestamp, which indicates when documents of this
      // entity type were most recently pushed to Solr. Then, if we find it,
      // decrement it by one, and set this node's Solr index last-changed timestamp
      // to that value. This ensures that, if Solr tries to find nodes to index,
      // this node is not part of that list, since it was just sent to Solr in this
      // function.
      $last_index_position = apachesolr_get_last_index_position($env_id, $type);
      $last_changed = $last_index_position['last_changed'];
      $last_indexed = !empty($last_changed) ? $last_changed - 1 : $last_changed;

      // Update the record's timestamp instead of deleting the record because we
      // want this node's row to remain in indexer table.
      db_merge(apachesolr_get_indexer_table($type))
        ->key(array(
        'entity_type' => $type,
        'entity_id' => $id,
      ))
        ->fields(array(
        'bundle' => $bundle,
        'status' => 1,
        'changed' => $last_indexed,
      ))
        ->execute();
    }
  }
}