You are here

public function DefaultSubscriber::elasticsearchConnectorPrepareIndexMapping in Elasticsearch Connector Autocomplete 8

Called on elasticsearch_connector.prepare_index_mapping event.

Parameters

\Symfony\Component\EventDispatcher\Event $event: The event.

File

src/EventSubscriber/DefaultSubscriber.php, line 59

Class

DefaultSubscriber
Class DefaultSubscriber.

Namespace

Drupal\elasticsearch_connector_autocomp\EventSubscriber

Code

public function elasticsearchConnectorPrepareIndexMapping(Event $event) {
  $index = $this
    ->loadIndexFromIndexName($event
    ->getIndexName());
  $params = $event
    ->getIndexMappingParams();
  $ngram_index_analyzer_enabled = $index
    ->getThirdPartySetting('elasticsearch_connector', 'ngram_filter_enabled');
  if ($ngram_index_analyzer_enabled) {
    foreach ($index
      ->getFields() as $field_id => $field_data) {
      if ($field_data
        ->getType() == 'text_ngram') {
        $params['body'][$params['type']]['properties'][$field_id]['type'] = 'text';
        $params['body'][$params['type']]['properties'][$field_id]['boost'] = $field_data
          ->getBoost();
        $params['body'][$params['type']]['properties'][$field_id]['fields'] = [
          "keyword" => [
            "type" => 'keyword',
            'ignore_above' => 256,
          ],
        ];
        $params['body'][$params['type']]['properties'][$field_id]['analyzer'] = 'ngram_analyzer';
        $params['body'][$params['type']]['properties'][$field_id]['search_analyzer'] = 'standard';
      }
    }
  }
  $event
    ->setIndexMappingParams($params);
}