You are here

function search_api_index_specific_items in Search API 7

Indexes the specified items on the given index.

Items which were successfully indexed are marked as such afterwards.

Parameters

SearchApiIndex $index: The index on which items should be indexed.

array $ids: The IDs of the items which should be indexed.

Return value

array The IDs of all successfully indexed items.

Throws

SearchApiException If any error occurs during indexing.

2 calls to search_api_index_specific_items()
search_api_index_items in ./search_api.module
Indexes items for the specified index.
_search_api_index_queued_items in ./search_api.module
Shutdown function which indexes all queued items, if any.

File

./search_api.module, line 1782
Provides a flexible framework for implementing search services.

Code

function search_api_index_specific_items(SearchApiIndex $index, array $ids) {

  // Before doing anything else, check whether there are pending tasks that need
  // to be executed on the server. It might be important that they are executed
  // before any indexing occurs.
  if (!search_api_server_tasks_check($index
    ->server())) {
    throw new SearchApiException(t('Could not index items since important pending server tasks could not be performed.'));
  }
  $items = $index
    ->loadItems($ids);

  // Clone items because data alterations may alter them.
  $cloned_items = array();
  foreach ($items as $id => $item) {
    if (is_object($item)) {
      $cloned_items[$id] = clone $item;
    }
    else {

      // Normally, items that can't be loaded shouldn't be returned by
      // entity_load (and other loadItems() implementations). Therefore, this is
      // an extremely rare case, which seems to happen during installation for
      // some specific setups.
      $type = search_api_get_item_type_info($index->item_type);
      $type = $type ? $type['name'] : $index->item_type;
      watchdog('search_api', "Error during indexing: invalid item loaded for @type with ID @id.", array(
        '@id' => $id,
        '@type' => $type,
      ), WATCHDOG_WARNING);
    }
  }
  $indexed = $items ? $index
    ->index($cloned_items) : array();
  if ($indexed) {
    search_api_track_item_indexed($index, $indexed);

    // If some items could not be indexed, we don't want to try re-indexing
    // them right away, so we mark them as "freshly" changed. Sadly, there is
    // no better way than to mark them as indexed first...
    if (count($indexed) < count($ids)) {

      // Believe it or not but this is actually quite faster than the equivalent
      // $diff = array_diff($ids, $indexed);
      $diff = array_keys(array_diff_key(array_flip($ids), array_flip($indexed)));
      $index
        ->datasource()
        ->trackItemIndexed($diff, $index);
      $index
        ->datasource()
        ->trackItemChange($diff, array(
        $index,
      ));
    }
  }
  return $indexed;
}