You are here

public static function IndexFactory::bulkIndex in Elasticsearch Connector 8.5

Same name and namespace in other branches
  1. 8.7 src/ElasticSearch/Parameters/Factory/IndexFactory.php \Drupal\elasticsearch_connector\ElasticSearch\Parameters\Factory\IndexFactory::bulkIndex()
  2. 8.2 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 112

Class

IndexFactory
Create Elasticsearch Indices.

Namespace

Drupal\elasticsearch_connector\ElasticSearch\Parameters\Factory

Code

public static function bulkIndex(IndexInterface $index, array $items) {
  $params = IndexFactory::index($index, TRUE);
  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;
            default:
              $values[] = $value;
          }
        }
        $data[$field
          ->getFieldIdentifier()] = $values;
      }
    }
    $params['body'][] = [
      'index' => [
        '_id' => $id,
      ],
    ];
    $params['body'][] = $data;
  }
  return $params;
}