You are here

public static function IndexFactory::mapping in Elasticsearch Connector 8.6

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

Build parameters required to create an index mapping.

TODO: We need also: $params['index'] - (Required) ['type'] - The name of the document type ['timeout'] - (time) Explicit operation timeout.

Parameters

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

Return value

array Parameters required to create an index mapping.

File

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

Class

IndexFactory
Create Elasticsearch Indices.

Namespace

Drupal\elasticsearch_connector\ElasticSearch\Parameters\Factory

Code

public static function mapping(IndexInterface $index) {
  $params = static::index($index, TRUE);
  $properties = [
    'id' => [
      'type' => 'keyword',
      'index' => 'true',
    ],
  ];

  // Figure out which fields are used for autocompletion if any.
  if (\Drupal::moduleHandler()
    ->moduleExists('search_api_autocomplete')) {
    $autocompletes = \Drupal::entityTypeManager()
      ->getStorage('search_api_autocomplete_search')
      ->loadMultiple();
    $all_autocompletion_fields = [];
    foreach ($autocompletes as $autocomplete) {
      $suggester = \Drupal::service('plugin.manager.search_api_autocomplete.suggester');
      $plugin = $suggester
        ->createInstance('server', [
        '#search' => $autocomplete,
      ]);
      assert($plugin instanceof SuggesterInterface);
      $configuration = $plugin
        ->getConfiguration();
      $autocompletion_fields = isset($configuration['fields']) ? $configuration['fields'] : [];
      if (!$autocompletion_fields) {
        $autocompletion_fields = $plugin
          ->getSearch()
          ->getIndex()
          ->getFulltextFields();
      }

      // Collect autocompletion fields in an array keyed by field id.
      $all_autocompletion_fields += array_flip($autocompletion_fields);
    }
  }

  // Map index fields.
  foreach ($index
    ->getFields() as $field_id => $field_data) {
    $properties[$field_id] = MappingFactory::mappingFromField($field_data);

    // Enable fielddata for fields that are used with autocompletion.
    if (isset($all_autocompletion_fields[$field_id])) {
      $properties[$field_id]['fielddata'] = TRUE;
    }
  }
  $properties['_language'] = [
    'type' => 'keyword',
  ];
  $params['body'][$params['type']]['properties'] = $properties;

  // Allow other modules to alter index mapping before we create it.
  $dispatcher = \Drupal::service('event_dispatcher');
  $prepareIndexMappingEvent = new PrepareIndexMappingEvent($params, $params['index']);
  $event = $dispatcher
    ->dispatch(PrepareIndexMappingEvent::PREPARE_INDEX_MAPPING, $prepareIndexMappingEvent);
  $params = $event
    ->getIndexMappingParams();
  return $params;
}