You are here

public function Index::indexSpecificItems in Search API 8

Indexes some objects on this index.

Will return the IDs of items that were marked as indexed – that is, items that were either rejected from indexing (by a processor or alter hook) or were successfully indexed.

Parameters

\Drupal\Core\TypedData\ComplexDataInterface[] $search_objects: An array of search objects to be indexed, keyed by their item IDs.

Return value

string[] The IDs of all items that should be marked as indexed.

Throws

\Drupal\search_api\SearchApiException Thrown if any error occurred during indexing.

Overrides IndexInterface::indexSpecificItems

1 call to Index::indexSpecificItems()
Index::indexItems in src/Entity/Index.php
Indexes a set amount of items.

File

src/Entity/Index.php, line 943

Class

Index
Defines the search index configuration entity.

Namespace

Drupal\search_api\Entity

Code

public function indexSpecificItems(array $search_objects) {
  if (!$search_objects || $this->read_only) {
    return [];
  }
  if (!$this->status) {
    $index_label = $this
      ->label();
    throw new SearchApiException("Couldn't index values on index '{$index_label}' (index is disabled)");
  }

  /** @var \Drupal\search_api\Item\ItemInterface[] $items */
  $items = [];
  foreach ($search_objects as $item_id => $object) {
    $items[$item_id] = \Drupal::getContainer()
      ->get('search_api.fields_helper')
      ->createItemFromObject($this, $object, $item_id);
  }

  // Remember the items that were initially passed, to be able to determine
  // the items rejected by alter hooks and processors afterwards.
  $rejected_ids = array_keys($items);
  $rejected_ids = array_combine($rejected_ids, $rejected_ids);

  // Preprocess the indexed items.
  $this
    ->alterIndexedItems($items);
  $description = 'This hook is deprecated in search_api:8.x-1.14 and is removed from search_api:2.0.0. Please use the "search_api.indexing_items" event instead. See https://www.drupal.org/node/3059866';
  \Drupal::moduleHandler()
    ->alterDeprecated($description, 'search_api_index_items', $this, $items);
  $event = new IndexingItemsEvent($this, $items);
  \Drupal::getContainer()
    ->get('event_dispatcher')
    ->dispatch(SearchApiEvents::INDEXING_ITEMS, $event);
  $items = $event
    ->getItems();
  foreach ($items as $item) {

    // This will cache the extracted fields so processors, etc., can retrieve
    // them directly.
    $item
      ->getFields();
  }
  $this
    ->preprocessIndexItems($items);

  // Remove all items still in $items from $rejected_ids. Thus, only the
  // rejected items' IDs are still contained in $ret, to later be returned
  // along with the successfully indexed ones.
  foreach ($items as $item_id => $item) {
    unset($rejected_ids[$item_id]);
  }

  // Items that are rejected should also be deleted from the server.
  if ($rejected_ids) {
    $this
      ->getServerInstance()
      ->deleteItems($this, $rejected_ids);
  }
  $indexed_ids = [];
  if ($items) {
    $indexed_ids = $this
      ->getServerInstance()
      ->indexItems($this, $items);
  }

  // Return the IDs of all items that were either successfully indexed or
  // rejected before being handed to the server.
  $processed_ids = array_merge(array_values($rejected_ids), array_values($indexed_ids));
  if ($processed_ids) {
    if ($this
      ->hasValidTracker()) {
      $this
        ->getTrackerInstance()
        ->trackItemsIndexed($processed_ids);
    }

    // Since we've indexed items now, triggering reindexing would have some
    // effect again. Therefore, we reset the flag.
    $this
      ->setHasReindexed(FALSE);
    $description = 'This hook is deprecated in search_api:8.x-1.14 and is removed from search_api:2.0.0. Please use the "search_api.items_indexed" event instead. See https://www.drupal.org/node/3059866';
    \Drupal::moduleHandler()
      ->invokeAllDeprecated($description, 'search_api_items_indexed', [
      $this,
      $processed_ids,
    ]);
    $dispatcher = \Drupal::getContainer()
      ->get('event_dispatcher');
    $dispatcher
      ->dispatch(SearchApiEvents::ITEMS_INDEXED, new ItemsIndexedEvent($this, $processed_ids));

    // Clear search api list caches.
    Cache::invalidateTags([
      'search_api_list:' . $this->id,
    ]);
  }

  // When indexing via Drush, multiple iterations of a batch will happen in
  // the same PHP process, so the static cache will quickly fill up. To
  // prevent this, clear it after each batch of items gets indexed.
  if (function_exists('drush_backend_batch_process') && batch_get()) {
    \Drupal::getContainer()
      ->get('entity.memory_cache')
      ->deleteAll();
  }
  return $processed_ids;
}