You are here

class MappingFactory in Elasticsearch Connector 8.6

Same name and namespace in other branches
  1. 8.7 src/ElasticSearch/Parameters/Factory/MappingFactory.php \Drupal\elasticsearch_connector\ElasticSearch\Parameters\Factory\MappingFactory
  2. 8.2 src/ElasticSearch/Parameters/Factory/MappingFactory.php \Drupal\elasticsearch_connector\ElasticSearch\Parameters\Factory\MappingFactory
  3. 8.5 src/ElasticSearch/Parameters/Factory/MappingFactory.php \Drupal\elasticsearch_connector\ElasticSearch\Parameters\Factory\MappingFactory

Class MappingFactory.

Hierarchy

  • class \Drupal\elasticsearch_connector\ElasticSearch\Parameters\Factory\MappingFactory

Expanded class hierarchy of MappingFactory

1 file declares its use of MappingFactory
MappingFactoryTest.php in tests/src/Unit/ElasticSearch/Parameters/Factory/MappingFactoryTest.php

File

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

Namespace

Drupal\elasticsearch_connector\ElasticSearch\Parameters\Factory
View source
class MappingFactory {

  /**
   * Helper function. Get the elasticsearch mapping for a field.
   *
   * @param FieldInterface $field
   *
   * @return array|null
   *   Array of settings when a known field type is provided. Null otherwise.
   */
  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;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MappingFactory::mappingFromField public static function Helper function. Get the elasticsearch mapping for a field.