You are here

function apachesolr_index_entities in Apache Solr Search 8

Same name and namespace in other branches
  1. 6.3 apachesolr.index.inc \apachesolr_index_entities()
  2. 7 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) {
    watchdog('Apache Solr', nl2br(check_plain($e
      ->getMessage())), NULL, WATCHDOG_ERROR);
    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);
    $documents = array();
    foreach ($rows as $row) {
      $row_documents = apachesolr_index_entities_document($row, $entity_type, $env_id);
      $documents = array_merge($documents, $row_documents);
    }
    $indexed = apachesolr_index_send_to_solr($env_id, $documents);
    if ($indexed !== FALSE) {
      $documents_submitted += count($documents);
      $index_position = apachesolr_get_last_index_position($env_id, $entity_type);
      $max_changed = $index_position['last_changed'];
      $max_entity_id = $index_position['last_entity_id'];
      foreach ($rows as $row) {
        if (!empty($row->status)) {
          if ($row->changed > $max_changed) {
            $max_changed = $row->changed;
          }
          if ($row->entity_id > $max_entity_id) {
            $max_entity_id = $row->entity_id;
          }
        }
      }
      apachesolr_set_last_index_position($env_id, $entity_type, $max_changed, $max_entity_id);
      apachesolr_set_last_index_updated($env_id, REQUEST_TIME);
    }
  }
  return $documents_submitted;
}