You are here

public function SearchApiAlgoliaBackend::indexItems in Search API Algolia 8

Same name and namespace in other branches
  1. 3.0.x src/Plugin/search_api/backend/SearchApiAlgoliaBackend.php \Drupal\search_api_algolia\Plugin\search_api\backend\SearchApiAlgoliaBackend::indexItems()
  2. 2.0.x src/Plugin/search_api/backend/SearchApiAlgoliaBackend.php \Drupal\search_api_algolia\Plugin\search_api\backend\SearchApiAlgoliaBackend::indexItems()

Indexes the specified items.

Parameters

\Drupal\search_api\IndexInterface $index: The search index for which items should be indexed.

\Drupal\search_api\Item\ItemInterface[] $items: An array of items to be indexed, keyed by their item IDs.

Return value

string[] The IDs of all items that were successfully indexed.

Throws

\Drupal\search_api\SearchApiException Thrown if indexing was prevented by a fundamental configuration error.

Overrides BackendSpecificInterface::indexItems

1 call to SearchApiAlgoliaBackend::indexItems()
SearchApiAlgoliaBackend::indexItem in src/Plugin/search_api/backend/SearchApiAlgoliaBackend.php
Indexes a single item on the specified index.

File

src/Plugin/search_api/backend/SearchApiAlgoliaBackend.php, line 195

Class

SearchApiAlgoliaBackend
Class SearchApiAlgoliaBackend.

Namespace

Drupal\search_api_algolia\Plugin\search_api\backend

Code

public function indexItems(IndexInterface $index, array $items) {
  $objects = [];

  /** @var \Drupal\search_api\Item\ItemInterface[] $items */
  foreach ($items as $id => $item) {
    $objects[$id] = $this
      ->prepareItem($index, $item);
  }

  // Let other modules alter objects before sending them to Algolia.
  $this
    ->alterAlgoliaObjects($objects, $index, $items);
  if (count($objects) > 0) {
    $itemsToIndex = [];
    if ($this->languageManager
      ->isMultilingual()) {
      foreach ($objects as $item) {
        $itemsToIndex[$item['search_api_language']][] = $item;
      }
    }
    else {
      $itemsToIndex[''] = $objects;
    }
    foreach ($itemsToIndex as $language => $items) {
      try {
        $this
          ->connect($index, '', $language);
        $this
          ->getAlgoliaIndex()
          ->saveObjects($items);
      } catch (AlgoliaException $e) {
        $this
          ->getLogger()
          ->warning(Html::escape($e
          ->getMessage()));
      }
    }
  }
  return array_keys($objects);
}