You are here

public static function IndexFactory::bulkIndex in Elasticsearch Connector 8.7

Same name and namespace in other branches
  1. 8.2 src/ElasticSearch/Parameters/Factory/IndexFactory.php \Drupal\elasticsearch_connector\ElasticSearch\Parameters\Factory\IndexFactory::bulkIndex()
  2. 8.5 src/ElasticSearch/Parameters/Factory/IndexFactory.php \Drupal\elasticsearch_connector\ElasticSearch\Parameters\Factory\IndexFactory::bulkIndex()
  3. 8.6 src/ElasticSearch/Parameters/Factory/IndexFactory.php \Drupal\elasticsearch_connector\ElasticSearch\Parameters\Factory\IndexFactory::bulkIndex()

Build parameters to bulk delete indexes.

Parameters

\Drupal\search_api\IndexInterface $index: Index object.

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

Return value

array Array of parameters to send along to Elasticsearch to perform the bulk index.

File

src/ElasticSearch/Parameters/Factory/IndexFactory.php, line 106

Class

IndexFactory
Create Elasticsearch Indices.

Namespace

Drupal\elasticsearch_connector\ElasticSearch\Parameters\Factory

Code

public static function bulkIndex(IndexInterface $index, array $items) {
  $params = static::index($index);
  foreach ($items as $id => $item) {
    $data = [
      '_language' => $item
        ->getLanguage(),
    ];

    /** @var \Drupal\search_api\Item\FieldInterface $field */
    foreach ($item as $name => $field) {
      $field_type = $field
        ->getType();
      if (!empty($field
        ->getValues())) {
        $values = array();
        foreach ($field
          ->getValues() as $value) {
          switch ($field_type) {
            case 'string':
              $values[] = (string) $value;
              break;
            case 'text':
              $values[] = $value
                ->toText();
              break;
            case 'boolean':
              $values[] = (bool) $value;
              break;
            default:
              $values[] = $value;
          }
        }
        $data[$field
          ->getFieldIdentifier()] = $values;
      }
    }
    $params['body'][] = [
      'index' => [
        '_id' => $id,
        '_type' => $index
          ->id(),
      ],
    ];
    $params['body'][] = $data;
  }

  // Allow other modules to alter index params before we send them.
  $indexName = IndexFactory::getIndexName($index);
  $dispatcher = \Drupal::service('event_dispatcher');
  $buildIndexParamsEvent = new BuildIndexParamsEvent($params, $indexName);
  $event = $dispatcher
    ->dispatch(BuildIndexParamsEvent::BUILD_PARAMS, $buildIndexParamsEvent);
  $params = $event
    ->getElasticIndexParams();
  return $params;
}