You are here

function apachesolr_index_entities in Apache Solr Search 7

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

Processes all index queues associated with the passed environment.

An environment usually indexes one or more entity types. Each entity type stores its queue in a database table that is defined in the entity type's info array. This function processes N number of items in each queue table, where N is the limit passed as the second argument.

The indexing routine allows developers to selectively bypass indexing on a per-entity basis by implementing the following hooks:

Parameters

string $env_id: The machine name of the environment.

int $limit: The number of items to process per queue table. For example, if there are two entities that are being indexed in this environment and they each have their own queue table, setting a limit of 50 will send a maximum number of 100 documents to the Apache Solr server.

Return value

int The total number of documents sent to the Apache Solr server for indexing.

See also

apachesolr_index_get_entities_to_index()

apachesolr_index_entity_to_documents()

apachesolr_index_send_to_solr()

2 calls to apachesolr_index_entities()
apachesolr_cron in ./apachesolr.module
Implements hook_cron(). Runs the indexing process on all writable environments or just a given environment.
apachesolr_index_batch_index_entities in ./apachesolr.admin.inc
Batch Operation Callback
7 string references to 'apachesolr_index_entities'
apachesolr_get_indexer_table in ./apachesolr.module
Retrieve the indexer table for an entity type.
apachesolr_schema in ./apachesolr.install
Implements hook_schema().
apachesolr_update_7012 in ./apachesolr.install
Rename some variables and update the database tables
apachesolr_update_7013 in ./apachesolr.install
Make consistent (and reduce) field lengths which cause excess pkey length.
apachesolr_update_7014 in ./apachesolr.install
Remove status from the key.

... See full list

File

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

Code

function apachesolr_index_entities($env_id, $limit) {
  $documents_submitted = 0;
  try {

    // Get the $solr object
    $solr = apachesolr_get_solr($env_id);

    // If there is no server available, don't continue.
    if (!$solr
      ->ping(variable_get('apachesolr_ping_timeout', 4))) {
      throw new Exception(t('No Solr instance available during indexing.'));
    }
  } catch (Exception $e) {
    apachesolr_log_exception($env_id, $e);
    return FALSE;
  }
  foreach (entity_get_info() as $entity_type => $info) {

    // With each pass through the callback, retrieve the next group of nids.
    $rows = apachesolr_index_get_entities_to_index($env_id, $entity_type, $limit);

    // If there are none for this entity type - ignore it and go to the next
    // entity type.
    if (!count($rows)) {
      continue;
    }
    $documents = array();
    foreach ($rows as $row) {
      $row_documents = apachesolr_index_entities_document($row, $entity_type, $env_id);
      $documents = array_merge((array) $documents, $row_documents);
    }
    $indexed = apachesolr_index_send_to_solr($env_id, $documents);
    if ($indexed !== FALSE) {
      $documents_submitted += count($documents);

      // Check who's the last in line
      $last_row = end($rows);

      // set our last position to the entity id and changed value so we can
      // keep track where we left off
      if (!empty($last_row->changed) && !empty($last_row->entity_id)) {
        apachesolr_set_last_index_position($env_id, $entity_type, $last_row->changed, $last_row->entity_id);
      }
      else {
        $message = 'Environment @env_id: Failure recording indexing progress. Last entity id processed: %entity_id with timestamp %last_changed';
        $variables = array(
          '@env_id' => $env_id,
          '%entity_id' => $last_row->entity_id,
          '%last_changed' => $last_row->changed,
        );

        // Add it to watchdog
        watchdog('Apache Solr', $message, $variables, WATCHDOG_ERROR);
      }
      apachesolr_set_last_index_updated($env_id, REQUEST_TIME);
    }
  }
  return $documents_submitted;
}