You are here

public static function MappingFactory::mappingFromField in Elasticsearch Connector 8.7

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

Helper function. Get the elasticsearch mapping for a field.

Parameters

FieldInterface $field:

Return value

array|null Array of settings when a known field type is provided. Null otherwise.

2 calls to MappingFactory::mappingFromField()
IndexFactory::mapping in src/ElasticSearch/Parameters/Factory/IndexFactory.php
Build parameters required to create an index mapping.
MappingFactoryTest::testMappingFromField in tests/src/Unit/ElasticSearch/Parameters/Factory/MappingFactoryTest.php
@covers ::mappingFromField

File

src/ElasticSearch/Parameters/Factory/MappingFactory.php, line 22

Class

MappingFactory
Class MappingFactory.

Namespace

Drupal\elasticsearch_connector\ElasticSearch\Parameters\Factory

Code

public static function mappingFromField(FieldInterface $field) {
  $type = $field
    ->getType();
  $mappingConfig = NULL;
  switch ($type) {
    case 'text':
      $mappingConfig = [
        'type' => 'text',
        'boost' => $field
          ->getBoost(),
        'fields' => [
          "keyword" => [
            "type" => 'keyword',
            'ignore_above' => 256,
          ],
        ],
      ];
      break;
    case 'uri':
    case 'string':
    case 'token':
      $mappingConfig = [
        'type' => 'keyword',
      ];
      break;
    case 'integer':
    case 'duration':
      $mappingConfig = [
        'type' => 'integer',
      ];
      break;
    case 'boolean':
      $mappingConfig = [
        'type' => 'boolean',
      ];
      break;
    case 'decimal':
      $mappingConfig = [
        'type' => 'float',
      ];
      break;
    case 'date':
      $mappingConfig = [
        'type' => 'date',
        'format' => 'strict_date_optional_time||epoch_second',
      ];
      break;
    case 'attachment':
      $mappingConfig = [
        'type' => 'attachment',
      ];
      break;
    case 'object':
      $mappingConfig = [
        'type' => 'nested',
      ];
      break;
    case 'location':
      $mappingConfig = [
        'type' => 'geo_point',
      ];
      break;
  }

  // Allow other modules to alter mapping config before we create it.
  $dispatcher = \Drupal::service('event_dispatcher');
  $prepareMappingEvent = new PrepareMappingEvent($mappingConfig, $type, $field);
  $event = $dispatcher
    ->dispatch(PrepareMappingEvent::PREPARE_MAPPING, $prepareMappingEvent);
  $mappingConfig = $event
    ->getMappingConfig();
  return $mappingConfig;
}