You are here

function apachesolr_index_entity_to_documents in Apache Solr Search 7

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

Worker callback for apachesolr_index_entities().

Loads and proccesses the entity queued for indexing and converts into one or more documents that are sent to the Apache Solr server for indexing.

The entity is loaded as the user specified in the "apachesolr_index_user" system variable in order to prevent sentive data from being indexed and displayed to underprivileged users in search results. The index user defaults to a user ID of "0", which is the anonymous user.

After the entity is loaded, it will be handed over to apachesolr_convert_entity_to_documents() to be converted to an array via the callback specified in the entity type's info array. The array that the entity is converted to is the model of the document sent to the Apache Solr server for indexing. This function allows developers to modify the document by implementing the following hooks:

  • apachesolr_index_document_build()
  • apachesolr_index_document_build_ENTITY_TYPE()
  • apachesolr_index_documents_alter()

Parameters

stdClass $item: The data returned by the queue table containing:

  • entity_id: An integer containing the unique identifier of the entity, for example a node ID or comment ID.
  • entity_type: The unique identifier for the entity, i.e. "node", "file".
  • bundle: The machine-readable name of the bundle the passed entity is associated with.
  • status: The "published" status of the entity. The status will also be set to "0" when entity is deleted but the Apache Solr server is unavailable.
  • changed: A timestamp flagging when the entity was last modified.

string $env_id: The machine name of the environment.

Return value

array An associative array of documents that are sent to the Apache Solr server for indexing.

See also

apachesolr_index_nodes() for the old-skool version.

3 calls to apachesolr_index_entity_to_documents()
apachesolr_devel in ./apachesolr.admin.inc
Page callback for node/%node/devel/apachesolr.
apachesolr_index_entities_document in ./apachesolr.index.inc
Convert a certain entity from the apachesolr index table to a set of documents. 1 entity can be converted in multiple documents if the apachesolr_index_entity_to_documents decides to do so.
DrupalSolrNodeTestCase::testNodeToDocument in tests/apachesolr_base.test

File

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

Code

function apachesolr_index_entity_to_documents($item, $env_id) {
  global $user;
  drupal_save_session(FALSE);
  $saved_user = $user;

  // build the content for the index as an anonymous user to avoid exposing restricted fields and such.
  // By setting a variable, indexing can take place as a different user
  $uid = variable_get('apachesolr_index_user', 0);
  if ($uid == 0) {
    $user = drupal_anonymous_user();
  }
  else {
    $user = user_load($uid);
  }

  // Pull out all of our pertinent data.
  $entity_type = $item->entity_type;

  // Entity cache will be reset at the end of the indexing algorithm, to use the cache properly whenever
  // the code does another entity_load
  $entity = entity_load($entity_type, array(
    $item->entity_id,
  ));
  $entity = $entity ? reset($entity) : FALSE;

  // Return an empty array if the entity failed to load, but not immediately,
  // since we first need to restore the user.
  $documents = empty($entity) ? array() : apachesolr_convert_entity_to_documents($entity, $entity_type, $env_id);

  // Restore the user.
  $user = $saved_user;
  drupal_save_session(TRUE);
  return $documents;
}